如果可以存储来自Curl脚本的回显结果,是否有人可以做?
脚本示例已提交至:
\\some code to generate images using imagick and the post variables
$array = array(1,2,3,4,5,6,7,8,9);
$result = json_encode($array);
echo $result;
卷曲的示例:
$id = 1;
$champ = array("product" => "1","id"=> $id,"occasion"=> $o,"field1" => "1991","field2" => "test","field3" =>"test1","field4" => "test2","field5" =>"test3","field6" =>"test4","field7" =>"test5","field8" =>"test6","test7");
foreach($champ as $key => $data){
$field_string .= $key."=".$data."&";
}
rtrim($field_string, "&");
$url = "http://www.test.com/website/script.php";
$ch = curl_init();
curl_setopt($ch,CURLOPT_URL,$url);
curl_setopt($ch,CURLOPT_POST, count($champ));
curl_setopt($ch,CURLOPT_POSTFIELDS, $field_string);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, FALSE);
$result = curl_exec($ch);
$array = json_decode($result);
curl_close($ch);
var_dump($array);
如果我var_dump($ result)我得到了bool(true),那么我知道脚本已正确执行,并且输出在屏幕上显示,但是我似乎无法将信息存储到要处理的变量中。
先感谢您
正如curl_exec文档所说:
curl_exec() returns TRUE on success or FALSE on failure.
However, if the CURLOPT_RETURNTRANSFER option is set,
it will return the result on success, FALSE on failure.
在您的情况下,请关闭CURLOPT_RETURNTRANSFER
电源。通过打开它
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
然后再次检查$ result。
注意:使用foreach循环和rtrim()构建请求,如果您的数据包含&
字符,则可能会损坏该请求。
只需将http_build_query()与您的一起使用$champ
:
curl_setopt($ch,CURLOPT_POSTFIELDS, http_build_query($champ));
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句