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

如何在PHP中将复杂对象数组发送到另一个页面?,php,arrays,json,curl,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"

我有一个数组
$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);
如您所见,I
json\u encode
ed
$batchRequest
并通过
cURL
发送,以下是它的输出:

string(16) "[[],{},{},{},{}]"
使用http\u build\u查询

$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\u dump($\u POST['batchArrayChild'])
之后,它会显示:

string(0) ""

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

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


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

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