user989557
user989557

Reputation: 929

PHP Curl post not working, nothing in post

I have two sites on the same server:

api.mysite.com john.mysite.com

On my API site, I have a service that accepts a POSTed json array. In my john.mysite.com site I'm calling the service and posting it using:

 $info['id'] ="oo_".uniqid();
 $info['version'] ="5";

 $url = "http://api.mysite.com/services/addclient";
 $posted_fields = json_encode($info);

   $ch = curl_init($url);
    curl_setopt($ch, CURLOPT_POST, TRUE);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $postfields);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
   $result = curl_exec($ch);
   echo $result;

When this is called, I'm not getting anything posted to the webservice. I've gone into that code and dumped out $_POST and it's empty. Why isn't the curl sending the data via POST?

Thanks for any help!

Upvotes: 1

Views: 1955

Answers (2)

Levi Morrison
Levi Morrison

Reputation: 19552

One problem is that you are json_encodeing the data instead of form encoding it.

Another is that $info might not be initialized.

Another is that you typo'd $postfields on line 9.

Try:

$info['id'] ="oo_".uniqid();
$info['version'] ="5";

$url = "http://api.mysite.com/services/addclient";

$ch = curl_init($url);
curl_setopt($ch, CURLOPT_POST, TRUE);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($info));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
$result = curl_exec($ch);
echo $result;

Upvotes: 6

su-
su-

Reputation: 3166

Typo in line 9

$postfields

should be

$posted_fields

Upvotes: 1

Related Questions