简体   繁体   English

使用php curl进行身份验证的基本Web服务

[英]Basic web service with authentication using php curl

Need to consume some data from a web service that requires a username/password for access. 需要使用需要用户名/密码才能访问的Web服务中的某些数据。

The following returns NULL 以下返回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);

When I hit https://example.com/2365139.json in a browser, it prompts for un/pw and when I enter them it displays the JSON, so the data is there but something I have written above isn't working. 当我在浏览器中点击https://example.com/2365139.json时,它会提示输入un / pw,当我输入它们时会显示JSON,所以数据就在那里,但我上面写的东西不起作用。

Original code works fine, but because the resource is https it requires the following option; 原始代码工作正常,但由于资源是https,它需要以下选项;

curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);

The caveat to using this is "This basically causes cURL to blindly accept any server certificate, without doing any verification as to which CA signed it, and whether or not that CA is trusted. If you're at all concerned about the data you're passing to or receiving from the server, you'll want to enable this peer verification properly." 使用它的警告是“这基本上导致cURL盲目地接受任何服务器证书,而不对CA签署它的任何验证,以及该CA是否是可信的。如果您完全关心数据,那么'重新传递到服务器或从服务器接收,您将需要正确启用此对等验证。“ - Taken from http://unitstep.net/blog/2009/05/05/using-curl-in-php-to-access-https-ssltls-protected-sites/ - 摘自http://unitstep.net/blog/2009/05/05/using-curl-in-php-to-access-https-ssltls-protected-sites/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM