php - 使用 curl 发送请求时出现无效 header 错误

标签 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 );

我不断得到
Bad Request - Invalid Header

HTTP Error 400. The request has an invalid header name.

最佳答案

你出人意料地收到错误

Bad Request - Invalid Header

HTTP Error 400. The request has an invalid header name.

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

你用
Content-Type : application/json; charset=utf-8

注意 空间在标题名称中

正确的用法是:
Content-Type: application/json; charset=utf-8

关于php - 使用 curl 发送请求时出现无效 header 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20396639/

相关文章:

php - 对象的管理器类

php - PDO::last Insert Id 总是返回 0 与 MYSQL 和 prepare 语句一起使用

php - php v.5.4.6 中的 php.ini 在哪里?

linux - Solr服务器安装问题

powershell - Rest API调用可与curl配合使用,但不适用于PowerShell

javascript - 使用 mysql 查询从单列返回值

php - 在 PHP 中同时进行运算符和赋值?

php - curl 错误: Operation timed out

c++ - 如何正确删除指向回调函数的指针。

linux - Bash 输出消失