0

How to make the headers same using PHP? I want to set the headers same while sending request from cURL. Basically, i am able to access a url by typing in the browser. But while using PHP cURL, am not able to access the page. So, maybe making the headers same can solve this.

Headers while sending actual request:

Request Line:  GET /jlp.cgi?Flag=Html_Data&LogType=0&Dummy=1341550112739 HTTP/1.1

Host: <abc>

Proxy-Connection:  Keep Alive

Cookie: iR=548729

There is no such field as Connection and Referer in the original request. All the other fields (User-Agent, Accept, Accept-Language, Accept-Encoding) are same in both requests.

Headers when sending from cURL:

Request Line: GET /curlexp2.php?address=http%3A%2F%2F10.128.58.200%2Fjlp.cgi%3FFlag%3DHtml_Data%26LogType%3D0%26Dummy%3D1341550112739&submit=Log+In HTTP/1.1

Host: localhost

Connection: Keep Alive

Referer: http://localhost/exp2.php

There is no such field as Proxy-Connection and Cookie in this cURL request.

Thanks in advance.

3
  • To make headers same as WHAT? The question is not clear. Commented Jul 6, 2012 at 5:22
  • Headers sent while using cURL should be same as those when the actual request i sent on the browser when i type in the URL. Commented Jul 6, 2012 at 5:26
  • not familiar enough with curl, but could you do: "require_once('head.php');" and then write the head.php document to be the same? Commented Jul 6, 2012 at 5:35

1 Answer 1

1

use php's curl_setopt function and set the required header contents as an array.

curl_setopt(
 cur_resource,
 CURLOPT_HTTPHEADER,
 array(
  "Content-Type: application/x-www-form-urlencoded",
  "Content-Length: " . strlen($data),
  "Referrer": "...",
 )
);
Sign up to request clarification or add additional context in comments.

4 Comments

@webbandit When i do curl_setopt($ch, CURLOPT_HEADER, true); I get this information: HTTP/1.1 200 OK Date: SUN, 06 JUL 2012 04:42:42 GMT Server: CANON HTTP Server Ver2.21 Transfer-Encoding: chunked So now what do i need to do..just maybe write curl_setopt($ch, CURLOPT_HTTPHEADER,array("Date: SUN, 06 JUL 2012 04:42:40 GMT", 'Server: CANON HTTP Server Ver2.21','Transfer-Encoding: chunked ')); ?? I tried this but still not working. Wht am i doing wrong?
You have to set each header as separate key-pair in array like array('Date' => 'SUN, 06 JUL 2012 04:42:40 GMT', 'Server' => 'CANON HTTP Server Ver2.21'); ... But most of headers you are trying to redefine are self-generated, you shouldn't replace them.
your arguments should be passed as an array of hash as pointed out by @webbandit
I did as said by @webbandit.('Date' => 'SUN, 06 JUL 2012 04:42:40 GMT', 'Server' => 'CANON HTTP Server Ver2.21'); But not able to get any output. I have another doubt. What values should I put in the HTTPHEADER array? Is it those returned by CURLOPT_HEADER, or should i put the headers which i got when sending the actual request(which i got from HTTPFox) ??

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.