0

如果可以存储来自 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) 所以我知道脚本已正确执行并且输出显示在屏幕上但是我似乎无法将信息存储到要处理的变量中。

先感谢您

4

1 回答 1

0

正如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));
于 2013-07-12T16:13:44.897 回答