I've been working on a PHP project using cURL to access external API.
Even though connecting via API is successfully done, one subtle thing bothers me...
That is, "return values of curl_exec($curl) are dumped out automatically".
Here's my codes.
$curl = curl_init();
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, 'POST');
curl_setopt($curl, CURLOPT_HTTPHEADER, ["Content-Type: application/x-www-form-urlencoded"]);
curl_setopt($curl, CURLOPT_URL, 'http://...');
curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($some_post_data));
$result = curl_exec($curl);
curl_close($curl);
That's all.
I didn't write "var_dump" or "print_r" or anything to output the result.
Nevertheless, there's always dumped result values on the display... more precisely, the dumping occurs at the line
$result = curl_exec($curl);
Does anyone know what's happening?
Set CURLOPT_RETURNTRANSFER to TRUE to return the transfer as a string of the return value of curl_exec() instead of outputting it out directly.
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
Related
I am new to the Visa Developer Platform (VDP) APIs and I am running into issues trying to read the output of the response as a json using php. I followed the tutorial here. I am using the Offers Data API.
My goal is to be able to generate a list of deals to be displayed in the frontend. I am trying to do this by reading the json output and parsing the info for the deals.
This is what the original tutorial had and it works fine:
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_PORT, 443);
curl_setopt($curl, CURLOPT_VERBOSE, 1);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 2);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 1);
curl_setopt($curl, CURLOPT_SSLVERSION, 1);
curl_setopt($curl, CURLOPT_SSLCERT, $cert);
curl_setopt($curl, CURLOPT_SSLKEY, $key);
curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($curl, CURLOPT_USERPWD, "$username:$password");
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
$response = curl_exec($curl); //This output contains the json output as well
$response_info = curl_getinfo($curl);
To get the information, I am running: $response_data = file_get_contents($response); which does not appear to work. Since the output it not just the json, but with other info, I am not able to run: $arr = json_decode($response,true); to parse the json. This is what the json looks like:
{"ReturnedResults":4,"StartIndex":1,"Offers":[{"indexNumber":1,"offerContentId":105515,"offerId":
and so on. The json starts with {"indexNumber":1 and everything before it needs to be discarded.
Please let me know what I can do to fix this, or if there is a better way to accomplish the goal. Thank you!
Goal
The json starts with {"indexNumber":1 and everything before it needs to be discarded. Please let me know what I can do to fix this, or if there is a better way to accomplish the goal.
Code
The response variable contains a valid json object. Since you only need Offers you can use this code to obtain the Offers:
$response = '{"ReturnedResults":4,"StartIndex":1,"Offers":[{"indexNumber":1,"offerContentId":105515,"offerId":""}]}';
$json = json_decode($response, true);
var_dump($json['Offers']);
Output
array(1) {
[0]=>
array(3) {
["indexNumber"]=>
int(1)
["offerContentId"]=>
int(105515)
["offerId"]=>
string(0) ""
}
}
Init cURL with CURLOPT_RETURNTRANSFER => true - then you will have your content in $response variable. Without that option, result of curl_exec is always boolean.
Description
I'm trying to make a request to an API via PHP cURL
$access_token = $tokens['access_token'];
$headers = array(
"Authorization: Bearer " . $access_token
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,env('USER_INFO'));
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET");
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);
curl_setopt($ch, CURLOPT_TIMEOUT_MS, 1000);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_VERBOSE, TRUE);
curl_setopt($ch, CURLOPT_ENCODING , "gzip");
curl_setopt($ch, CURLOPT_USERAGENT,'php');
curl_setopt($ch, CURLOPT_HTTPHEADER,$headers);
$info = curl_getinfo($ch);
$userInfo = curl_exec($ch);
$userInfo = json_decode($userInfo, true);
dd($userInfo);
I kept getting this back
{"sub":"acr:123;type=STAT","updated_at":1509463516,"name":"User","email":"user#email.com"}
1
Try #2
If I do
$userInfo = json_encode($userInfo, true);
I got
{"sub":"acr:123;type=STAT","updated_at":1509463516,"name":"User","email":"user#email.com"}
"true"
How do I get rid of the 1 or true below it, and only get the JSON data?
Is there another param for json_decode() that I need to pass in?
Set the CURLOPT_RETURNTRANSFER curl option. curl_exec is just returning true without that, (it echoes the response and returns true; see curl_exec return values) and json_decode(true) is true.
How would one go about and debug this further?
A couple of things to experiment with:
Remove $userInfo = json_decode($userInfo, true); and
dd($userInfo);. Without the CURLOPT_RETURNTRANSFER option set, you should still see the non-decoded JSON, but not the true or 1.
Add another dd($userInfo); before the json_decode. You'll see what curl_exec actually returned, which may help you eliminate json_decode as a possible cause for this odd looking behavior.
The following line should solve your problem with the response.
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
I want to create a simple page that prints the region you are in, I am using an API from this site freegeoip.net. I have it set up so it runs a users ip through the site and returns JSON, however I am having issues parsing that response. This is the code I have written:
<?php
$person = $_SERVER["REMOTE_ADDR"];
$url = "freegeoip.net/json/$person";
$cURL = curl_init();
curl_setopt($cURL, CURLOPT_URL, $url);
curl_setopt($cURL, CURLOPT_HTTPGET, true);
curl_setopt($cURL, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json',
'Accept: application/json'
));
$result = curl_exec($cURL);
curl_close($cURL);
$json = json_decode($result, true);
echo $json['region_name'];
echo $json['city'];
?>
However for some reason, it still prints the full response from the server API... how do I fix this?
Add the following line after setting CURLOPT_HTTPHEADER:
curl_setopt($cURL, CURLOPT_RETURNTRANSFER, 1);
By default curl_exec() outputs the response out directly and it returns TRUE on success or FALSE on failure. If you set CURLOPT_RETURNTRANSFER to TRUE, curl_exec() will return the actual result of successful operation, but still will return FALSE on failure.
I am working on two system.In which asterisk runs on one system-1.I want to run command in asterisk and get result back in system-2.I make curl request like below.How to get value back on system2?enter code here
exec('asterisk -rx "sip show peers"',$sip);
$POST_DATA = array(
'filename'=>$sip,
);
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL,'http://192.168.50.138/test.php');
curl_setopt($curl, CURLOPT_TIMEOUT, 30);
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, $POST_DATA);
$response = curl_exec($curl);
curl_close ($curl);
?>
Since you already have
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
in your code. curl_exec should already returns the content of the page instead of a BOOL.
This is a snippet of a library I use. As pointed out this might not be needed but it helped me out once...
//The content - if true, will not download the contents
curl_setopt($ch, CURLOPT_NOBODY, false);
Also it seems to have some bugs related to CURLOPT_NOBODY (which might explain why you have this issue):
http://osdir.com/ml/web.curl.general/2005-07/msg00073.html
http://curl.haxx.se/mail/curlphp-2008-03/0072.html
I am able to send the GET request and receive the response at following line.
$curl_resp = curl_exec($curl);
I used the following to parse the response, but it does not work, I have manually set some values to $curl_resp but still not sure how to access the value of each tag of the xml separately.
$xml = simplexml_load_string($curl_resp);
NOTE: I recevice the actual xml but cant parse it, (I need to get each tag's value separately in a variable)
Code:
<?php
$service_url = ' The Url goes here';
$curl = curl_init($service_url);
$curl_post_data = array(
"PASSWORD" => 'pass',
"USERNAME" => 'username'
);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $curl_post_data);
$curl_resp = curl_exec($curl);
curl_close($curl);
Your variable $curl_response is different than $curl_resp (what you're trying to parse)
You can access the value of each tag just like any other array.
if the CURLOPT_RETURNTRANSFER option is not set then your $curl_resp will just return true/false.
if it is set you may be returning false or a poorly formed xml string. If you post more code or the actual curl response text we may be able to provide more info.
EDIT:
upon reading the code looks like you are assigning the response text to $curl_response instead of $curl_resp
$ch = curl_init();
curl_setopt( $ch, CURLOPT_URL, "your url");
curl_setopt($ch, CURLOPT_USERAGENT, 'Googlebot/2.1 (+http://www.google.com/bot.html)');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$response = curl_exec($ch);