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

时间:2016-09-20 05:59:59

标签: php arrays json curl

我有一个如下所示的数组> lm1 <- lm(Fertility ~ ., data = swiss) > add1(lm1, ~ I(Education^2) + .^2, test='F') Single term additions Model: Fertility ~ Agriculture + Examination + Education + Catholic + Infant.Mortality Df Sum of Sq RSS AIC F value Pr(>F) <none> 2105.0429 190.69135 I(Education^2) 1 11.818686 2093.2242 192.42672 0.22585 0.63721 Agriculture:Examination 1 10.667353 2094.3756 192.45257 0.20373 0.65416 Agriculture:Education 1 1.826563 2103.2164 192.65055 0.03474 0.85309 Agriculture:Catholic 1 75.047836 2029.9951 190.98513 1.47878 0.23109 Agriculture:Infant.Mortality 1 4.438027 2100.6049 192.59215 0.08451 0.77278 Examination:Education 1 48.693777 2056.3492 191.59137 0.94719 0.33628 Examination:Catholic 1 40.757983 2064.2850 191.77240 0.78977 0.37948 Examination:Infant.Mortality 1 65.856710 2039.1862 191.19745 1.29182 0.26248 Education:Catholic 1 278.189298 1826.8536 186.02953 6.09111 0.01796 * Education:Infant.Mortality 1 92.950398 2012.0925 190.56880 1.84784 0.18165 Catholic:Infant.Mortality 1 2.358769 2102.6842 192.63865 0.04487 0.83332 --- Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

$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" } } 的页面。这是我尝试过的:

使用JSON

parallelImport.php

您可以看到我$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

使用http_build_query

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

在parallelImport.php上做$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); 后,它说:

var_dump($_POST['batchArrayChild'])

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

1 个答案:

答案 0 :(得分:2)

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

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