我想向URL发出cURL请求以及以下标题:
'Content-Type:application / json', '授权':'基本XXXXXXXXXX'
我有以下代码:
<?php
$post_url = "https://api.interlinkexpress.com/user/?action=login";
$curl = curl_init($post_url);
$headers = array(
'Content-Type: application/json',
'Authorization': 'Basic XXXXXXXXX'
);
//curl_setopt($curl, CURLOPT_USERPWD, "username":"Password");
curl_setopt($curl, CURLOPT_URL, $post_url);
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
curl_setopt($curl, CURLOPT_POST, true);
//curl_setopt($curl, CURLOPT_POSTFIELDS,json_encode($post_data) );
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
$post_response = curl_exec($curl);
?>
它将结果显示为:
客户发送了错误请求
这是$ header array
下的'Authorization ...'行感谢任何建议/帮助。
答案 0 :(得分:6)
问题在于header.It需要修复.authorization将整个字符串如下:
$headers = array(
'Content-Type: application/json',
'Authorization: Basic XXXXXXXXX'
);
让我给你一个卷曲的完整例子:
function CurlSendPostRequest($url,$request)
{
$authentication = base64_encode("username:password");
$ch = curl_init($url);
$options = array(
CURLOPT_RETURNTRANSFER => true, // return web page
CURLOPT_HEADER => false, // don't return headers
CURLOPT_FOLLOWLOCATION => false, // follow redirects
// CURLOPT_ENCODING => "utf-8", // handle all encodings
CURLOPT_AUTOREFERER => true, // set referer on redirect
CURLOPT_CONNECTTIMEOUT => 20, // timeout on connect
CURLOPT_TIMEOUT => 20, // timeout on response
CURLOPT_POST => 1, // i am sending post data
CURLOPT_POSTFIELDS => $request, // this are my post vars
CURLOPT_SSL_VERIFYHOST => 0, // don't verify ssl
CURLOPT_SSL_VERIFYPEER => false, //
CURLOPT_VERBOSE => 1,
CURLOPT_HTTPHEADER => array(
"Authorization: Basic $authentication",
"Content-Type: application/json"
)
);
curl_setopt_array($ch,$options);
$data = curl_exec($ch);
$curl_errno = curl_errno($ch);
$curl_error = curl_error($ch);
//echo $curl_errno;
//echo $curl_error;
curl_close($ch);
return $data;
}
答案 1 :(得分:0)
500错误通常表明错误发生在服务器端。您可以尝试模拟您的请求(我个人建议使用Advanced ReST Client,这是一个谷歌浏览器应用),以验证是否是这种情况。