简体   繁体   English

Php:标头不起作用的卷曲

[英]Php : Curl With Headers Not Working

I am trying to set an headers in curl.But My curl is not working if i use headers. 我正在尝试在curl中设置标题。但是如果我使用标题,我的卷曲将不起作用。 Below is my code. 下面是我的代码。

   $ch = curl_init();
    $headers = array("Content-Type: multipart/form-data; boundary=---------------------------643932844140921187526840362", "Content-Length: 1927");
    curl_setopt($ch,CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
    curl_setopt($ch, CURLOPT_REFERER, 'http://domain.com/submit/');
    curl_setopt($ch, CURLOPT_RETURNTRANSFER,true);
    curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US) AppleWebKit/525.13 (KHTML, like Gecko) Chrome/0.A.B.C Safari/525.13");
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
    curl_setopt($ch,CURLOPT_COOKIEJAR, "cookie.txt");
    curl_setopt($ch,CURLOPT_COOKIEFILE, "cookie.txt");
    curl_setopt($ch,CURLOPT_POST, count($fields));
    curl_setopt($ch,CURLOPT_POSTFIELDS, $fields_string);
    $result = curl_exec($ch); 
    $last = curl_getinfo($ch, CURLINFO_EFFECTIVE_URL);
    return array($result,$last);
    curl_close($ch);

But if I remove below code, code works fine. 但是,如果我删除下面的代码,代码可以正常工作。

$headers = array("Content-Type: multipart/form-data; boundary=---------------------------643932844140921187526840362", "Content-Length: 1927");
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);

Is this the correct way to set an header? 这是设置标题的正确方法吗? Or am I wrong? 还是我错了?

Try replacing: 尝试更换:

"Content-Length: 1927"

with

"Content-Length: " . strlen($fields_string)

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

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