Response:
{
"error":null,
"value1":"STRING",
"valuelist":{
valueurl:{
"status":STATUS_TEXT,
},
}
}
cURL:
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $response);
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, false);
curl_setopt($ch, CURLOPT_TIMEOUT, 3);
$content = curl_exec ($ch);
curl_close ($ch);
return $content;
now I have json_decoded it, I can't get value of "status".
echo $foo->value1; works fine.
echo $foo->status; isn't right. How would I get that value?
The value of status should be access like below:
$foo->valuelist->valueurl->status
When sometimes you don't know where is certain property or index you can always maka a var_dum() de variable, example:
var_dump(json_decode($content));
So you can see the tree structure:
$foo->valuelist->valueurl->status
Related
I need help with API Integration. When I echo the variable $url, I get the result, but I do not know why cURL is not working for me:
<?php
$token="43e6c623dda8f35df4bXXXfa5f0ec57d58e91154a ";
$format="json";
$waybill="974510010010";
$ref_nos="";//either this or waybill
$verbose="0";// meta info need to append in url
$url="https://test.delhivery.com/api/packages/json/?token=".$token."&format=".$format."&waybill=".$waybill."&ref_nos=".$ref_nos."&verbose=".$verbose;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HTTPGET, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
//curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
echo curl_error($ch);
$return = curl_exec ($ch);
curl_close ($ch);
echo $return;
?>
Do you have access to the service you are connecting to?
It could be that the service requires the data as POST variables as opposed to the GET parameters you are sending.
Or perhaps some error handling on server that does not validate the waybill value you are sending. The ref_nos variable is also empty, which some applications could interpret as invalid. A tips would be to omit the ref_nos variable from the request string if its value is empty.
Details are here:
First i will hit on this link eg. http://your.app/callback?code=abc123
then i will receive value from code variable from url then i want to redirect this url as a POST action in https://api.another.com/token
grant_type=authorization_code&
code=[CODE] url, bearing the value of code
Use Curl:
An example:
<?php
// if you get the code here from the url,
$code = $_GET['code'];
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,"http://api.another.com/");
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS,
"token_grant_type=authorization_code&code=" + $code);
// receive server response ...
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$server_output = curl_exec ($ch);
curl_close ($ch);
?>
I'm using this to get my response (only relevant code below):
$ch = curl_init();
curl_setopt($ch, CURLOPT_HTTPHEADER,$headr);
curl_setopt($ch, CURLOPT_URL,$target_url);
curl_setopt($ch, CURLOPT_POST,1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $fields);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
$result=curl_exec ($ch);
curl_close ($ch);
echo $result;
$my_resp = $result['total_count'];
var_dump($my_resp);
echo $result gives me an object that appears to be valid JSON. Like this:
{
"total_count": 1
}
However, $my_resp comes back as NULL. What am I doing wrong here?
You need to turn it into an object from a string
$result = json_decode( curl_exec($ch));
or an associative array
$result = json_decode( curl_exec($ch), true );
If it is JSON, you should json_decode() it before trying to read fields.
I want to dislay the number up to 2 decimal point the output is coming from the xml file through curl function. I tried the number_format, printf('%.2f',$number)and sprintf() functon.these are displaying the result 0.00.The code is following.
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, `http://orbisadvisors.redinews.com/tools/XM01? queryid=QJ33020&fields=Last&fields=Change&fields=Chperc&symbol=BSZ`);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$sp= curl_exec ($ch);
$sp1=explode(" ",$sp);
echo "????".$lsp1=$sp1[4];
printf("%.2f",$lsp1);
print_r($sp1);
curl_close ($ch);
In case I use the static number in place of the variable .It is giving the correct result.
Thanks
You should be using a library like SimpleXML to parse the XML data that you're receiving, like the following:
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'http://orbisadvisors.redinews.com/tools/XM01?queryid=QJ33020&fields=Last&fields=Change&fields=Chperc&symbol=BSZ');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$sp= curl_exec ($ch);
curl_close ($ch);
$xml = simplexml_load_string($sp);
$last = sprintf("+%.2f", $xml->Stock->Last); // 1280.70
I'm doing curl post but the problem is that I need to return empty results so I can echo out another value for my ajax request results:
below is the code:
$ch = curl_init("http://www.rankreport.com.au/ajax/add_new_lead");
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, "lead_company_id=1&lead_business=1234&lead_first_name=asdf&lead_website=12314.com&lead_phone=1234&lead_email=test#test.com&lead_package=seo");
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_exec($ch);
curl_close($ch);
echo 'error 3';
When I do this, I'm getting below results:
{"lead_date":1315637343,"lead_company_id":"1","lead_business":"1234","lead_first_name":"asdf","lead_last_name":"","lead_website":"12314.com","lead_phone":"1234","lead_email":"test#test.com","lead_package":"seo"} 3
How do set curl options so that it doesn't return any curl results?
Set CURLOPT_RETURNTRANSFER:
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
$result = curl_exec($ch);
Use CURLOPT_RETURNTRANSFER = true to have the returned content as a return value ($ch) instead of output.