Display Number up to two decimal point error - php

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

Related

How can I do a PHP request like I already have in Python which gives me data in JSON

I have a Python script which gives me back some data in JSON. It starts a session, posts some auth data and requests data which comes back with JSON. That works fine, but can somebody help me to do this in PHP? I am sure it is possible but I am struggling to construct that.
import requests
with requests.Session() as s:
start_time = time.time()
s.post('http://IP:PORT/WHATEVER/AUTH', data={'username':'jdoe','password':'forgotten'})
req = 'http://IP:PORT/WHATEVER/DATA/BOM%20fe?table=cars,tires,&format=json'
res = s.get(req)
you need to look for curl request in php. here is a example
function getdata($url){
if(!function_exists("curl_init"))
die("cURL extension is not installed");
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS,
"postvar1=value1&postvar2=value2&postvar3=value3");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$output = curl_exec ($ch);
curl_close ($ch);
return $output;
}
$output = getdata("https://website.com");
var_dump($output);
you can use json decode fuction to covert into matrix and use the variables
$arr = json_decode($output,true);
you can use something like this for formatting:
curl_setopt($ch, CURLOPT_POSTFIELDS,
http_build_query(array('postvar1' => 'value1')));

Post data from Android App to Backend PHP

If i assign a value to a variable and pass it to url, it is working fine.
$waybill=508518;
$url = 'http://api.ecomexpress.in/track_me/api/mawbd/?awb='.$waybill.'&order=&username=cs#subhashreeinfotech.com&password=####';
$ch = curl_init(); // initiate curl
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); // return the output in string format
$output = curl_exec ($ch);
But what i need is , i have to get the waybill number dynamically from android app.For that i am posting the waybill value to back end php code like below.But its not working
$waybill=$_POST['bill'];
$url = 'http://api.ecomexpress.in/track_me/api/mawbd/?awb='.$waybill.'& order=&username=cs#subhashreeinfotech.com&password=####';
$ch = curl_init(); // initiate curl
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); // return the output in string format
$output = curl_exec ($ch);
Error : undefined index bill
Solved the issue using the following code
$waybill=$_POST['srt'];
ob_start();
echo $_POST['srt'];
$my = ob_get_contents();
$url='http://52.66.178.241//track_me/api/mawbd/?
awb='.$my.'&order=&username=subhashree54396&password=su54nbh39tmed';
$ch = curl_init(); // initiate curl
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); // return the output in
string format
$output = curl_exec ($ch);
print_r($output);
curl_close($ch);

cURL PHP Getting value of JSON

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

How to Post Action automatically in php?

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);
?>

PHP Curl Print Problem

I'm trying to print an external site's HTML using curl, but when I go to print the html I just get a bunch of bad characters:
D\ÓLþ¢GΖ´ï!ñ{HÑ,Jþ»H¹§L+÷53j?‰²î¡<‘*tÜe÷uÖbìê~Æô¬²c˜‹§ ~áäÆL#f?ⶊªþU™á˜ÉÉOæ{^¤ëaÀ Tê"1Û¨Dtî’œxˆk‘:#ŽD5î:'¶e\*³q‘׸`…±¾ôäó÷ð1j7þä‘åQ6®9bcxã„A2ã—-ÇøüåÉò÷2{ÂÐe桢ǙŒÄg©Az!Ø¡>±zךÂ+;f RZÛÝ€ížáÒžHa¬¢Æ'ë•ñ þ=Ð=ºtyšÖâå'ÇpžÄ¦ÆN½€5½roåðFe¹)ˆš`ØnhŠy(GÆÔ} Bu7H¥JzÐ iVê÷áÆ”øG>6HÿUµÞhµj YH-ÌaaEÚx±‰…Êâ£-ûeÎqCÆLÌå㘎‰†LÐÆM I]€a)Ï.$—ÅH••£ ŒŒ’ªÉ;cŽc(i´¥!I]‹€„³tFc^ë'€e±øÉ_øß
Here is my code:
$curl = curl_init();
curl_setopt ($curl, CURLOPT_URL, "http://www.thesite.com/");
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
$result = curl_exec ($curl);
curl_close ($curl);
print $result;
Any help would be great,
Thanks!
If the response is GZIP'd, you may use this:
curl_setopt($curl, CURLOPT_ENCODING, 'gzip');
See: http://www.php.net/manual/en/function.curl-setopt.php
To see if it's gzip'd, check the headers (see CURLOPT_HEADER).

Categories