我正在使用php curl调用需要基本身份验证的外部api。我可以用邮递员打电话,也可以收到回复。但是,我自己似乎超时了。这就是我在php中的调用方式。
$form_details_url = "xxxx"; //url
$username = "xxx";
$password = "xxx";
echo "request " . $form_details_url . PHP_EOL;
$ch = curl_init();
if(!$ch){
die("could not initiate curl handle");
}
curl_setopt($ch, CURLOPT_URL, $form_details_url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, '1');
curl_setopt($ch, CURLOPT_VERBOSE, '1');
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($ch, CURLOPT_USERPWD, "$username:$password");
$response = curl_exec($ch);
$err = curl_error($ch);
curl_close($ch);
if ($err) {
echo "cURL Error #:" . $err;
} else {
$result = json_decode($response, true);
print_r($result);
}
die();
此代码段来自邮递员,确实给了我答复。
curl_setopt_array($curl, array(
CURLOPT_URL => "xxxxxxxx",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "GET",
CURLOPT_HTTPHEADER => array(
"Authorization: Basic ZGl0LWNmaS1wZHgtcmVtb3RlOmRpdC1jZmktcGR4LXJlbXNsZWVw",
"Content-Type: application/x-www-form-urlencoded",
"Postman-Token: xxxxxxx",
"cache-control: no-cache"
),
));
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err) {
echo "cURL Error #:" . $err;
} else {
echo $response;
}
任何建议,不胜感激。我的猜测是我正在处理关于curl请求中的auth错误的问题。谢谢。