我目前有一個返回JSON的API腳本。它一直在努力,直到我試圖在它之前添加一個curl php POST
腳本。 curl腳本正在處理它自己,並且它也在API腳本中工作。但是JSON代碼沒有被返回。CURL PHP POST干擾JSON
下面的方法是否存在根本性錯誤?
在此先感謝。
編輯:curl
腳本100%自行工作。 說的腳本也在裏面工作,只是JSON
不返回。
$name = "foo";
$age = "bar";
//set POST variables
$url = 'https://www.example.com';
$fields = array(
'name' => urlencode($name),
'age' => urlencode($age)
);
//url-ify the data for the POST
foreach($fields as $key=>$value) { $fields_string .= $key.'='.$value.'&'; }
rtrim($fields_string, '&');
//open connection
$ch = curl_init();
//set the url, number of POST vars, POST data
curl_setopt($ch,CURLOPT_URL, $url);
curl_setopt($ch,CURLOPT_POST, count($fields));
curl_setopt($ch,CURLOPT_POSTFIELDS, $fields_string);
//execute post
$result = curl_exec($ch);
//close connection
curl_close($ch);
return json_encode(
array(
"status" => 1,
"message" => "Success!",
"request" => 10
)
);
_「但是沒有被返回的JSON代碼。 「_ - 然後啓用錯誤報告,因爲當沒有調用'return'時,您的代碼在此之前明顯失敗。 – CodeCaster
也許你想「回聲」JSON呢? –
它在foreach點失敗,但curl腳本起作用。這些值已成功發佈 –