I am trying to get the data of id "yfs_a00_xauusd=x" from span from the xml generated by yql..
I tried this.. but the result was
Ask:
true http://finance.yahoo.com/q?s=XAGUSD%3DX&ql=1 613 600 27426 28.3600 where i only want to grab 28.3600
<?php
session_start();
$yql_base_url = "http://query.yahooapis.com/v1/public/yql";
$xpath='//*[#id="yfs_g00_xagusd=x"]';
$url = 'http://finance.yahoo.com/q?s=XAGUSD%3DX&ql=1';
$yql_query = "select * from html where url=$url and xpath = $xpath;";
$yql_query_url = $yql_base_url . "?q=" . urlencode($yql_query);
$yql_query_url .= "&env=http://datatables.org/alltables.env";
$yql_query_url .= "&format=json";
$session = curl_init($yql_query_url);
curl_setopt($session, CURLOPT_RETURNTRANSFER,true);
$json = curl_exec($session);
$phpObj = json_decode($json);
if(!is_null($phpObj->query->results->span)){
foreach($phpObj->query->results->span as $result){
$_SESSION['price'] = $result;
}
}
?>
<div> Ask:<?php echo $_SESSION['span'];?> </div>
The XML generated by yahoo is....
<query yahoo:count="1" yahoo:created="2012-05-16T19:00:25Z" yahoo:lang="en-US">
<diagnostics>
<publiclyCallable>true</publiclyCallable>
<url execution-start-time="1" execution-stop-time="601" execution-time="600" proxy="DEFAULT">http://finance.yahoo.com/q?s=XAGUSD%3DX&ql=1</url>
<user-time>613</user-time>
<service-time>600</service-time>
<build-version>27426</build-version>
</diagnostics>
<results><span id="yfs_g00_xagusd=x">28.3600</span></results>
</query>
I really appreciate your help.. Thanks in advance.
Your query is broken, the values should be quoted.
$yql_query = "select * from html where url='$url' and xpath='$xpath';";
Or better yet, provide those values via query parameters.
$yql_query = "select * from html where url=#url and xpath=#xpath;";
$params = array(
'q' => $yql_query,
'url' => $url,
'xpath' => $xpath,
'env' => 'http://datatables.org/alltables.env',
'format' => 'json',
);
$yql_query_url = $yql_base_url . '?' . http_build_query($params);
Once you have got YQL returning the result that you really want, the price that you are looking for will be available at.
$phpObj->query->results->span->content
See a running example.
Related
Well I will not take credit for these codes as I found it but I would appreciate if someone can help me display the weather with this following coding I will really appreciate it.
$BASE_URL = "http://query.yahooapis.com/v1/public/yql";
$yql_query = 'select * from weather.forecast where woeid in (select woeid from geo.places(1) where text="sc")';
$yql_query_url = $BASE_URL . "?q=" . urlencode($yql_query) . "&format=json";
// Make call with cURL
$session = curl_init($yql_query_url);
curl_setopt($session, CURLOPT_RETURNTRANSFER,true);
$json = curl_exec($session);
// Convert JSON to PHP object
$phpObj = json_decode($json);
echo '<pre>';print_r($phpObj).'<pre>';
I just want this code to display the weather of a particular place with some variable which I can echo like
echo $city;
echo $temp;
something like this.
really thank you for your valuable time and kindness for helping
For Php Object:
$phpObj = json_decode($json); // converting to object
echo $phpObj->property_name;
For Php Array:
$phpArr = json_decode($json, true); // converting to array
echo $phpArr['index'];
ok i got it and sharing the codes so that it could be of use to someone who is looking for weather api
$BASE_URL = "http://query.yahooapis.com/v1/public/yql";
$yql_query = 'select * from weather.forecast where woeid in (select woeid from geo.places(1) where text="sc")';
$yql_query_url = $BASE_URL . "?q=" . urlencode($yql_query) . "&format=json";
//Make call with cURL
$session = curl_init($yql_query_url);
curl_setopt($session, CURLOPT_RETURNTRANSFER,true);
$json = curl_exec($session);
// Convert JSON to PHP object
$phpObj = json_decode($json);
echo $phpObj->query->results->channel->location->city.' Weather <br/>';
echo 'Current: '.$phpObj->query->results->channel->item->condition->text.', ';
echo sprintf("%0.0f", ($phpObj->query->results->channel->item->condition->temp - 32) * 5 / 9).'°C <br/>';
echo $phpObj->query->results->channel->item->forecast[0]->day.': ';
echo $phpObj->query->results->channel->item->forecast[0]->text.', ';
echo '<small>'.sprintf("%0.0f", ($phpObj->query->results->channel->item->forecast[0]->low - 32) * 5 / 9).'Min°C - </small>';
echo '<small>'.sprintf("%0.0f", ($phpObj->query->results->channel->item->forecast[0]->high - 32) * 5 / 9).'Max°C </small><br/>';
echo $phpObj->query->results->channel->item->forecast[1]->day.': ';
echo $phpObj->query->results->channel->item->forecast[1]->text.', ';
echo '<small>'.sprintf("%0.0f", ($phpObj->query->results->channel->item->forecast[1]->low - 32) * 5 / 9).'Min°C - </small>';
echo '<small>'.sprintf("%0.0f", ($phpObj->query->results->channel->item->forecast[1]->high - 32) * 5 / 9).'Max°C </small><br/>';
I am developing an android app where I have to get the user's speed and match it with the speedlimit of the road in order to see how he/she is driving.
I am using Open Street Maps to get the road speed limit. But, I'm having problem in getting the tag 'max-speedlimit' which gives the speed limit of the road. Below given php code I am using to get the latitude and longitude from the app and then finding the speed limit. I am having hardtime getting the speedlimit. As there are so many tags in the response from the OSM, I only want to get the max-speed tag's value
Thank you so much!
<?php
$lat = isset($_POST['lat']) ? floatval($_POST['lat']) : "";
$lng = isset($_POST['lng']) ? floatval($_POST['lng']) : "";
$latm = -0.00015 + $lat;
$latp = 0.00015 + $lat;
$lngm = -0.00015 + $lng;
$lngp = 0.00015 + $lng;
$json_url = 'http://overpass.osm.rambler.ru/cgi/interpreter';
$data = '<query type="way"> <bbox-query s="' . $lngm . '" w="' . $latm . '" n="' . $lngp . '" e="' . $latp . '"/> <!--this is auto-completed with the current map view coordinates.--> </query> <print/>';
$ch = curl_init( $json_url );
$options = array(
CURLOPT_POST => true,
CURLOPT_POSTFIELDS => $data,
CURLOPT_RETURNTRANSFER => true,
);
curl_setopt_array( $ch, $options );
$result = curl_exec($ch);
//CONVERT THE RESPONSE IN STRING AND GET THE MAXSPEED TAG VALUE ONLY!
I am trying to get the speed limit using the OSM through php. But, I am unable to do that because I am getting the message as written below:
The data included in this document is from www.openstreetmap.org. The data is made available under ODbL.
What could be the problem? Below is my php file.
Note: the code is still incomplete, I'm for now testing if I am able to access the OSM database.. But I cant. I hope someone could point me to the right direction..
<?php
$lat = 24.32633;
$lng = 54.58061;
$latm = -0.00015 + $lat;
$latp = 0.00015 + $lat;
$lngm = -0.00015 + $lng;
$lngp = 0.00015 + $lng;
//$json_url = 'http://overpass-api.de/api/interpreter?data=[out:json];node(24.326180, 54.580460,24.336580, 54.580860);way(bn);(._;>;);out;';
$json_url = 'http://overpass.osm.rambler.ru/cgi/interpreter';
$data = '<query type="way"> <bbox-query s="' . $lngm . '" w="' . $latm . '" n="' . $lngp . '" e="' . $latp . '"/> <!--this is auto-completed with the current map view coordinates.--> </query> <print/>';
$ch = curl_init( $json_url );
$options = array(
CURLOPT_POST => true,
CURLOPT_HTTPHEADER => array('Content-type: application/json') ,
CURLOPT_POSTFIELDS => $data,
CURLOPT_RETURNTRANSFER => true,
);
curl_setopt_array( $ch, $options );
$result = curl_exec($ch);
echo curl_exec($ch);
/*$resultArr = explode("<",$result);
foreach ($resultArr as $val) {
$temp = explode('"', $val);
//check the size of the array, if it is == 5, then do
if ($temp[1]=="maxspeed")
$speedlimit=$temp[3];
}
echo '{"speedlimit": "120"}'; */
?>
Your bounding box is quite small and there's simply no data available in OpenStreetMap for your bounding box. That why you get the following almost empty, but valid result:
<?xml version="1.0" encoding="UTF-8"?>
<osm version="0.6" generator="Overpass API">
<note>The data included in this document is from www.openstreetmap.org. The data is made available under ODbL.</note>
<meta osm_base="2015-06-06T08:10:03Z"/>
<bounds minlat="54.5805" minlon="24.3262" maxlat="54.5808" maxlon="24.3265"/>
</osm>
I would highly recommend to try different options in overpass turbo first to get more familiar with Overpass API. Please check the following link as a starting point: http://overpass-turbo.eu/s/9MQ - it's also restricted to highways with a maxspeed tag (that's what you're looking for, right?).
For illustration purposes, here's a screenshot with your tiny bounding box in the middle:
So I'm having a problem with the following code.
I've got CURLOPT_RETURNTRANSFER set to true, yet nothing is returned when curl_exec is hit. Any and all help is appreciated!
<?php
$yql_base_url = "http://query.yahooapis.com/v1/public/yql?q=";
$yql_query = "select * from csv where url='http://download.finance.yahoo.com/d/quotes.csv?s=YHOO,GOOG,AAPL&f=sl1d1t1c1ohgv&e=.csv' and columns='symbol,price,date,time,change,col1,high,low,col2'";
$yql_params = "&format=json&diagnostics=true&callback=";
$yql_url = $yql_base_url . urlencode($yql_query) . $yql_params;
$session = curl_init($yql_url);
curl_setopt($session, CURLOPT_RETURNTRANSFER, true);
$json = curl_exec($session);
curl_close($session);
$phpObj = json_decode($json);
if(!is_null($phpObj->query->results))
{
echo $phpObj->query->results;
}
?>
$phpObj->query->results is an array of Object and you can not do echo on it. Simply use print_r() or var_dump() on it.
Example:
print_r($phpObj->query->results);
var_dump($phpObj->query->results);
I am integrating the API from our monitoring software (PRTG) into our website and attempting to use a function that generates a list of data in XML format. As it is generated as needed, the URL doesn't point to an existing file.
I tried using "simplexml_load_file" and "simplexml_load_string" and passing the URL with no luck. I've also tried use "file_put_contents" to first save the file, but it also fails since the URL doesn't actually point to a file.
How can this be made to work?
<?php
$prtg_url = "http://prtg.domain.net:8080/";
$prtg_user = "username";
$prtg_hash = "passwordhash";
function getSensorData($deviceid)
{
$sensor_xml_file = $GLOBALS['prtg_url'] . "api/table.xml?content=sensors&output=xml&columns=objid,type,device,sensor,status&id=" . $deviceid . "&username=" . $GLOBALS['prtg_user'] . "&passhash=" . $GLOBALS['prtg_hash'];
file_put_contents("sensor.xml", fopen($sensor_xml_file, 'r'));
$sensors = simplexml_load_file("sensor.xml");
foreach ($sensors->item as $sensor)
{
$sensor_ping = $sensor->ping;
$sensor_id = $sensor->objid;
$sensor_type = $sensor->type;
$sensor_typeraw = $sensor->type_raw;
echo $sensor_ping . "</br>";
echo $sensor_id . "</br>";
echo $sensor_type . "</br>";
echo $sensor_typeraw . "</br>";
}
}
getSensorData("3401");
?>
It may be something to do with the file handler. You could try using simplexml_load_file() with your URL. For example:
$url = $GLOBALS['prtg_url']
. "api/table.xml?content=sensors&output=xml&columns=objid,type,device,sensor,status&id="
. $deviceid . "&username=" . $GLOBALS['prtg_user']
. "&passhash=" . $GLOBALS['prtg_hash']);
$xml = simplexml_load_file($url);
try this:
$c = curl_init();
curl_setopt($c, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($c, CURLOPT_URL, $sensor_xml_file);
$xmlString = curl_exec($c);
curl_close($c);
$sensors = simplexml_load_string($xmlString);
$opts = array('http' =>
array(
'method' => 'GET',
'header' => "Content-Type: text/xml\r\n",
'timeout' => 60
)
);
$context = stream_context_create($opts);
$url = $GLOBALS['prtg_url']
. "api/table.xml?content=sensors&output=xml&columns=objid,type,device,sensor,status&id="
. $deviceid . "&username=" . $GLOBALS['prtg_user']
. "&passhash=" . $GLOBALS['prtg_hash']);
$result = file_get_contents($url, false, $context);
echo $results;
This forces a 60 second timeout on the operation, that way you can see if it actually fetches any results. If it does return incomplete results, switch to using XMLReader for your parser.