0
使用PHP中的Curl檢索數據。使用curl在php中檢索數據
我用下面的代碼發佈數據API它的工作原理:
$data_string = array("info_id" => "JPT00vZmVmMz", "info" => "my first info");
$data = json_encode($data_string);
//$data = $data_string;
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => "mysite.com/info",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_POSTFIELDS => "$data",
CURLOPT_HTTPHEADER => array(
"accept: application/json",
"authorization: Bearer mybear_id",
"content-type: application/json; charset=utf-8"
),
));
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err) {
echo "cURL Error #:" . $err;
} else {
echo $response;
}
現在我想/檢索信息回用info_id。爲此,我用POST
代替GET
方法,如下面這行代碼
CURLOPT_CUSTOMREQUEST => "POST",
但它沒有工作,因此下面的代碼用於檢索。看來info_id
未提交以啓用數據檢索。
有人可以幫我解決這個問題嗎?由於
$data_string = array("info_id" => "JPT00vZmVmMz");
$data = json_encode($data_string);
//$data = $data_string;
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => "mysite.com/info",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "GET",
CURLOPT_POSTFIELDS => "$data",
CURLOPT_HTTPHEADER => array(
"accept: application/json",
"authorization: Bearer mybear_id",
"content-type: application/json; charset=utf-8"
),
));
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err) {
echo "cURL Error #:" . $err;
} else {
echo $response;
}
它不工作?你是否收到任何錯誤消息或端點是否返回空響應?你需要更具體。信息端點是否接受POST或GET請求? – Cyclonecode
第一個代碼包含在數組中傳遞的info_id和Info(要發送的消息)。我使用Post方法併發布。在第二個代碼中,我嘗試通過get方法使用info_id檢索信息,但是沒有找到錯誤「info_id」。似乎「info_id」未提交以啓用消息回覆 – akwaeze