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/>';
Related
I have to use following API URL to fetch Books information (found here)-
http://webservices.amazon.com/onca/xml?
Service=AWSECommerceService
&Operation=ItemLookup
&ResponseGroup=Large
&SearchIndex=All
&IdType=ISBN
&ItemId=076243631X
&AWSAccessKeyId=[Your_AWSAccessKeyID]
&AssociateTag=[Your_AssociateTag]
&Timestamp=[YYYY-MM-DDThh:mm:ssZ]
&Signature=[Request_Signature]
I can use PHP SDK for this, I could not find any doc on how to achieve this using SDK.
EDIT
Following this & this links, I have written following code-
<?php
date_default_timezone_set('UTC');
$ItemId = 1603843698;
$ResponseGroup = 'Offers';
//$Timestamp = gmdate("Y-M-DTh:m:sZ");
$Timestamp = gmdate("Y-m-d\TH:i:s\Z");
$AWSAccessKeyId = "EXAMPLEEXAMPLE";
$associateTag = "something-10";
echo $Timestamp;
echo "<br />";
$str = "GET\n
webservices.amazon.com\n
/onca/xml\n
AWSAccessKeyId=".$AWSAccessKeyId."&AssociateTag=".$associateTag."&ItemId=".$ItemId."&Operation=ItemLookup&ResponseGroup=".$ResponseGroup."&Service=AWSECommerceService&Timestamp=".urlencode($Timestamp);
$str = urlencode(base64_encode(hash_hmac("sha256",$str,'my secrete',true)));
$url = "http://webservices.amazon.com/onca/xml?Service=AWSECommerceService&Operation=ItemLookup&ResponseGroup=Offers&IdType=ASIN&ItemId=".$ItemId."&AssociateTag=ebooksprices-20&AWSAccessKeyId=".$AWSAccessKeyId."&Timestamp=".urlencode($Timestamp)."&Signature=".$str;
echo "<br />";
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($ch);
curl_close($ch);
var_dump($result);
But with no success, I get this output-
2016-07-14T09:50:06Z string(427) " SignatureDoesNotMatchThe request
signature we calculated does not match the signature you provided.
Check your AWS Secret Access Key and signing method. Consult the
service documentation for
details.34b23224-4750-46c7-8f75-929239f955de"
Any help will be appreciated.
Thanks
Finally I got time to post my working code-
$ItemId = $_GET['isbn'];
$ResponseGroup = 'Offers';
//$Timestamp = gmdate("Y-M-DTh:m:sZ");
//date_default_timezone_set('America/Los_Angeles');
$Timestamp = gmdate("Y-m-d\TH:i:s\Z");
$AWSAccessKeyId = "/your key/";
$AssociateTag = "/your-tag/";
$Version = "2013-08-01";
//echo $Timestamp;
//echo "<br />";
$str = "Service=AWSECommerceService&Operation=ItemLookup&ResponseGroup=".$ResponseGroup."&IdType=ASIN&ItemId=".urlencode($ItemId)."&AssociateTag=".$AssociateTag."&AWSAccessKeyId=".$AWSAccessKeyId."&Timestamp=".urlencode($Timestamp);
$ar = explode("&", $str);
//var_dump($ar);
natsort($ar);
//var_dump($ar);
$str = "GET
webservices.amazon.com
/onca/xml
";
$str .= implode("&", $ar);
//echo $str;
//echo "<br />";
$str = urlencode(base64_encode(hash_hmac("sha256",$str,'/your secret/',true)));
$url = "http://webservices.amazon.com/onca/xml?Service=AWSECommerceService&Operation=ItemLookup&ResponseGroup=Offers&IdType=ASIN&ItemId=".$ItemId."&AssociateTag=your-tag&AWSAccessKeyId=".$AWSAccessKeyId."&Timestamp=".urlencode($Timestamp)."&Signature=".$str;
//echo "<br />";
//echo $url;
//echo "<br />";
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($ch);
curl_close($ch);
//var_dump($result);
$xml=simplexml_load_string($result) or die("Error: Cannot create object");
echo "<h1>AMAZON API</h1><pre>";
print_r($xml);
This code returns the response in XML format.
Thanks
The AWS SDK's does currently not support the Amazon Product Advertising API.
For implementation there's a PHP code example provided by Amazon on this page: Implementing a Product Advertising API Request
For the request signature, follow this documentation: http://docs.aws.amazon.com/AWSECommerceService/latest/DG/rest-signature.html. More specific details on a PHP implementation of the request signing has been answer here before: Amazon Product API returns “SignatureDoesNotMatch”
To get the search result from google, I used those code. Sometimes it work perfectly but sometimes it don't give any answer. Now don't know what is the problem. I need those result for research purpose so I had to browse for different query
if (isset($_GET['content'])) {
// echo $_GET['content'];
$url_all=NULL;
$visibleurl=NULL;
$title_all=NULL;
$content_all=NULL;
$mainstring=NULL;
$searchTerm=$_GET['content'];
$endpoint = 'web';
$key= 'angelic-bazaar-111103';
$url = "http://ajax.googleapis.com/ajax/services/search/".$endpoint;
$args['q'] = $searchTerm;
$args['v'] = '1.0';
$url .= '?'.http_build_query($args, '', '&');
$url .="&rsz=". 8;
$ch = curl_init()or die("Cannot init");
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$body = curl_exec($ch)or die("cannot execute");
curl_close($ch);
$json = json_decode($body);
for($x=0;$x<count($json->responseData->results);$x++){
$url_all .="#$*##" . $json->responseData->results[$x]->url;
$visibleurl .="#$*##" . $json->responseData->results[$x]->visibleUrl;
$title_all .="#$*##" . $json->responseData->results[$x]->title;
$content_all .="#$*##" . $json->responseData->results[$x]->content;
}
**EDIT
This Code works well sometimes, other times it doesn't, Is it a problem of google or something else. I get this error
$json->responseData->results for this showing "Trying to get property of non-object in"
I am trying to integrate amazon api into my website, so far I have managed to have it navigate to the correct xml part and store that as an variable however i am now trying to insert this varibable into my mysql database and it is giving me this error
Notice: Undefined variable: sql_select in
Fatal error: Call to a member function query() on a non-object
what am i doing wrong?
Entire Code
define('INCLUDED', 1);
error_reporting(E_ALL);
ini_set("display_errors", 1);
$AWS_ACCESS_KEY_ID = "HIDDENFORPRIVACY";
$AWS_SECRET_ACCESS_KEY = "HIDDENFORPRIVACY";
$base_url = "http://free.apisigning.com/onca/xml?";
$url_params = array('Operation'=>"ItemLookup",'Service'=>"AWSECommerceService",
'AWSAccessKeyId'=>$AWS_ACCESS_KEY_ID,'AssociateTag'=>"HIDDENFORPRIVACY",
'Version'=>"2011-08-01",'Availability'=>"Available",'ItemId'=>"0273702440",
'ItemPage'=>"1",'ResponseGroup'=>"EditorialReview", 'Title'=>"Accounting and Finance for Non-Specialists 5th Edition");
// Add the Timestamp
$url_params['Timestamp'] = gmdate("Y-m-d\TH:i:s.\\0\\0\\0\\Z", time());
// Sort the URL parameters
$url_parts = array();
foreach(array_keys($url_params) as $key)
$url_parts[] = $key."=".$url_params[$key];
sort($url_parts);
// Construct the string to sign
//$string_to_sign = "GET\nhttp://free.apisigning.com/".implode("&",$url_parts);
//$string_to_sign = str_replace('+','%20',$string_to_sign);
//$string_to_sign = str_replace(':','%3A',$string_to_sign);
//$string_to_sign = str_replace(';',urlencode(';'),$string_to_sign);
// Sign the request
//$signature = hash_hmac("sha256",$string_to_sign,$AWS_SECRET_ACCESS_KEY,TRUE);
// Base64 encode the signature and make it URL safe
//$signature = base64_encode($signature);
//$signature = str_replace('+','%2B',$signature);
//$signature = str_replace('=','%3D',$signature);
//$signature = str_replace(';',urlencode(';'),$string_to_sign);
$url_string = implode("&",$url_parts);
$url = $base_url.$url_string;
//print $url;
$ch = curl_init(); //this part we set up curl
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 15);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
$xml_response = curl_exec($ch);
curl_close($ch);
header('Content-type: application/xml'); //specify as xml to not display as one long string
echo $xml_response;
$xml = new SimpleXMLElement($xml_response); //time to echo back the correct piece of data
$editorialReview = $xml->Items->Item->EditorialReviews->EditorialReview->Content;
$variable = '<p>Editorial review: '.html_entity_decode($editorialReview).'</p>'."\n";
echo $variable;
$sql_select = mysql_query("INSERT INTO " . DB_PREFIX . "auctions
(amazon_description) VALUES
('" . $variable . "')");
echo ($sql_select);
this does not send errors, but the mysql database shows no change
The error "Undefined variable" indicates that you have not initialized the object.
From the code you printed it seems that $sql_select should be a string which has the select statement.
So you need to do something like :
$variable = '<p>Editorial review: '.html_entity_decode($editorialReview).'</p>'."\n";
echo $variable;
$sql_select .= "INSERT INTO " . DB_PREFIX . "auctions
(amazon_description) VALUES
('" . $variable . "')";
$DB_object->query($sql_select);
Please post the entire code so that the solution can be accurately found out.
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.
I am trying to send this query:
http://api.geonames.org/search?featureCode=PRK&maxRows=10&username=demo&country=US&style=full&adminCode1=AK
To a web service and pull down and parse a bunch of fields there, namely these:
// 1) totalResultsCount
// 2) name
// 3) lat
// 4) lng
// 5) countryCode
// 6) countryName
// 7) adminName1 - gives full state name
// 8) adminName2 - owner of the park.
I am doing this:
$query_string = "http://api.geonames.org/search?featureCode=PRK&maxRows=10&username=demo&country=US&style=full&adminCode1=AK";
Could someone please provide the right code to loop through the results and get values?
Since the response is XML, you can use SimpleXML:
$url = "http://api.geonames.org/search?featureCode=PRK&maxRows=10&username=demo&country=US&style=full&adminCode1=AK";
$xml = new SimpleXMLElement($url, null, true);
echo "totalResultsCount: " . $xml->totalResultsCount . "<br />";
foreach($xml->geoname as $geoname) {
echo $geoname->toponymName . "<br />";
echo $geoname->lat . "<br />";
echo $geoname->countryCode . "<br />";
echo $geoname->countryName . "<br />";
echo $geoname->adminName1 . "<br />";
echo $geoname->adminName2 . "<br />";
}
Which will displays the results as follows:
totalResultsCount: 225
Glacier Bay National Park and Preserve
58.50056
US
United States
Alaska
US.AK.232
...
Firstly, looks like that web service is returning XML rather than JSON. You can use SimpleXML to parse that out.
Secondly, you may want to check out curl
An Example:
$ch = curl_init("http://api.geonames.org/search?featureCode=PRK&maxRows=10&username=demo&country=US&style=full&adminCode1=AK");
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
$content = curl_exec($ch);
curl_close($ch);
fopen will give you a resource, no the file. Since you're doing a json decode, you'll want to just the whole thing as a string. Easiest way to do this is file_get_contents.
$query = 'http://api.geonames.org/search?featureCode=PRK&maxRows=10&username=demo&country=US&style=full&adminCode1=AK';
$response = file_get_contents($query);
// You really should do error handling on the response here.
$decoded = json_decode($response, true);
echo '<p>Decoded: '.$decoded['lat'].'</p>';