php - 如何在 PHP 中将复杂对象数组发送到另一个页面?

标签 php arrays json curl

我有一个数组$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_encode 编辑了 $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) ""

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

最佳答案

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

如果您想让服务彼此高度依赖(包括共享状态),您可以尝试对数据使用 serialize()

关于php - 如何在 PHP 中将复杂对象数组发送到另一个页面?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39586720/

相关文章:

javascript - 如何从维基百科的信息框获取数据?

php - 使用 PHP 计算两个日期之间的天数(以月为单位)

javascript - 在 TypeScript 中将值与枚举进行比较并不明显

php - 来自数据库的 Laravel 显示名称

php - 构建具有多个where条件的动态mysql查询

Javascript 通过引用或值分配数组元素

c - 使用 scanf 的二维数组中的段错误

php - 令人困惑的 foreach 循环显示数组的值

java - 如何解决GETJson口是心非异常?

json - 遍历 XML 节点,然后检查是否有匹配的 json 值,然后选择该值