【问题标题】:How to send array of complex objects to another page in PHP?如何将复杂对象数组发送到 PHP 中的另一个页面?
【发布时间】:2016-09-20 05:59:58
【问题描述】:

我有一个数组$batchRequest,看起来像这样:

array(5) {
  [0]=>
  array(0) {
  }
  [1]=>
  object(Facebook\FacebookRequest)#18 (9) {
    ["app":protected]=>
    object(Facebook\FacebookApp)#9 (2) {
      ["id":protected]=>
      string(16) "xxxxxxx"
      ["secret":protected]=>
      string(32) "xxxxxxxx"
    }
    ["accessToken":protected]=>
    string(49) "xxxxx|xxxxxxx"
    ["method":protected]=>
    string(3) "GET"
    ["endpoint":protected]=>
    string(75) "/10209064245580796?fields=id%2Cname%2Cpicture%2Cgender%2Cfriends%2Cbirthday"
    ["headers":protected]=>
    array(0) {
    }
    ["params":protected]=>
    array(0) {
    }
    ["files":protected]=>
    array(0) {
    }
    ["eTag":protected]=>
    NULL
    ["graphVersion":protected]=>
    string(4) "v2.5"
  }
  [2]=>
  object(Facebook\FacebookRequest)#17 (9) {
    ["app":protected]=>
    object(Facebook\FacebookApp)#9 (2) {
      ["id":protected]=>
      string(16) "xxxxx"
      ["secret":protected]=>
      string(32) "xxxxxxx"
    }
    ["accessToken":protected]=>
    string(49) "xxxx|xxxxxxxx"
    ["method":protected]=>
    string(3) "GET"
    ["endpoint":protected]=>
    string(75) "/10208823390691752?fields=id%2Cname%2Cpicture%2Cgender%2Cfriends%2Cbirthday"
    ["headers":protected]=>
    array(0) {
    }
    ["params":protected]=>
    array(0) {
    }
    ["files":protected]=>
    array(0) {
    }
    ["eTag":protected]=>
    NULL
    ["graphVersion":protected]=>
    string(4) "v2.5"
  }
  [3]=>
  object(Facebook\FacebookRequest)#19 (9) {
    ["app":protected]=>
    object(Facebook\FacebookApp)#9 (2) {
      ["id":protected]=>
      string(16) "xxxxx"
      ["secret":protected]=>
      string(32) "xxxxxxx"
    }
    ["accessToken":protected]=>
    string(49) "xxxxx|xxxxxxx"
    ["method":protected]=>
    string(3) "GET"
    ["endpoint":protected]=>
    string(74) "/1294280923934896?fields=id%2Cname%2Cpicture%2Cgender%2Cfriends%2Cbirthday"
    ["headers":protected]=>
    array(0) {
    }
    ["params":protected]=>
    array(0) {
    }
    ["files":protected]=>
    array(0) {
    }
    ["eTag":protected]=>
    NULL
    ["graphVersion":protected]=>
    string(4) "v2.5"
  }
  [4]=>
  object(Facebook\FacebookRequest)#20 (9) {
    ["app":protected]=>
    object(Facebook\FacebookApp)#9 (2) {
      ["id":protected]=>
      string(16) "xxxxx"
      ["secret":protected]=>
      string(32) "xxxxxxxx"
    }
    ["accessToken":protected]=>
    string(49) "xxxxx|xxxxxxxxxx"
    ["method":protected]=>
    string(3) "GET"
    ["endpoint":protected]=>
    string(74) "/1274474365912572?fields=id%2Cname%2Cpicture%2Cgender%2Cfriends%2Cbirthday"
    ["headers":protected]=>
    array(0) {
    }
    ["params":protected]=>
    array(0) {
    }
    ["files":protected]=>
    array(0) {
    }
    ["eTag":protected]=>
    NULL
    ["graphVersion":protected]=>
    string(4) "v2.5"
  }
}

所以它是一个数组,其元素是复杂的对象。我需要将它们发送到另一个名为parallelImport.php 的页面。这是我尝试过的:

使用 JSON

$data = array('batchArrayChild' => json_encode($batchRequest), 'app_id' => $appId, 'app_secret' => $appSecret);
$endpoint_url = 'https://some-domain.net/pages/parallelImport.php';
$curl = curl_init($endpoint_url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0);
$curl_response = curl_exec($curl);
$result = $curl_response;
print_r($result);

如您所见,我 json_encodeed $batchRequest 并通过 cURL 发送,这是它的输出:

string(16) "[[],{},{},{},{}]"

使用 http_build_query

$data = array('batchArrayChild' => http_build_query($batchRequest), 'app_id' => $appId, 'app_secret' => $appSecret);
$endpoint_url = 'https://some-domain.net/pages/parallelImport.php';
$curl = curl_init($endpoint_url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0);
$curl_response = curl_exec($curl);
$result = $curl_response;
print_r($result);

在我对parallelImport.php 执行var_dump($_POST['batchArrayChild']) 之后,它说:

string(0) ""

您知道我可以将这个数组发送到执行脚本并获得某种响应的任何其他方式吗?

【问题讨论】:

  • 乍一看还不错,所以我怀疑发送是问题所在。也许您想转储一次网络流量以检查您是否真的发送了包含您期望的内容的 POST 请求。您得到的结果很可能是出乎意料的,因为远程脚本不理解您发送的数据的结构。显然,这不是我们可以说的。
  • 我在 txt 文档中记录了整个 $_POST 并且这个 - 'batchArrayChild' 字段仍然是空的。
  • 你是怎么做到的? “登录到 txt 文档”听起来很可疑……那么也许你想先检查一下 $data 是否真的包含有效的 JSON 数据?
  • 我没有测试过,但是如果 json_encode 确实包含任何受保护或私有属性,我会感到惊讶。看起来你的类中的所有内容都受到保护,所以我希望从 json_encode 获取空对象
  • @arkascha 是的,你是对的.. 'batchArrayChild' 看起来在发送之前是空的.. 你们知道如何将这个带有受保护值的对象数组发送到另一个页面吗?

标签: php arrays json curl


【解决方案1】:

我不喜欢在系统之间发送复杂的(内部)对象,所以我会创建一个具有公共属性的 DTO(数据传输对象)并使用该对象发送数据以避免服务之间的对象耦合。如果你想让它变得简单,DTO 甚至可以是 stdClass 类型。

如果您想让服务相互超级依赖,包括共享状态,您可以尝试在数据上使用serialize()

【讨论】:

    猜你喜欢
    • 2013-12-26
    • 1970-01-01
    • 1970-01-01
    • 2021-08-12
    • 1970-01-01
    • 1970-01-01
    • 2022-08-13
    • 2023-03-30
    • 2021-07-22
    相关资源
    最近更新 更多