简体   繁体   English

PHP curl()标头错误的请求

[英]PHP curl() Headers bad request

I am trying to figure out why pasing a custom header is resulting in a 400 BAD REQUEST from the server. 我试图弄清楚为什么粘贴自定义标头会导致服务器发出400错误请求。

    $headers = array(
                    'API KEY: asdf',
                    );
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:24.0) Gecko/20100101 Firefox/24.0" );
    curl_setopt($ch, CURLOPT_URL, 'http://url');
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_POST, 1 );
    curl_setopt($ch, CURLOPT_POSTFIELDS, 'stuff');
    curl_setopt($ch, CURLOPT_COOKIEFILE, './tmp/cookie.txt');
    curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);

    curl_setopt($ch, CURLOPT_PROXYPORT, '0000');
    curl_setopt($ch, CURLOPT_PROXYTYPE, 'HTTP');
    curl_setopt($ch, CURLOPT_PROXY, '0.0.0.0');

    $result = curl_exec($ch);
    curl_close($ch);

I thought that using CURLOPT_HTTPHEADER would add a custom header to the request, but I'm now wondering whether it's simply overriding everything else I set? 我以为使用CURLOPT_HTTPHEADER会向请求中添加自定义标头,但是我现在想知道它是否只是在覆盖我设置的其他所有内容?

There are more reasons a server will give a 400 response than just a header value. 服务器给出400响应的原因不仅仅是标头值。 Without more information about the endpoint it's difficult to say what's causing the 400 response. 没有关于端点的更多信息,很难说是什么导致了400响应。 With the exception of the extra "," in the headers array in the example the code looks okay. 在示例的headers数组中,除了多余的“,”外,代码看起来还不错。 cURL Options cURL选项

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM