This is my json data
$data = array(
"api_key"=>"xxxx",
"email_details"=> array( "fromname"=>"test",
"subject"=>"Registration",
"from"=>"xxx#xxx.com",
"content"=>"test"),
"recipients"=>["xxx#gmail.com"]
);
generated the json data:
$str_data = json_encode($data);
it gives following format of json:
{"api_key":"xxxx",
"email_details":{"fromname":"test","subject":"Registration","from":"xxx#xxx.com","content":"test"},
"recipients":["xxx#gmail.com"]}
it is valid json format
verified from this url
http://jsonlint.com/
from the postman i have tried to run the API:
https://api.xxx.com/xxx/json?data={"api_key":"xxxx","email_details": {"fromname":"xxx","subject":"Registration","from":"xxx#xxx.com","content":"test" },"recipients":["xxx#gmail.com"]}
it is given the success message
when i tried to run this api from php code :
$url_email="https://api.xxx.com/xxx/json?data=";
$chh = curl_init();
curl_setopt($chh, CURLOPT_URL,$url_email);
curl_setopt($chh, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($chh, CURLOPT_POSTFIELDS, $str_data);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json',
'Content-Length: ' . strlen($str_data))
);
curl_setopt($chh, CURLOPT_RETURNTRANSFER, true);
curl_exec($chh);
curl_close($chh);
i didn't get the o/p
getting the error message :
{"message":"ERROR","errorcode": "100" ,"errormessage":"Invalid JSON format used in API Call.Hence failed."}
whats wrong here ?
I suppose it should be:
$url_email="https://api.xxx.com/xxx/json"; // no `data` here
$chh = curl_init();
curl_setopt($chh, CURLOPT_URL, $url_email);
curl_setopt($chh, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($chh, CURLOPT_POSTFIELDS, 'data=' . $str_data); // `data` moved here
Related
I'm trying to get the data from pull service in PHP while i"m calling the end point in postman I'm getting the output
but while I'm trying to call using PHP I'm getting the 500
i have attaching the PHP code please guide me where I'm doing wrong
//API Url
$url = 'https://abc.xyz/vlet?Info=';
//The JSON data.
$jsonData =
array(
'NAME' => 'anish',
'Key' => 'vQOgCmDgxxdw',
'authToken' => 'xswSgsG8Dtz05Rfhfzr83t25',
'work_date' => '2020-07-07'
);
//print_r( $jsonData); exit;
//Encode the array into JSON.
$jsonDataEncoded = json_encode($jsonData);
$full_url = $url.$jsonDataEncoded ;
//echo $full_url ;
$result1 = file_get_contents($full_url);
print_r( $result1);
//Initiate cURL.
$curl = curl_init();
$ch = curl_setopt($curl, CURLOPT_URL, $full_url);
//$ch = curl_init($url);
//Tell cURL that we want to send a POST request.
curl_setopt($ch, CURLOPT_POST, 1);
//Attach our encoded JSON string to the POST fields.
curl_setopt($ch, CURLOPT_POSTFIELDS, $jsonDataEncoded);
//Set the content type to application/json
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
//Execute the request
$result = curl_exec($ch);
print_r($result);
i am trying to get indivdual data using curl from this website http://dummy.restapiexample.com/ .. I use the following code to get all data
<?php
$url='http://dummy.restapiexample.com/api/v1/employees';
$my_curl = curl_init();
curl_setopt($my_curl, CURLOPT_URL, $url);
curl_setopt($my_curl, CURLOPT_BINARYTRANSFER, true);
curl_setopt($my_curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($my_curl, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($my_curl, CURLOPT_SSL_VERIFYPEER, 0);
$my_data = curl_exec($my_curl);
$my_json = json_decode($my_data);
echo '<pre>'; print_r($my_json ); echo '</pre>';
?>
1) This is working . But i want to get the indvidual data , so i tried to chage url and use the same code
$url="http://dummy.restapiexample.com/api/v1/employee/1"
But it is not working .
2) Also i try to create a new record , that is working
$data = array("name" => "Hagridbo", "salary" => "123", "age" => "36");
$data_string = json_encode($data);
$ch = curl_init('http://dummy.restapiexample.com/api/v1/create');
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json',
'Content-Length: ' . strlen($data_string))
);
$result = curl_exec($ch);
But how can i get the response like data is inserted ? what data is inserted ?
1. Regarding individual employee data is not coming
If you look to the http://dummy.restapiexample.com/api/v1/employees, you will see that employee id 1 is not there. That's why you are not getting any data.
for example check data of this link:- http://dummy.restapiexample.com/api/v1/employee/20276
Also it seems that v1/employees API data changing on a regular interval.
2. Regarding created an employee successfully, but not getting any response.
do var_dump($result); after $result = curl_exec($ch);and see what it give you. I think it will give you a json data response of added user.
I said it based on details of that API link, check here: http://dummy.restapiexample.com/create
This is my sample JSON Request
{
"Username":"admin",
"Password":"root123",
"PinCodeā : "hello321"
}
And also I want to post a request token as well, the request token has to be posted as a Request Header.
When I successfully sent those data and valid token, I need to receive a jSON response as below,
{
"Title": "Mr.",
"Name": "Pushkov",
"Age": "18"
}
My Question is,
How can I POST my above mentioned JSON data and the token in PHP cURL and display an error if the details are wrong in code igniter?
This is what I have done so far..and its not giving me the exact output im looking for and I couldn't figure out a way to send my token as a header request...
public function send_veri(){
$url='http://helloworld21.azurewebsites.net/api/member/login';
$ch = curl_init('http://localhost/apphome');
$data = array("Username" =>$this->input->post('un'), "Password" =>$this->input->post('pw'), "PinCode" =>$this->input->post('VerificationCode'));
$data_string = json_encode($data);
// Disable SSL verification
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
//curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json',
'Content-Length: ' . strlen($data_string))
);
// Will return the response, if false it print the response
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
// Set the url
//curl_setopt($ch, CURLOPT_URL,$url);
// Execute
$result=curl_exec($ch);
}
once I run this, I could only get Username, Password and the PinCode only. I cannot post the request token in my header, in causing of that i cannot get the member detail response.
For Sending Data -
public function send_veri(){
$url='http://helloworld21.azurewebsites.net/api/member/login';
$ch = curl_init( $url );
$bodyData=json_encode(array()); // here you send body Data
$data = json_encode(array("Username" =>$this->input->post('un'), "Password" =>$this->input->post('pw'), "PinCode" =>$this->input->post('VerificationCode')));
curl_setopt( $ch, CURLOPT_POSTFIELDS, $bodyData );
curl_setopt( $ch, CURLOPT_HTTPHEADER, array("headerdata: ".$data.",Content-Type:application/json"));
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );
$result = curl_exec($ch);
curl_close($ch);
}
Get Header data on Apache Server Code -
$hEAdERS = apache_request_headers();
$data = json_decode($hEAdERS['headerdata'], true);
echo $contentType=$hEAdERS['Content-Type'];
echo $Username = #trim($data['Username']);
echo $Password = #trim($data['Password']);
echo $PinCode = #trim($data['PinCode']);
It works for me.
I have a web service that accepts GET, POST values as parameters. So far I used to call the data using a simple CURL script, as below:
http://localhost/service/API.php?key=some_password&request=some_request&p1=v1&p2=v2
This is successfully posted using "CURLOPT_POSTFIELDS":
$base_args = array(
'key' => 'some_password',
'request' => 'some_request',
);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
The problem is, I need to also POST a JSON body to this service. I found out that this can be done by using "CURLOPT_POSTFIELDS", but also found out that I am not able to use both POST and JSON.
$data_string ="{'test':'this is a json payload'}";
$ch = curl_init('http://localhost/service/API.php');
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json',
'Content-Length: ' . strlen($data_string)));
$result = curl_exec($ch);
echo ($result); // string returned by the service.
I can either set the POST values, OR set the JSON body, but not both. Is there a way I can push the two different types of values?
Try sending the json text via a variable:
$data = array('data' => "{'test':'this is a json payload'}", 'key' => 'some_password', 'request' => 'some_request');
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));
Then in your API you do:
var_dump(json_decode($_POST['data']));
You can still use the other key/request variables.
I could not find any way of sending both POST and JSON body at the same time. Therefore the workaround was to add the POST arguments as part of the JSON body.
$base_args = array(
'key' => '8f752Dd5sRF4p',
'request' => 'RideDetails',
'data' => '{\'test\',\'test\'}',
);
$ch = curl_init('http://localhost/service/API.php');
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($base_args));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json',
'Content-Length: ' . strlen(http_build_query($base_args)))
);
$result = curl_exec($ch);
echo ($result);
The full JSON can be returned by the API as below:
var_dump(json_decode(file_get_contents('php://input')));
I want to make a post to a server I have by using php.
I was thinking in curl but all examples I find, urlfy data and I have to send a json file but not in the url.
I already have the json in an array : 'key'=>'value'...
I have to add headers, I think I can with this:
curl_setopt($ch,CURLOPT_HTTPHEADER,array('HeaderName: HeaderValue','HeaderName2: HeaderValue2'));
but I don't knoe how to add my array and post it.
Any idea?
I need to add a json like this:
[{"a":"q",
"b":"w",
"c":[{
"e":"w",
"r":"t"
}]
}]
Here how you can post the data using CURL, and as you mentioned you already have a json you can do so as
$ch = curl_init();
curl_setopt( $ch, CURLOPT_URL, 'your api end point');
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, TRUE);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postfields); // $postfields is the json that you have
$request_headers = array();
$request_headers[] = 'HeaderName: HeaderValue','HeaderName2: HeaderValue2';
$request_headers[] = 'Content-Type: application/json','Content-Length: ' . strlen($postfields) ;
curl_setopt($ch, CURLOPT_HTTPHEADER, $request_headers);
$response = curl_exec($ch);
curl_close ($ch);