3

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.

1
  • This is quite a big project with a lot of code. I basically have a controller.php file for each resource in the API. In the controller.php, depending on what the user sends as a request to the API via cURL, will be various HTTP responses. eg. header('HTTP/1.1 200 OK' ); or header('HTTP/1.1 401 Unauthorized' ); However, these are ignored when accessing the controller.php via cURL. If I access the controller.php going direct to it using the browser, it works. Commented Oct 19, 2011 at 11:32

3 Answers 3

2

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: ";
Sign up to request clarification or add additional context in comments.

1 Comment

again, I need to set the headers away from the cURL settings. The users of the API will access a resource using cURL and my web service needs to be able to set different HTTP responses. I don;t understand how the headers from firefox can help, please expain. thanks
1

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.

1 Comment

I need to be able to set the header parametered AFTER the user makes the cURL connection, if that makes sense. Therefore this woulode not work. Or am I misunderstanding?
0

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

2 Comments

I assume you are posting this as an example of passing though the header info using CURLOPT_HTTPHEADER? If so, the problem is, the users of the API will be setting up the cURL themselves and my API needs to change the header HTTP responses depending on the users requests.
I dont understand the solution you have provided, which bit of this solves my issue? Can you explain how this would solve my problem? Much appreciated as I am a total loss with this.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.