使用PHP中的Curl检索数据。
我使用下面的代码将数据发布到它可行的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
METHOD,如下面的这一行
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;
}
答案 0 :(得分:0)
在GET
方法上,我看到您使用帖子字段传递了info_id
。
CURLOPT_POSTFIELDS => "$data",
如果使用GET
方法,通常数据应在URL参数中传递,而不是在帖子字段中传递。
如果你有APIDOC,那就更好了,所以我们知道访问GET
所需的参数。