JFourie
JFourie

Reputation: 35

Doing a curl request

I am trying to do a curl request on a site and I can't seem to get it working.

Here is what I have

<?php
 $mynumber = $_GET["mynumber"];
 echo $mynumber;
 $url = 'thewebsite/c/number='.$mynumber; // like this?
 $curl_header = array('X-Requested-With: XMLHttpRequest');
 $ch = curl_init($url);
 curl_setopt($ch, CURLOPT_HTTPHEADER, $curl_header);
 curl_setopt($ch, CURLOPT_POSTFIELDS, "number=$mynumber"); //??? --data     "number=12345678" ?
 curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
 curl_setopt($ch, CURLOPT_COOKIE, $cookie);
 curl_setopt($ch, CURLOPT_COOKIESESSION, TRUE);
 curl_setopt($ch, CURLOPT_VERBOSE, true);
 $result = curl_exec($ch);
 echo $result;
 curl_close($ch)
 ?>

And here is the curl request:

"Content-Type: application/x-www-form-urlencoded; charset=UTF-8" -H "Accept: application/json, text/javascript, */*; q=0.01" -H "Referer: thewebsite" -H "X-Requested-With: XMLHttpRequest" -H "Connection: keep-alive" --data "number=12345678" --compressed  POST /thewebsite/c HTTP/1.1

I am not sure how to pass "--data "number=12345678"

Upvotes: 1

Views: 176

Answers (1)

Bjorn
Bjorn

Reputation: 5362

It looks like you're passing the --data parameter correctly. But you forgot to set the Content-Type, Accept, Referer and Connection headers. You can add those the way you set the X-Requested-With header (in $curl_header):

<?php

$cookie = '';
$mynumber = $_GET['mynumber'];

$ch = curl_init('http://example.com/c/');

curl_setopt($ch, CURLOPT_HTTPHEADER, array(
    'X-Requested-With: XMLHttpRequest',
    'Content-Type: application/x-www-form-urlencoded; charset=UTF-8',
    'Accept: application/json, text/javascript, */*; q=0.01',
    'Referer: http://example.com/c/',
    'Connection: keep-alive'
));
curl_setopt($ch, CURLOPT_POSTFIELDS, "number=$mynumber");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_COOKIE, $cookie);
curl_setopt($ch, CURLOPT_COOKIESESSION, TRUE);
curl_setopt($ch, CURLOPT_VERBOSE, true);

$result = curl_exec($ch);
var_dump($result);

Upvotes: 3

Related Questions