Post json data with PHPCuRL Raw data - php

$data = array('msisdn' =>'01033000939','basic'=>true);
$data_json = json_encode($data);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $this->urlFullSubs);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS,$data_json);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json','Content-Length: ' . strlen($data_json)));
$response = curl_exec($ch);
$s = curl_error($ch);
curl_close($ch);
return $response;
Hi I want to get information from the API and i POST Json data using PHP Curl but I cannot get the output.Can someone let me know where did I do wrong

Related

How can I get part of a JSON response that is returned from a curl POST using PHP

I am posting to an API to get an authorization token. The problem is this token is only activated for 24 hours so I am creating a cronjob that gets a new token every 24 hours.
This is my post using CURL:
<?PHP
// GET auth token
$url = 'https://website.nl';
$data = array("client_id" => "myclientid","client_secret" => "mysecret");
$postdata = json_encode($data);
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postdata);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
$result = curl_exec($ch);
curl_close($ch);
echo '<pre>';
print_r ($result);
echo '</pre>';
?>
This returns:
{
"payload": "{\"access_token\":\"my_auth_token\",\"expires_in\":86400,\"token_type\":\"Bearer\"}"
}
How can I get access_token as the only output? I've tried echoing payload but I am not sure how to do it.
echo $result[0]->payload;
echo $result->payload;
Both don't work.
You have forgotten to json_decode
$url = 'https://jsonplaceholder.typicode.com/todos/1';
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json', 'Accept: application/json'));
$result = curl_exec($ch);
curl_close($ch);
var_dump(json_decode($result));
Please take a look at playground
looks like you're getting a JSON response from the curl request.
With the dummy parameters in the code I could't test it, but I guess the solution would be something like this:
<?PHP
// GET auth token
$url = 'https://website.nl';
$data = array("client_id" => "myclientid","client_secret" => "mysecret");
$postdata = json_encode($data);
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postdata);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
$response = curl_exec($ch);
curl_close($ch);
$result = json_decode(stripslashes($response));
echo $result->payload->access_token;
?>
Let me know if it works.

Getting JSON data from API with PHP and curl

There is an API page and I want to get the whole API data with the help of PHP+curl
For example the url is https://example.com/api which contains the following:
{"data": [{"information":{"username": "john","id": 15}]}
I want to have on my php result page: {"information":{"username": "john","id": 15}
using GET method.
<?php
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://example.com/api');
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'GET');
$result = curl_exec($ch);
if (curl_errno($ch)) {
echo 'Error:' . curl_error($ch);
}
print_r($result);
curl_close($ch);
?>
and I get white page.Where is the error?

Parameter not passing in curl using Php

I am trying to fetch data from webservice(nodejs) using curl in php but i am getting result "lang is required"
i tried with following code but not working for me where i am wrong ?
Here is my code
$post = ['lang'=> "593f973dea53161779dd5660",'password'=> "amit123d"];
$ch = curl_init();
$url="http://xxxxxx:8000/api/employer/login";
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($post));
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
$response = curl_exec($ch);
$result = json_decode($response);
echo "<pre>";print_R($result);
Usually to send post variables i use : the http_build_query function.
$url = "http://xxxxxx:8000/api/employer/login";
$post = ['lang'=> "593f973dea53161779dd5660",'password'=> "amit123d"];
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
curl_setopt($ch,CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/x-www-form-urlencoded'));
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($post));
$response = curl_exec($ch);
$result = json_decode($response);
echo "<pre>";print_R($result);
It works better.
first check the request whether send post field or not.
$post = ['lang'=> "593f973dea53161779dd5660",'password'=> "amit123d"];
$ch = curl_init();
$url="https://httpbin.org/post";
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($post));
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
$response = curl_exec($ch);
$result = json_decode($response);
echo "<pre>";print_R($result);
the site: https://httpbin.org/ will response the post field if the post data exists.
I think the problem may occurs the nodejs side. Does it check the post field rightly ?
from my experience your code is right.

Curl not json response in php

not getting response using curl. I have put all solution but did not get response.
$ch = curl_init();
$header = array('api_key:xxxxxxxxxxxxxxxx','Content-Type: application/json');
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 0);
curl_setopt($ch, CURLOPT_VERBOSE, 1);
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method);
if($postdata!=""){
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postdata);
}
$response = curl_exec($ch);
curl_close($ch);
$result = json_decode($response,true);
print_r($result); // not display result
this example not displaying any result but it send to specific place.
Add this in your code and then check.
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
For your used-case you have to change your code.
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,"https://api.nhs.uk/organisations/FNM60");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
curl_close($ch);
$result = json_decode($response,true);
print_r($result);
here we are getting response without echo on the top of the page but we don't need to that type of out put so we have disabled using span tag.
echo "<span style='display:none;'>"; //to hide the curl response
$ch = curl_init();
$header = array('api_key:xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx','Content-Type: application/json');
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_VERBOSE, 1);
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method);
if($postdata!=""){
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postdata);
}
$response = curl_exec($ch);
$info = curl_getinfo($ch);
$responseBody = json_decode($response);
print_r($responseBody);
echo $response; // display 1(one)
curl_close($ch);
echo "</span>";
and result is : 1
check full code

PHP curl doesn't work

Hey guys im trying to print one api using curl. but so far i wasnt able to get it to work. maybe you guys can help me. the website uses api user and pass both can be viewed in the code i made so far. what this does is it gets the $original_url and gives us a preview based on the template_id which is 20016. you can read the documentation here https://support.dudamobile.com/API/API-Use-Cases/Multiscreen-White-Label-Setup
$original_url = "http://rsportugal.org/index.html";
$data = array("template_id"=>"20016","url"=>$original_url);
$data = json_encode($data);
define("API_USER","...");
define("API_PASS","...");
$ch = curl_init();
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_URL, 'https://api.dudamobile.com/api/sites/multiscreen/templates');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_USERPWD, API_USER.':'.API_PASS);
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
$output = curl_exec($ch);
if(curl_errno($ch)) {
die('Curl error: ' . curl_error($ch));
}
$output = json_decode($output);
curl_close($ch);
return $output->site_name;
hopefully you guys can help me
It seems you want to do a http POST on an url that doesn't support POST.
If you want to get 20016 template data, you have to do so :
$templateId = 20016;
define("API_USER","...");
define("API_PASS","...");
$ch = curl_init();
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_URL, 'https://api.dudamobile.com/api/sites/multiscreen/templates/' . $templateId);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_USERPWD, API_USER.':'.API_PASS);
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_POST, count($data));
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET");
$output = curl_exec($ch);
if(curl_errno($ch)) {
die('Curl error: ' . curl_error($ch));
}
$output = json_decode($output);
print_r($output);
curl_close($ch);
Full API documentation here => Duda mobile api Doc

Categories