LeeTee
LeeTee

Reputation: 6601

PHP - headers not setting when using cURL

When using cURL to access a file, it is ignoring the set headers in the said file. When I access the same file directly through the browser without cURL, its works and the headers are set correctly. Anyone know why this is and how I get round this?

I am writing an API and need to set HTTP header responses in web service and not in file used to connect to the file via cURL. Hope that makes sense.

Many thanks in advance.

Upvotes: 3

Views: 6946

Answers (3)

Sreeraj
Sreeraj

Reputation: 2720

I thins will help you

//open connection
$ch = curl_init();


//set the url, number of POST vars, POST data
curl_setopt($ch,CURLOPT_URL,"URL");
curl_setopt($ch,CURLOPT_USERAGENT, "Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)");
curl_setopt($ch,CURLOPT_HTTPHEADER, array('newVar:newValue','Content-type: text/plain', 'Content-length: 100')); 
curl_setopt($ch,CURLOPT_RETURNTRANSFER, true); 

//execute post
ob_clean();
$result = curl_exec($ch);
echo $result;
curl_close($ch);

Upvotes: 0

vinay rajan
vinay rajan

Reputation: 361

As You are using CURL, try this as your useragent

curl_setopt($c_link, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 6.0; en-US; rv:1.9.2) Gecko/20100115 Firefox/3.6 (.NET CLR 3.5.30729)");

or

curl_setopt($c_link, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)");

You can also copy headers from firefox which might look like this

$header = "Accept: text/xml,application/xml,application/xhtml+xml,";
  $header = "text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5";
  $header = "Cache-Control: max-age=0";
  $header = "Connection: keep-alive";
  $header = "Keep-Alive: 300";
  $header = "Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7";
  $header = "Accept-Language: en-us,en;q=0.5";
  $header = "Pragma: ";

Upvotes: 2

Caniko
Caniko

Reputation: 872

curl_setopt($ch, CURLOPT_HEADER, <true or false>);

may help you.

Or if want to give a http header exactly,

curl_setopt($ch, CURLOPT_HTTPHEADER,array(<header parameters>));

would be a solution.

Upvotes: 1

Related Questions