Php 基本http身份验证不起作用的curl
我正在尝试使用curl连接到WS。 我的代码:Php 基本http身份验证不起作用的curl,php,curl,Php,Curl,我正在尝试使用curl连接到WS。 我的代码: $url ="https://example?param=ee¶m2=rr"; $headers = array( 'Content-Type:application/json', 'Authorization: Basic '. base64_encode("username:password") // <--- ); $curl = curl_init(); curl_setopt($curl, CURLOPT_URL,$
$url ="https://example?param=ee¶m2=rr";
$headers = array(
'Content-Type:application/json',
'Authorization: Basic '. base64_encode("username:password") // <---
);
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL,$url);
curl_setopt($curl, CURLOPT_HEADER,true);
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
//curl_setopt($curl, CURLOPT_USERPWD, 'username:passwrd');
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
$curl_response = curl_exec($curl);
我无法找出代码中的错误。是否缺少参数?尝试添加:
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
默认情况下,当您使用HTTPS协议时,CURL会检查服务器上的SSL sertificate,如果它无效,则会给出一个错误。上面的一行关闭了此功能。WS指的是网站,而不是.WS网站吗?您不使用CURLOPT_USERPWD的任何原因,不是自己组装授权标头?它是在标头数组上设置的。你可以编辑你的答案并解释你更改了什么以及你的代码工作的原因吗?thx但我使用的是get方法,所以我不需要
curl\u setopt($ch,CURLOPT\u CUSTOMREQUEST,“POST”)代码>虽然这可能是正确的,但值得注意的是,关闭cURL验证是一种非常糟糕的做法,因为TLS安全站点如果无法进行身份验证,也可能不安全。(不过,修复此验证可能是另一个问题)。
$url ="https://example?";
$headers = array(
'Content-Type:application/json',
'Authorization: Basic '. base64_encode("username:password")
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS,"param=ee¶m2=rr");
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
// execute the request
$output = curl_exec($ch);
$url ="https://example?";
$headers = array(
'Content-Type:application/json',
'Authorization: Basic '. base64_encode("username:password")
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS,"param=ee¶m2=rr");
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
// execute the request
$output = curl_exec($ch);