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


How to send array of complex objects to another page in PHP?

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

你知道有什么其他的方法,我可以发送这个数组到执行脚本,并得到一些响应?

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

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