因此,我使用了我发现的向Google QPX API发送帖子请求的唯一来源。我想将它保存在json_decoded PHP数组中,但由于某种原因,$result = curl_exec($ch);
行不起作用,json仍在屏幕上打印。
有什么我不理解cURL中发生的事情吗?谢谢!
$data = array ( "request" => array(
"passengers" => array(
adultCount => 1
),
"slice" => array(
array(
origin => "BOS",
destination => "LAX",
date => "2015-09-09"),
array(
origin => "LAX",
destination => "BOS",
date => "2015-09-10"),
),
solutions => "10"
),
);
$data_string = json_encode($data);
$ch = curl_init('https://www.googleapis.com/qpxExpress/v1/trips/search?key=MY-API-KEY');
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, false);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
$result = curl_exec($ch);
curl_close($ch);
答案 0 :(得分:1)
此:
curl_setopt($ch, CURLOPT_RETURNTRANSFER, false);
CURLOPT_RETURNTRANSFER
-TRUE
将转移作为返回值curl_exec()
的字符串返回,而不是直接输出。
如果要将结果保存在变量中,请将此选项设置为true
。