PHP Post JSON With CURL - php

I'm trying to post JSON by using CURL, and I created a table to store the data and the response I get from the url that Im sending the data to.
However, the response keeps saying that my "Post data is empty", and I can't figure out why.
I tested it with websites like https://reqbin.com/ with the data that is stored in my table and it works fine, since I get a "successful" response.
Can someone help me figure out why my data keeps being emptied?
Below is my code
$url = "url that im trying to post Json to.";
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($json_object));
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
$result = curl_exec($ch);
I've also tried using the http_build_query but it does not work.
Thanks in advance!

PHP recognizes application/x-www-form-urlencoded standard data type by default
// one
$post = $GLOBALS['HTTP_RAW_POST_DATA'];
// two
$post = file_get_contents("php://input");

Related

How to transfer data from one server to another server

I am having some data in Server A i need to request the Server A using some api to get the data, after fetching the data from server A i need to refine that JSON Response which i got from Server A, and i need to send that refined JSON data to Server B
Note : I don't do any UI work just i need to run the code to transfer data
But I do know which language or framework should i use to implement this, and where to start this.
Can anyone suggest me ?
For getting the data from Server A you can use PHP cURL. Transform the output to the desired structure, for example by using json_decode(), transform and then json_encode() to pass it to Server B using another PHP cURL call.
// receive from ServerA
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://SERVER_A_URL");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$serverA = curl_exec($ch);
curl_close($ch);
$serverA = json_decode($serverA);
// some transformation
$serverB = [
'varOne' => $serverA->varOne,
'varTwo' => $serverA->varTwo
// ...
];
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://SERVER_B_URL");
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($serverB));
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$serverA = curl_exec($ch);
curl_close($ch);

What's wrong with this PHP CURL script (integrating Foursquare API)?

I am trying to integrate Foursquare API to my website, here is the code:
$curl = curl_init('https://api.foursquare.com/v2/photos/add?v=20181008&oauth_token='.$token.'&photo='.$args['img'].'&venueId='.$title);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_HTTPHEADER, array("Content-type: multipart/form-data"));
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
$data = curl_exec($curl);
curl_close($curl);
// Decode the response
$data = json_decode($data);
// Verify if the post was published
if ( #$data->meta->code == 200) {
return true;
} else {
return false;
}
But when I run this script on my website and try to post image, it is showing
An error occurred while processing your request
Remove the lines
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_HTTPHEADER, array("Content-type: multipart/form-data"));
and try a simple
curl_setopt($curl, CURLOPT_POST, false);
You are sending the Request with GET Arguments
This could have multiple reasons.
I can't tell from the error message where exactly it triggers but when the curl fails, you should first investigate if there were any issues with the curl request itself.
Use curl_error for this: http://php.net/manual/de/function.curl-error.php
And are you using the API-Call the correct way?
In the docs POST https://api.foursquare.com/v2/photos/add
theres no $photo param at all.
Also when using POST you should add the parameters via
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
and not as GET-Parameters via URL.

Can't see XML response from PHP cURL api Call

I'm using a remote API with cURL and querystrings. The issue I'm having is that I can't see the data being returned to me (which is in XML format). To start, here's my code:
$url = 'https://api.somehost.com/GetRequest.do';
$query = 'User=JohnDoe&Password=ABC123&Function=auth';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $query);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
$curlresponse = curl_exec($ch);
I know the cURL request is working because if I change the password to something incorrect, I get a response back (can see it using print_r($curlresponse) telling me the password is incorrect.
However, when I enter the correct password, I see nothing when I print_r($curlresponse) yet I know I'm receiving the data because a print_r(curl_getinfo($ch)) shows a larghe download_content_length which is the data I'm expecting. The data I'm expecting back is in XML format. I'm just not sure what I'm missing.
Thank you!

POST request from php

I need a php page, when loaded, to send a POST request automatically without using a submit button or any user input. I don't need to do anything with the response.
I simply need to POST "8" to www.mydomain.com
$url = 'http://www.mydomain.com/';
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, 8);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
curl_close($ch);
What am I doing wrong?
Try this:
curl_setopt($ch, CURLOPT_POSTFIELDS, array('data' => 8));
On your domain edit script to:
echo $_POST['data'];
Kind of a strange request .. you should consider using a key for the value of 8.
You can, however, get the POST body on http://www.mydomain.com/ by reading 'php://input'.
//on www.mydomain.com/index.php
echo file_get_contents('php://input'); //8
By the way you also have $curl instead of $ch on the third setopt.

POST XML data to url as value portion of key:value pair in post body with PHP/cURL (or anything else)

Ok so here's my situation. I need to POST XML data to a secure URL (ColdFusion web service) and display the returned results (returned as XML data). The requirements are as follows:
Must be a POST request.
Must use XML data string.
The XML data must be the value portion of a name=>value pair. (name can be anything)
The name=>value pair must be in the body of the request.
Content Type header must be "application/x-www-form-urlencoded"
Other than that I know of no other restrictions. I am trying to do this with PHP/cURL but can't seem to get it to work. I am able to establish the connection with the web service fine but I am returning an error that indicates that no post body is being sent. Here's my code:
$url = "https://coldfusion.cfm";
$username = "xxxxx";
$password = "xxxxxxx";
$array = array(
"idata" => "<?xml version='1.0' standalone='yes'?>
<XMLDATA1>
<XMLDATA2>
<XMLDATA3>Query</XMLDATA3>
<XMLDATA4>Platinum</XMLDATA4>
<XMLDATA5>1</XMLDATA5>
</XMLDATA2>
<XMLDATA6>
<XMLDATA7>2440031317</XMLDATA7>
</XMLDATA6>
</XMLDATA1>");
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_HEADER, TRUE);
curl_setopt($ch, CURLOPT_USERPWD, $username . ":" . $password);
curl_setopt($ch, CURLOPT_TIMEOUT, 30);
curl_setopt($ch, CURLOPT_POST, TRUE);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/x-www-form-urlencoded'));
curl_setopt($ch, CURLOPT_POSTFIELDS, $array);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
$output = curl_exec($ch);
if(curl_errno($ch))
print curl_error($ch);
else
curl_close($ch);
echo $output;
Can someone please give me an idea what i am doing wrong? If I use REST Console in Chrome I can POST the XML data as a name=>value pair via request parameters in Request Payload options and everything functions as it should. Please HELP!
Your your xml has to be a string sent via post.

Categories