浏览器直接访问接口时会弹出账号密码框
当用程序调用时需要加入
curl_setopt($ch, CURLOPT_USERPWD, "$username:$password");链接要使用双引号,包含的双引号要用转译,不能用单引号代替。
代码如下
$ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_POST, true); curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_USERPWD, "$username:$password"); $file_contents = curl_exec($ch); $fileinfo = curl_getinfo($ch); curl_close($ch); if($fileinfo['http_code'] == 200){ // echo "111"; $data = json_decode($file_contents, true); print_r($data); }else{ echo "POST FAIL"; }
另外
发现一篇不错的文章分享一下
http://blog.csdn.net/fdipzone/article/details/44475801#0-tsina-1-13155-397232819ff9a47a7b7e80a40613cfe1