Reputation: 1668
I'm Trying to send POST request to an api to create user using PHP cURL. Here is the code sample
<?php
$email="[email protected]";
$name = "jas";
$data = array(
"user" => array("email"=>$email,"name"=>$name)
);
//encoding to json format
$jsondata= json_encode($data);
$credentials = "username:pass";
$header[] = "Content-Type: application/json";
$header[] = "Authorization: Basic " . base64_encode($credentials);
$connection = curl_init();
curl_setopt($connection, CURLOPT_RETURNTRANSFER, true);
curl_setopt($connection, CURLOPT_HTTPHEADER, $header);
curl_setopt($connection, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($connection, CURLOPT_HEADER, false);
//POSTS
curl_setopt($connection, CURLOPT_POST, 1);
curl_setopt($connection, CURLOPT_POSTFIELDS, $jsondata);
curl_setopt($connection, CURLOPT_VERBOSE, 1);
curl_setopt($connection, CURLOPT_URL, "http://domain.freshdesk.com/contacts.json");
$response = curl_exec($connection);
?>
It looks like it is not sending post actually even though i have set
curl_setopt($connection, CURLOPT_POST, 1);
curl_setopt($connection, CURLOPT_POSTFIELDS, $jsondata);
I see a GET request in firebug net tab.
Is it really a post request? Because the indented operation(create new user) are not happening instead it is listing all user as it is a GET Request.
Upvotes: 0
Views: 1079
Reputation: 474
The error is in the logic of using firebug to debug this request.
You send a GET request to your server/page, create-user.php. In turn, this script/server sends a POST request to the API site. Your web client (browser), and therefore firebug, does not "know" this second part, which happens on your server.
To see the actual POST request, you should use different tools. For example, point the POST request to a machine of your own, then confirm in the server log that there was an inbound POST request.
Upvotes: 1
Reputation: 9765
That GET
request is simply your request to PHP
script which then executes POST
request.
You can't see requests done with cURL in your developer console because they were sent from server, not client.
Upvotes: 1