webdevRefinery Forum: Converting header data into cURL - webdevRefinery Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Rate Topic: -----

User is offline Sole_Wolf 

  • Group: Members
  • Posts: 307
  • Joined: 24-March 10
  • Expertise:HTML,PHP,Java,SQL

Posted 10 July 2012 - 10:44 PM (#1)

Converting header data into cURL


I am having a bit of trouble trying to convert this header information into a php script for a project on my site and I was wondering whether anyone on WDR could give it a shot or point out anything that I am doing incorrectly.

Here is the captured header information:
POST /testing/validate HTTP/1.1
Host: test.guysthatcode.com
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.7; rv:12.0) Gecko/20100101 Firefox/12.0
Accept: */*
Accept-Language: en-us,en;q=0.5
Accept-Encoding: gzip, deflate
Connection: keep-alive
Content-Type: application/x-www-form-urlencoded; charset=UTF-8
X-Requested-With: XMLHttpRequest
Referer: http://test.guysthatcode.com/testing
Content-Length: 40
Cookie: testcookie=1;
Pragma: no-cache
Cache-Control: no-cache
testToken=sd8f97dgh3jk43oi&code=password


And here is my code attempt right now.

$ch = curl_init("http://test.guysthatcode.com/testing/validate");
    $data ="testToken=sd8f97dgh3jk43oi&code=password");
    curl_setopt($ch, CURLOPT_HTTPHEADER, array(
    "Host" => "test.guysthatcode.com",
    "User-Agent" => "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.7; rv:12.0) Gecko/20100101 Firefox/12.0",
    "Accept" => "*/*",
    "Accept-Language" => "en-us,en;q=0.5",
    "Accept-Encoding" => "gzip, deflate",
    "Connection" => "keep-alive",
    "Content-Type" => "application/x-www-form-urlencoded; charset=UTF-8",
    "Content-Length" => "40",
    "X-Requested-With" => "XMLHttpRequest",
    "Referer" => "http://test.guysthatcode.com/testing"
    ));
    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");                                                                     
    curl_setopt($ch, CURLOPT_POSTFIELDS, $data);                                                                  
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);  
    curl_setopt($ch, CURLOPT_COOKIEJAR, 'cookie.txt');
    curl_setopt($ch, CURLOPT_COOKIEFILE, 'cookie.txt');
    $result = curl_exec($ch);
    echo $result;


The code above returns nothing even though it should have returned a "success!" message. Also, the testToken thing. If the testToken doesn't equal what I have right now, the page will redirect to an error page, but if the testToken does equal what I have, it will display the success message.

Any ideas?

Thanks.
--Sole_Wolf
1


User is offline Daniel15 

  • dan.cx
  • Group: Moderators
  • Posts: 3435
  • Joined: 17-April 10
  • LocationMelbourne, Australia
  • Expertise:HTML,CSS,PHP,Java,Javascript,Node.js,SQL

Posted 11 July 2012 - 12:34 AM (#2)

Try getting rid of the
CURLOPT_CUSTOMREQUEST
option - I've never used it and POSTs work fine without it.

Also try use an array for $data:
$data = array('testToken' => 'sd8f97dgh3jk43oi', 'code' => 'password');


Try sniffing the traffic and seeing what actually differs between the browser's request and yours.
Daniel15! :D
Posted Image

Repeat after me: jQuery is not JavaScript. It is not the answer to every JavaScript-related question. When you have to write some JavaScript, do not instantly react with "Oh, I'll do that with jQuery!"

Spoiler
0


User is offline Nico 

  • Group: Members
  • Posts: 262
  • Joined: 09-March 10
  • LocationSpain
  • Expertise:HTML,CSS,PHP,Javascript,SQL

Posted 11 July 2012 - 02:43 AM (#3)

The CURLOPT_HTTPHEADER array looks a little wrong. It should be:

curl_setopt($ch, CURLOPT_HTTPHEADER, array(
    "Host: test.guysthatcode.com",
    "User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.7; rv:12.0) Gecko/20100101 Firefox/12.0",
    "Accept: */*",
    // ...
));


And try adding this line:

curl_setopt($ch, CURLOPT_POST, true);


Where does the token come from? You you have to initiate a session on their server first?
0


Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users


Enter your sign in name and password


Sign in options
  Or sign in with these services