发送带有curl的请求时出现无效的报头错误

max*_*max 2 php curl

这是我的代码

echo 22;
$url = 'http://www.10bet.com/pagemethods.aspx/GetLeaguesContent';

$fields = array(    
        'BranchID' => urlencode('1') , 
        'LeaguesCollection' => urlencode('10098') , 
                );

$fields_string  = '';
foreach($fields as $key=>$value) { $fields_string .= $key.'='.$value.'&'; }
rtrim($fields_string, '&');



$ch = curl_init();
curl_setopt($ch,CURLOPT_URL, $url);
curl_setopt($ch,CURLOPT_POST, count($fields));
curl_setopt($ch,CURLOPT_POSTFIELDS, $fields_string);
curl_setopt($ch, CURLINFO_HEADER_OUT, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type : application/json; charset=utf-8'));
$result = curl_exec($ch);
curl_close($ch);
var_dump( $result );
Run Code Online (Sandbox Code Playgroud)

我不断

Bad Request - Invalid Header

HTTP Error 400. The request has an invalid header name.
Run Code Online (Sandbox Code Playgroud)

Zom*_*Spy 5

您惊讶地得到了错误

Bad Request - Invalid Header

HTTP Error 400. The request has an invalid header name.
Run Code Online (Sandbox Code Playgroud)

因为您使用了无效的标题名称:)

你用

Content-Type : application/json; charset=utf-8
Run Code Online (Sandbox Code Playgroud)

注意标题名称中的空格

正确的用法是:

Content-Type: application/json; charset=utf-8
Run Code Online (Sandbox Code Playgroud)