如果可以存储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)所以我知道脚本已正确执行并且输出显示在屏幕上但是我似乎无法将信息存储到变量中以进行处理
提前谢谢
答案 0 :(得分:0)
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));