PHP Curl不存储json_encoded回显数据 [英] PHP Curl not storing json_encoded echoed data
问题描述
有人可以存储来自Curl脚本的回显结果。
Does anyone no if it is possible to store echoed results from a Curl script.
脚本示例已提交到:
\\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);
如果i var_dump($ result)得到一个bool(true),所以我知道脚本
If i var_dump($result) i get a bool(true) so i know that the script has executed correctly and the output shows on screen however i don't seem to be able to store the information into a variable to process.
提前感谢您的建议
推荐答案
如 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
打开它
In your case you turn CURLOPT_RETURNTRANSFER
off.
Turn it on by
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
并再次检查$结果。
注意:如果数据包含&
字符,那么使用foreach循环和rtrim()构建请求可能会损坏它。
Note: building a request using foreach loop and rtrim() you may damage it if your data contains &
character.
只需使用 http_build_query()与您的 $ champ
:
curl_setopt($ch,CURLOPT_POSTFIELDS, http_build_query($champ));
这篇关于PHP Curl不存储json_encoded回显数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!