简体   繁体   中英

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. 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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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