How to POST JSON data via PHP?
| From: | Rob Gould | Date: | Wed, 06 Jan 2010 23:22:09 +0000 |
| Subject: | How to POST JSON data via PHP? | ||
| Groups: | php.general | ||
| Request: | Send a blank email to php-general+get-300854@lists.php.net to get a copy of this message | ||
I've used curl before to post "field" data to a server, but in this case, I actaully need to post JSON data. However, when I run this PHP script, I get nothing back. If I do this curl manually in Terminal, it works.
I'm pretty sure the bug has something to do with CURLOPT_POSTFIELDS - - - I just don't know what the alternative is. Basically I need to post this data:
{siteUrl:\"http://testurl.com\",siteRss:\"http://feeds.news.mysite.com/synfeeds/collect/6/rss.xml\",userName:\"testuser\",email:\"testemail\",siteName:\"testsitename\",loginId:\"testloginid\}
to the url:
http://dpdev-ld03.myservercom:8181/linkmethod/signup?f=json&c=signupcallback
Any obvious mistakes below?
<?php
$ch = curl_init();
$timeout = 10; // set to zero for no timeout
$urltouse = "http://dpdev-ld03.myservercom:8181/linkmethod/signup?f=json&c=signupcallback";
$fields_string = '{siteUrl:\"http://testurl.com\",siteRss:\"http://feeds.news.mysite.com/synfeeds/collect/6/rss.xml\",userName:\"testuser\",email:\"testemail\",siteName:\"testsitename\",loginId:\"testloginid\}';
curl_setopt($ch,CURLOPT_URL,$urltouse);
curl_setopt($ch,CURLOPT_POST,count($fields));
curl_setopt($ch,CURLOPT_POSTFIELDS,$fields_string); <-- shouldn't be postfields, but some other curl option I'm guessing
$response = curl_exec($ch);
curl_close($ch);
echo $response;
?>