yahoo weather API PHP authorization - php

In yahoo developer guide is example how to retrieve some info about weather
$BASE_URL = "http://query.yahooapis.com/v1/public/yql";
$yql_query = 'select wind from weather.forecast where woeid in (select woeid from geo.places(1) where text="chicago, il")';
$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);
var_dump($phpObj);
this example has limit 2000 request per day, to increase limits I must use API client key and secret KEY.
How I can do authorization ? It's possible in curl or ? only by OAuth 2.0

Related

yahoo weather api returns null

I'm working on yahoo weather system but yahoo api returns null result.
This code I get from here: https://developer.yahoo.com/weather/#php
$BASE_URL = "http://query.yahooapis.com/v1/public/yql";
$yql_query = 'select * from weather.forecast where woeid in (SELECT woeid FROM geo.places WHERE text=('.$time->latitude.','.$time->longitude.'))';
$yql_query_url = $BASE_URL . "?q=" . urlencode($yql_query) . "&format=json&diagnostics=true&callback=";
// 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);
var_dump($phpObj);
When I enter this url in browser then it returns required result.
valid result return weather system correctly
https://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20weather.forecast%20where%20woeid%20in%20(SELECT%20woeid%20FROM%20geo.places%20WHERE%20text=%22(40.7141667,-74.0063889)%22)&format=json&diagnostics=true&callback=
Yahoo weather APIs are being retired.
According to https://developer.yahoo.com/weather ...
Important EOL Notice
The weather.yahooapis.com and fallback endpoints are being retired. We
will no longer be providing free Weather API services for public
users. Please contact yahoo-weather-ydn-api#oath.com if you have any
questions, comments, or interest in supported paid services.
I think you are missing "near the WHERE text=
Replace
$yql_query = 'select * from weather.forecast where woeid in (SELECT woeid FROM geo.places WHERE text=('.$time->latitude.','.$time->longitude.'))';
with
$yql_query = 'select * from weather.forecast where woeid in (SELECT woeid FROM geo.places WHERE text="(' . $time->latitude . ',' . $time->longitude . ')")';
if you get Curl error: SSL certificate problem: unable to get local issuer certificate. then use
curl_setopt($session, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($session, CURLOPT_SSL_VERIFYPEER, 0);

yahoo import contacts through yql giving error

I am trying to import yahoo contacts with help of YQL but stuck with a problem.
I am using approach on this link https://developer.yahoo.com/yql/guide/yql-code-examples.html#yql_php
But I got this error:
{"error":{"lang":"en-US","description":"Authentication Error. The table social.contacts requires a higher security level than is provided, you provided ANY but at least USER is expected"}}
Do I am missing something
I have run this query on yql console its showing result but nt getting results directly hitting url or with curl.
My code is :
$BASE_URL = "https://query.yahooapis.com/v1/public/yql";
// Form YQL query and build URI to YQL Web service
$yql_query = "select * from social.contacts(0, 500) where guid=me";
$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);
// Confirm that results were returned before parsing
if(!is_null($phpObj->query->results)){
// Parse results and extract data to display
foreach($phpObj->query->results->event as $event){
$events .= "<div><h2>" . $event->name . "</h2><p>";
$events .= html_entity_decode(wordwrap($event->description, 80, "<br/>"));
$events .="</p><br/>$event->venue_name<br/>$event->venue_address<br/>";
$events .="$event->venue_city, $event->venue_state_name";
$events .="<p><a href=$event->ticket_url>Buy Tickets</a></p></div>";
}
}
// No results were returned
if(empty($events)){
$events = "Sorry, no events matching $query in $location";
}
// Display results and unset the global array $_GET
echo $events;
unset($_GET);

Get Session token using STS AWS using PHP Codeigniter

I'm new to AWS can anyone please help me how to generate session token using STS API to upload files to S3
Brief:
I went through AWS documentation and researched on Google I have found below library for codeigniter to upload files to S3
https://github.com/psugand/CodeIgniter-S3
It is working fine and I'm able to upload files using my Access ID and secret key. But our requirement is to generate get temporary credentials from Amazon and send to iOS developers so that they can upload files directly to S3. I found below link on Amazon documentation where I need to follow 4 Tasks to get the temporary credentials.
http://docs.aws.amazon.com/general/latest/gr/sigv4-create-canonical-request.html
But some how response always says signature that I'm creating is not matching. Below is my code and response from Amazon. If I'm doing anything wrong please help me.
$AWSAccessKeyId = "AKIAIK4R57JLAFN4Z5SA";
$SecretAccessKey = AMAZON_SECRET_KEY;
$region = 'us-east-1';
$service = 'sts';
$term = 'aws4_request';
$Timestamp = gmdate('D, d M Y H:i:s') . ' GMT';
$date = gmdate('Ymd\THis\Z');
$credentialsScope = gmdate('Ymd').'/'.$region.'/'.$service.'/'.$term;
$credentials = $AWSAccessKeyId.'/'.$credentialsScope;
Task 1 Canonical Request
$CanonicalRequest =
'POST'."\n".
"/"."\n".
'Action=GetSessionToken&Version=2011-06-15&Name=Aasim&X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Expires=3600&X-Amz-Credential='.$credentials.'&X-Amz-Date='.$date.'&X-Amz-SignedHeaders=content-type%3Bhost%3Bx-amz-date'."\n".
'content-type:text/xml;charset=utf-8'."\n".
'host:sts.amazonaws.com'."\n".
'x-amz-date:'.$Timestamp."\n"."\n".
'content-type;host;x-amz-date'."\n".
hash("sha256",'UNSIGNED-PAYLOAD')."\n";
$hashedRequestPayload = hash("sha256",$CanonicalRequest);
Task 2 creating String-to-sign
$StringToSign =
'AWS4-HMAC-SHA256'."\n".
$date."\n".
$credentialsScope."\n".
$hashedRequestPayload;
Task 3 Calculating Signature
$kDate = hash_hmac('sha256', 'AWS4'.$SecretAccessKey, gmdate('Ymd'));
$kRegion = hash_hmac('sha256', $kDate,'us-east-1');
$kService = hash_hmac('sha256', $kRegion,'sts');
$kSigning = hash_hmac('sha256', $kService,'aws4_request');
$Signature = hash_hmac('sha256',$kSigning, $StringToSign);
Task 4 Add the Signing Information to the Request
$querystring = 'Action=GetSessionToken';
$querystring .= '&Version=2011-06-15';
$querystring .= '&X-Amz-Algorithm=AWS4-HMAC-SHA256';
$querystring .= '&X-Amz-Credential='.$credentials;
$querystring .= '&X-Amz-Date='.$date;
$querystring .= '&X-Amz-Expires=3600';
$querystring .= '&X-Amz-SignedHeaders=content-type%3Bhost%3Bx-amz-date';
$querystring .= '&X-Amz-Signature='.$Signature;
Executing Curl Request
$endpoint = 'https://sts.amazonaws.com/?'.$querystring;
$headers = array(
'x-amz-date:'.$Timestamp,
'host:sts.amazonaws.com',
'content-type: text/xml;charset=utf-8',
);
$session = curl_init($endpoint); // create a curl session
curl_setopt($session, CURLOPT_POST, true); // POST request type
curl_setopt($session, CURLOPT_RETURNTRANSFER, true); // return values as a string - not to std out
curl_setopt($session, CURLOPT_HTTPHEADER, $headers);
$responseXML = curl_exec($session);
print_r($responseXML);
Response from Amazon
<ErrorResponse xmlns="https://sts.amazonaws.com/doc/2011-06-15/">
<Error>
<Type>Sender</Type>
<Code>SignatureDoesNotMatch</Code>
<Message>The 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.
The Canonical String for this request should have been
'POST
/
Action=GetSessionToken&Version=2011-06-15&X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Credential=AKIAIK4R57JLAFN4Z5SA%2F20141231%2Fus-east-1%2Fsts%2Faws4_request&X-Amz-Date=20141231T065554Z&X-Amz-Expires=3600&X-Amz-SignedHeaders=content-type%3Bhost%3Bx-amz-date
content-type:text/xml;charset=utf-8
host:sts.amazonaws.com
x-amz-date:Wed, 31 Dec 2014 06:55:54 GMT
content-type;host;x-amz-date
e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855'
The String-to-Sign should have been
'AWS4-HMAC-SHA256
20141231T065554Z
20141231/us-east-1/sts/aws4_request
c3ec81de483674a1bf52b60307ae36a4b5e00cff6c85a30f07cc5d00eeb0d699'
</Message>
</Error>
<RequestId>15cb2945-90ba-11e4-829d-1362e6783c1f</RequestId>
</ErrorResponse>
I'm using codeiginter for this. Please help me . Thanks in advance

YQL API Request Private Data Authentication Error

I am using YQL to pull some data for my yahoo fantasy football league. I have created the app and it gave me a consumer/secret key but how do I pass this information to the yahoo api to log me in? I know I have to use OAuth but I am not sure how to use the key in tandem with CURL and YQL
Here is my PHP Code:
<pre>
<?php
$BASE_URL = "https://query.yahooapis.com/v1/public/yql";
// Form YQL query and build URI to YQL Web service
$yql_query = "select * from fantasysports.leagues.standings where league_key='331.l.777400'";
$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);
var_dump($phpObj);
?>
</pre>
and the error I get is
"Authentication Error. The table fantasysports.leagues.standings
requires a higher security level than is provided, you provided ANY
but at least USER is expected"

How to use twitter api with "http basic auth"?

How to use twitter api with "http basic auth"?
I think I should use the "consumer key"! because twitter gave you limit rate of requests per hour, how can they count my requests if I didn't use my consumer key?
Whenever you want to use HTTP basic auth with anything, if you want to ignore the actual implementation and HTTP headers, just use cURL. Here's a simple example in PHP, cURL is available in other languages too:
<?php
$ch = curl_init();
// Sets the URL cURL will open
curl_setopt($ch, CURLOPT_URL, 'http://twitter.com/statuses/user_timeline.xml?screen_name=al3x');
// Here's the HTTP auth
// The 3rd argument is your Twitter username and password joined with a colon
curl_setopt($ch, CURLOPT_USERPWD, 'username:password');
// Makes curl_exec() return server response
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
// Lately the Twitter API expects an Expect header. It's a mystery
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Expect:'));
// And here's the result XML
$twitter_xml = curl_exec($ch);
curl_close($ch);
?>
And then $twitter_xml will contain the XML of al3x's public timeline. As far as rate limiting goes, ceejayoz already answered that pretty well.
Authenticated API GET requests are counted against your user account's tally.
Unauthenticated API GET requests (permitted by some methods) are counted against your IP address's tally.
POST requests are not rate limited.
More details are available in the Twitter docs.
I would think that they count the requests from the same IP - but I haven't double checked.
Even dynamic IP address will be static for a session and, in the absence of any other identifying information, the only thing that will distinguish you from other users.
As other posters have said - there are better methods if you've got a key.
$twitter = file_get_content("http://user:password#twitter.com/blabla");
more about native HTTP Wrapper support in PHP
I recently wrote some PHP to post to Twitter
This is the working part of it:
$message = 'A new revision (#' . $data['revision'] . ') was commited by ' . $data['author'] . ': ' . $data['message'] . "";
$message = substr($message, 0, 140);
$content = 'status=' . urlencode($message);
$packetString = "POST /statuses/update.xml HTTP/1.1\r\n";
$packetString .= "Authorization: Basic " . base64_encode($username . ":" . $password) . "\r\n";
$packetString .= "Content-Length:" . strlen($content) . "\r\n";
$packetString .= "HOST: twitter.com\r\n";
$packetString .= "\r\n" . $content . "\r\n";
$sock = fsockopen('twitter.com', 80);
fwrite($sock, $packetString);
//This is some logging, to a local file so I can monitor local what's going on
$response = fread($sock, 10240);
fwrite($fh, $packetString . "\r\n\r\n\r\n" . trim($response) . "\r\n\r\n\r\nD:\r\n" . $d);
fclose($fh);
You can see it in action here: http://twitter.com/fmsvn using a callback from our SVN server I am posting the SVN messages to the projects Twitter Feed.

Categories