I am trying to send a POST request to the server (JBOSS) which is running on localhost:9090 as shown below but it doesn't seem to be working. I mean, I am not seeing my webservice getting executed. Am I doing everything right below?
Since $_POST["mydata"] contains the following :
[{ "name": "FirstName", "value": "Mickey" }, { "name": "LastName", "value": "Mouse" }]
which is the result of running JSON.stringify() on the javascript object (using serializeArray()), I am not sending application/json related thing in the header.
$urlTest = 'http://localhost:9090/JAXRS_POST_Request/rest/Request/insertDataToDB';
$rCURL = curl_init();
curl_setopt($rCURL, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($rCURL, CURLOPT_POSTFIELDS, $_POST["myData"]);
curl_setopt($rCURL, CURLOPT_URL, $urlTest);
curl_setopt($rCURL, CURLOPT_HEADER, 0);
curl_setopt($rCURL, CURLOPT_RETURNTRANSFER, 1);
$response_post = curl_exec($rCURL);
curl_close($rCURL);
But if I have to manually test the webservice using a client like POSTMAN, I have to specify under headers as Content-Type : application/json and send it like this through the Body and it works perfectly fine there :
But while sending the request via curl, since JSON.stringify() converts the javascript object to string to send it to the server, I am confused whether I need to use header thing in my POST request or not.
P.S. A simple curl based GET request has worked fine for me so my application is successfully contacting the server.
Try this:
$urlTest = 'http://localhost:9090/JAXRS_POST_Request/rest/Request/insertDataToDB';
$data = 'FirstName=Mickey&LastName=Mouse';
$rCURL = curl_init();
curl_setopt($rCurl, CURLOPT_POST, true);//
curl_setopt($rCURL, CURLOPT_POSTFIELDS, $data);
curl_setopt($rCURL, CURLOPT_URL, $urlTest);
curl_setopt($rCURL, CURLOPT_HEADER, 0);
curl_setopt($rCurl, CURLOPT_RETURNTRANSFER, true);
if(curl_exec($rCurl) === false){
echo 'Curl error: ' . curl_error($rCurl);
}else{
$response_post = curl_exec($rCURL);
}
curl_close($rCurl);
Related
I am developing the web server using other service by CodeIgniter.
So I am going to send the data to the service using api calling.
But I have a issue to make the request.
This is the data what I want to send.
$datastr = 'proc_name=customer_upd¶ms={
"proc_info":
{
"proc_division":"U"
},
"data":[{
"table_name":"Customer",
"rows":[ {
"itemId" : "12231551",
"lastName" : "ads",
"firstName" : "fds"
} ]
}]
}'
I made my code for this.
$curl = curl_init('https://webapi.ooo.com/access/');
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($curl, CURLOPT_HTTPHEADER, array('Content-Type: application/x-www-form-urlencoded;charset=UTF-8'));
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $datastr);
curl_exec($curl);
But this is not work.
I am tested by postman, but it works well.
I think there is a issue to make the post request.
Please help me.
Thank you.
I think you are sending query string, instead, send only data in the key-value pair to the web service you are consuming. Check in which form web service is accepting the data. First, check web service from postman
Here is an example,
$data = array("key_1" =>value_1 , "key_2" =>"value_2" , "key_3" =>"value_3" , "key_4" =>"value_4" , "key_5" =>"value_5" );
function api($url,$api,$data)
{
$ch = curl_init();
$webservicelink = $url.$api;
curl_setopt($ch, CURLOPT_URL, $webservicelink);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json'));
$result = curl_exec($ch);
return json_decode($result, true);
}
I have a sample json object contain these data. I'm trying to store those data in a variable, and passing through curl POST
$json = '{
"mac": "1234567890",
"dns": "8.8.8.8,4.2.2.1",
"acl_mode": 1
}';
$url = 'http://my-site.com/api';
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode(array('json' => $json)));
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($ch);
curl_close($ch);
dd($result);
My result is 500
{
"status": 500,
"error_code": 1005
}
Is the way I set up my curl post wrong ?
After testing with curl in the terminal we found out that the endpoint consumes the entire POST payload without a key.
This was used for testing
curl -X POST -d '{"mac": "1234567890","dns": "8.8.8.8,4.2.2.1","acl_mode": 1}' http://mysite/api
So sending the $json payload as it is, without the json_encode and the array encapsulation.
curl_setopt($ch, CURLOPT_POSTFIELDS, $json);
worked just fine.
I have an api link which is working fine on terminal. I wand to use same api link to retrieve data using CURL in php. I tried below code:
<?php
// set up the curl resource
$ch = curl_init();
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
//curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_URL, 'urlxxxxx');
// execute the request
$output = curl_exec($ch);
// output the profile information - includes the header
echo "curl response is : <br> ".($output). PHP_EOL;
// close curl resource to free up system resources
curl_close($ch);
?>
but I am getting response as :
curl response is :
{ "error" : "JSON syntax error: malformed JSON string, neither array, object, number, string or atom, at character offset 0 (before \"(end of string)\") at /usr/share/perl5/JSON.pm line 171.\n" }
I am new to CURL guide me regarding the issue for resolving it.
Add:
curl_setopt($ch, CURLOPT_POSTFIELDS, "{}");
so you send an empty JSON object as the parameter. This is the PHP equivalent of
-d "{}"
from the command line.
To put some other array into the post data, use json_encode.
$data = array('userId' => array(),
'user_list' => array("1", "2", "3", "4", "5", "6", "7", "8")
);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data);
You either need to set the method to 'GET' by removing curl_setopt($ch, CURLOPT_POST, true);
Or supplying valid JSON curl_setopt($curl, CURLOPT_POSTFIELDS, "{}");
I am using KeyRock from FiWare.org. Previously everything seems to work find but now the rest server after user authorization send me the following response. I have looked at the server response and it seems to be a NULL/Empty response. But on the NodeJS the request is processed seamlessly. I am using wp-oauth plugin and have modified it to work with FiWare. I am sending this request via PHP CURL.
{
"error": {
"message": "Expecting to find application/json in Content-Type header - the server could not comply with the request since it is either malformed or otherwise incorrect. The client is assumed to be in error.",
"code": 400,
"title": "Bad Request"
}
}
Following is the CURL request I am using:
$url = URL_TOKEN . $url_params;
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, $params);
curl_setopt($curl, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
curl_setopt($curl, CURLOPT_USERPWD, CLIENT_ID . ":" . CLIENT_SECRET);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, (get_option('wpoa_http_util_verify_ssl') == 1 ? 1 : 0));
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, (get_option('wpoa_http_util_verify_ssl') == 1 ? 2 : 0));
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($curl);
have you tried to add application/json in Content-Type header?
BR
I am working to connect with an API using curl and PHP
I have this CURL statement
curl -i --user api:YXBpOmFiY2RlZmdoaWprbG1ub3BxcnN0dXZ3eHl6MDEyMzQ1 --data-binary #test.png https://api.blahblah.com/blah
where YXBpOmFiY2RlZmdoaWprbG1ub3BxcnN0dXZ3eHl6MDEyMzQ1 is the api key and #test.png is the image file I need to transfer to the API for upload. What I don't get is how to write the curl for the link above
and I also have this Example Statement as given by the site for developers(looks a JSON)
POST /blah HTTP/1.1
Host: api.blahblah.com
Authorization: Basic YXBpOmFiY2RlZmdoaWprbG1ub3BxcnN0dXZ3eHl6MDEyMzQ1
Till now, I have written this up,
$url = 'https://api.tinypng.com/shrink';
$image = 'bf4lwp2.png';
$key = 'YXBpOmFiY2RlZmdoaWprbG1ub3BxcnN0dXZ3eHl6MDEyMzQ1';
$jsonscript = array(
'Host' => 'api.tinypng.com',
'Authorization' => $key );
$json_string = json_encode($jsonscript);
$ch = curl_init($url);
// Disable SSL verification
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_BINARYTRANSFER, 1);
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, 'api:'.$key);
curl_setopt($ch, CURLOPT_PUT, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_FILE, $image);
// Execute
$result=curl_exec($ch);
Also, if my request is a success, I get this...
HTTP/1.1 201 Created
Location: https://api.blahblah.com/blahblah.png
Content-Type: application/json; charset=utf-8
{
"input": {
"size": 87654
},
"output": {
"size": 102020,
"ratio": 0.236
}
}
I know I have to use file_get_contents() to get all that, but I don't know what URL to put in that function.
To get data from json first you can use json decode and than use foreach loop to
parse those data into variables