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
Related
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.
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.
This is my function:
function postCurl($url, $jsql) {
$data = array('sql' => $jsql);//jsql is a json string
$headers = array('Content-Type: application/x-www-form-urlencoded');
var_dump($data);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,TRUE);
$output = curl_exec($ch);
return $output;
}
It always returns false. I've tried to make the same request with REST client and it works.
Check in the server whether curl is enabled or not!!
and use the below code
$data = array('name' => $_REQUEST['name']);
$ch = curl_init('www.example.com/');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));
$response = curl_exec($ch);
curl_close($ch);
$xml_output = htmlentities($response);
How to call more than one url in curl using php and must be executed sequentially one after the another..
Below is my php code
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,"http://192.168.1.220/cgi-bin/handle_login.tcl");
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS,
"user=admin&pw=admin&submit=Login");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
echo "$response";
curl_close ($ch);
$ch1= curl_init();
curl_setopt($ch1, CURLOPT_URL,"http://192.168.1.220/cgi-bin/controller.tcl?sid=$response&type=inverter&inverter=318002N463");
curl_setopt($ch1, CURLOPT_HTTPGET, TRUE);
curl_setopt($ch1, CURLOPT_RETURNTRANSFER, true);
$response1= curl_exec($ch1);
curl_close ($ch1);
$ch2= curl_init();
curl_setopt($ch2, CURLOPT_URL,"http://192.168.1.220/cgi-bin/overview.tcl?sid=$response&menuParentId=3");
curl_setopt($ch2, CURLOPT_HTTPGET, TRUE);
curl_setopt($ch2, CURLOPT_RETURNTRANSFER, true);
$response2= curl_exec($ch2);
curl_close($ch2);
Only my first curl command is executed remaining is not being executed
For sequential curl request you can define function and use it like this. If you are not concerned about sequential request you can use multi curl for that.
<?php
ini_set('display_errors', 1);
$response=curl_request("http://192.168.1.220/cgi-bin/handle_login.tcl","POST","user=admin&pw=admin&submit=Login");
curl_request("http://192.168.1.220/cgi-bin/controller.tcl?sid=$response&type=inverter&inverter=318002N463");
curl_request("http://192.168.1.220/cgi-bin/overview.tcl?sid=$response&menuParentId=3");
function curl_request($url,$method="GET",$postFields="")
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
if($method=="POST")
{
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postFields);
}
else
{
curl_setopt($ch, CURLOPT_HTTPGET, TRUE);
}
$response = curl_exec($ch);
echo "$response";
return $response;
}
The best exemple i have found is:
$request_body = 'some data';
$ch = curl_init('http://www.example.com');
curl_setopt($ch, CURLOPT_POSTFIELDS, $request_body);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "DELETE");
$response = curl_exec($ch);
var_dump($response);
Though this is to deltete an FB app request so the URL should look like this: https://graph.facebook.com/[request_id]?access_token=USER_ACCESS_TOKEN
Could someone show me how to implement that code for my case ?
Do not use curl_setopt($ch, CURLOPT_POSTFIELDS, $request_body);
Use
$ch = curl_init('https://graph.facebook.com/'.$request_uri.'&access_token='.$access_token);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "DELETE");
$response = curl_exec($ch);
var_dump($response);