需要使用需要用户名/密码才能访问的Web服务中的某些数据。
以下命令返回NULL
$service_url = 'https://example.com/2365139.json';
$curl = curl_init($service_url);
curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($curl, CURLOPT_USERPWD, "username:password");
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $curl_post_data);
$curl_response = curl_exec($curl);
$response = json_decode($curl_response);
curl_close($curl);
var_dump($response);
当我在浏览器中点击https://example.com/2365139.json时,会提示输入un / pw,当我输入它们时会显示JSON,所以数据就在那里,但我上面写的东西不起作用。
答案 0 :(得分:4)
原始代码工作正常,但由于资源是https,因此需要以下选项;
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
使用它的警告是“这基本上导致cURL盲目地接受任何服务器证书,而不对CA签署它的任何验证,以及该CA是否可信。如果你完全关心您传递给服务器或从服务器接收的数据,您需要正确启用此对等验证。“ - 取自http://unitstep.net/blog/2009/05/05/using-curl-in-php-to-access-https-ssltls-protected-sites/