Re: PHP CURL JSON POST Firebug

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 





On 04/09/2012 19:14, ioannes@xxxxxxxxxxxxxx wrote:


On 04/09/2012 18:41, Matijn Woudt wrote:
On Tue, Sep 4, 2012 at 7:35 PM, ioannes@xxxxxxxxxxxxxx
<ioannes@xxxxxxxxxxxxxx> wrote:
I am hoping someone can spot what is missing here.  I am getting null
result
from curl-ing a page with json post variables.

I try this url in my Firefox browser -

http://www.targetsite.com/search.php#somevar.someothervar

(#somevar.someothervar are irrelevant, I think, as I get the curl
variables
from Firebug below.)

In Firebug, this shows:

POST http://www.targetsite.com/ajax/search.xml

In Firebug, below this link are tabs for: Headers, Post, Response,
XML and
Cookies.  Post tab shows like:

JSON
VAR1           1
VAR2           "2012-09-12"
VAR3           null
CACHED         []
OPTIONS        null

To prove there is output, the Firebug Response tab shows:

<?xml version="1.0" encoding="utf-8"?>
<JSON> various JSON encoded stuff </JSON>

The above is what I am trying to curl.

My php code:

   $ch = curl_init();
   curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
   curl_setopt($ch, CURLOPT_POST, 1);

   //target page from Firebug above:
   curl_setopt($ch, CURLOPT_URL,
"http://www.targetsite.com/ajax/search.xml";);

   //I was not sure how to represent CACHED [], so set it to null
try "CACHED"=>array()
   $data = array(
     "VAR1" => 1,
     "VAR2" => "2012-09-12",
     "VAR3" => null,
     "CACHED"=>null,
     "OPTIONS"=>null,
   );
   curl_setopt($ch, CURLOPT_POSTFIELDS, $data);

   //make the request
   $result = curl_exec($ch);

   //this returns null

Any ideas where to go with this?  Maybe I need to include the
Cookies? I use
the above php and curl functions normally so it's all installed on the
server.

John

It might be that the site is using sessions/cookies. Have a look at
the header data with firebug.
Not sure if that's the problem, to find out what's really going on, call
echo curl_error($ch);
after curl_exec to find out what went wrong exactly.
If you still don't know how to proceed, paste the result of the
curl_error call in your reply.

- Matijn

I added the cookies to the post array.  I changed php array to
"CACHED"=>array() for the JSON "CACHED":[], and corrected php's null to
NULL.  It is not returning any error.  The browser was showing 'resource
not present' before I added the cookies to the post array, now it just
returns null $result.  Looks like I am transcribing something incorrectly.

John


I eventually sorted this out.  Solution involved:

POST params needed to be json_encoded
$params=json_encode(array(
	"name" => "value"
));

Thanks to http://www.lornajane.net/posts/2011/posting-json-data-with-php-curl

curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, $post);

Also, included headers as array and set application type as json:
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
    'Content-Type: application/json',
  	'Content-Length: ' . strlen($post))
);

Set encoding to auto-detect:
curl_setopt( $ch, CURLOPT_ENCODING, "");


John

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux