Requesting User Info with Foursquare API - php

I am using the Foursquare API to request information for my users.
It says to make a request to get user info like this,
https://api.foursquare.com/v2/users/self?oauth_token=TOKENHERE
So I am doing it like this,
$fsUser = file_get_contents("https://api.foursquare.com/v2/users/self?oauth_token=".$access_token);
When I var_dump I get null from the value I am saving it in, Here is what my full code looks like,
<?php
$client_id = "iwdhwouchweohcuwoehcowehcu";
$secret = "ojdwojwjwrhvo";
$redirect = "http://www.example.com";
if($_GET['code']){
//We need to hit up the authkey URL and get the key in JSON format
$authkey = file_get_contents("https://foursquare.com/oauth2/access_token?client_id=".$client_id."&client_secret=".$secret."&grant_type=authorization_code&redirect_uri=".$redirect."&code=".$_GET['code']);
//We then need to decode it and store that key in a variable
$auth = json_decode($authkey,true);
$access_token = $auth['access_token'];
//we then look up whatever endpoint of the api we want
$fsUser = file_get_contents("https://api.foursquare.com/v2/users/self?oauth_token=".$access_token);
$user = json_decode($fsUser, true);
// $name = $decoded_userinfo['response']['user']['firstName'];
}
?>
When I var_dump $fsUser I get bool(false)
I have var_dump every variable with no issues till I get to $fsUser I can not get past this part...

You also need to use the version number (v) with your request.
Like this
https://api.foursquare.com/v2/venues/search?client_id=CLIENT_ID&v=20140806&m=foursquare
See documentation here.
https://developer.foursquare.com/overview/versioning

Related

Get info from API/URL

I have the URL https://android.rediptv2.com/ch.php?usercode=5266113827&pid=1&mac=02:00:00:00:00:00&sn=&customer=GOOGLE&lang=eng&cs=amlogic&check=3177926680
which outputs statistics.
For example:
[{"id":"2972","name":"MBC 1","link":"http://46.105.112.116/?watch=TR/mbc1-ar&token=RED_cexVeBNZ8mioQnjmGiYNEg==,1643770076.5266113827&t=1&s=2&p=1&c=BR&r=1351&lb=1","epg":"https://epg.cdnrdn.com/MBC1En.ae-20220201.xml","dvr":"disabled","language":"Arabic","category":"TOP 100","logo":"http://files.rednetcontent.com/chlogo/mbc1.png"},{"id":"1858","name":"MBC 2","link":"http://46.105.112.116/?watch=TN/mbc2-ar&token=RED_cexVeBNZ8mioQnjmGiYNEg==,1643770076.5266113827&t=1&s=2&p=1&c=BR&r=1351&lb=1","epg":"https://epg.cdnrdn.com/MBC2En.ae-20220201.xml","dvr":"disabled","language":"Arabic","category":"TOP 100","logo":"http://files.rednetcontent.com/chlogo/mbc2.png"},{"id":"1859","name":"MBC 3","link":"http://46.105.112.116/?watch=TN/mbc3-ar&token=RED_cexVeBNZ8mioQnjmGiYNEg==,1643770076.5266113827&t=1&s=2&p=1&c=BR&r=1351&lb=1","epg":"https://epg.cdnrdn.com/-20220201.xml","dvr":"disabled","language":"Arabic","category":"TOP 100","logo":"http://files.rednetcontent.com/chlogo/mbc3.png"}]
I want to get the value of link count.
Can anyone help?
I tried to do:
<?php
$content = file_get_contents("https://android.rediptv2.com/ch.php?usercode=5266113827&pid=1&mac=02:00:00:00:00:00&sn=&customer=GOOGLE&lang=eng&cs=amlogic&check=3177926680");
$result = json_decode($content);
print_r( $result->link );
?>
But it didn't work.
Put the JSON in an editor and you'll see that it's an array and not an object with the link attribute. This is why you cannot access it directly. You have to loop over the items and then you'll be able to access the link property of one of the items. If you need to access the link by id, as you asked 4 months later, then just create a dictionnary in an array indexed by id and containing just the interesting data you need.
PHP code:
<?php
// The result of the request:
$content = <<<END_OF_STRING
[{"id":"2972","name":"MBC 1","link":"http://46.105.112.116/?watch=TR/mbc1-ar&token=RED_cexVeBNZ8mioQnjmGiYNEg==,1643770076.5266113827&t=1&s=2&p=1&c=BR&r=1351&lb=1","epg":"https://epg.cdnrdn.com/MBC1En.ae-20220201.xml","dvr":"disabled","language":"Arabic","category":"TOP 100","logo":"http://files.rednetcontent.com/chlogo/mbc1.png"},{"id":"1858","name":"MBC 2","link":"http://46.105.112.116/?watch=TN/mbc2-ar&token=RED_cexVeBNZ8mioQnjmGiYNEg==,1643770076.5266113827&t=1&s=2&p=1&c=BR&r=1351&lb=1","epg":"https://epg.cdnrdn.com/MBC2En.ae-20220201.xml","dvr":"disabled","language":"Arabic","category":"TOP 100","logo":"http://files.rednetcontent.com/chlogo/mbc2.png"},{"id":"1859","name":"MBC 3","link":"http://46.105.112.116/?watch=TN/mbc3-ar&token=RED_cexVeBNZ8mioQnjmGiYNEg==,1643770076.5266113827&t=1&s=2&p=1&c=BR&r=1351&lb=1","epg":"https://epg.cdnrdn.com/-20220201.xml","dvr":"disabled","language":"Arabic","category":"TOP 100","logo":"http://files.rednetcontent.com/chlogo/mbc3.png"}]
END_OF_STRING;
$items = json_decode($content);
echo '$items = ' . var_export($items, true) . "\n\n";
// Create a dictionnary to store each link accessible by id.
$links_by_id = [];
// Loop over all items:
foreach ($items as $i => $item) {
// Show how to access the current link.
echo "Link $i = $item->link\n";
// Fill the dictionary.
$links_by_id[(int)$item->id] = $item->link;
}
// To access the first one:
echo "\nFirst link = " . $items[0]->link . "\n";
// Example of access by id:
// The id seems to be a string. It could probably be "1895" or "zhb34" or whatever.
// (If they are only numbers, we could convert the string to an integer).
$id = "1859";
echo "\nAccess with id $id = " . $links_by_id[$id] . "\n";
Test it here: https://onlinephp.io/c/e8ab9
Another important point: You are getting a 403 Forbidden error on the URL you provided. So typically, you will not obtain the JSON you wanted.
As I explained in the comment below, I think that you will not be able to access this page without having a fresh URL with valid query parameters and/or cookies. I imagine you obtained this URL from somewhere and it is no longer valid. This is why you'll probably need to use cURL to visit the website with a session to obtain the fresh URL to the JSON API. Use Google to find some examples of PHP scraping/crawling with session handling. You'll see that depending on the website it can get rather tricky, especially if some JavaScript comes into the game.

Facebook Graph API- Page post fetch in website

I used following code to access facebook page posts.
$accessToken = 'EAAlGssgdgQIBAHuD9ZBZB6RWZClM3bmm8Vjv2nZBNmotTTnubgzdK4aiHbqJwhRlELjAurPEHKSqxJS7c0Pyd5ZBuqZAo2keabbkubx0AZCl3m6brDGlkXNgMq9dtNUZAx4P6QwdsXwNvJaEi2j3YDsHpZABiRxRK6qMAmZAyynLvJNCJ41ZBn9se28QUsDHG72mhZCzHFLpLQUxZCAZDZD';
$id = '109395947376896';
$url = "https://graph.facebook.com/$id/posts?access_token=$accessToken";
$result = file_get_contents($url);
$decoded = json_decode($result, true);
var_dump($decoded);
and get the following result. screenshot is attached
how do I get images of posts??
Without specifying the fields, you will only get default ones. This is how you can get fields (checkout the API docs for Posts about available ones):
$url = "https://graph.facebook.com/$id/posts?access_token=$accessToken&fields=full_picture,message,...";
Source: https://developers.facebook.com/docs/graph-api/reference/post/#fields
You can get them in field parameters
picture
full_picture
child_attachments
Also refer https://developers.facebook.com/docs/graph-api/reference/post/

Get a JSON from url with PHP

Im new to php and tried to get a json object from the twitch API to retrieve one of its values and output it. i.e
i need to get the information from this link: https://api.twitch.tv/kraken/users/USERNAME/follows/channels/CHANNELSNAME
plus i need to to something so i can modify the urls USERNAME and CHANNELSUSERNAME. I want it to be a api to call for howlong user XY is following channelXY and this will be called using nightbots $customapi function.
the date i need from the json is "created_at"
Since we were able to clear out the errorsheres the final PHP file that works if anyone encounters similiar errors:
<?php
$url = "https://api.twitch.tv/kraken/users/" . $_GET['username'] . "/follows/channels/" . $_GET['channel'];
$result = file_get_contents($url);
$result = json_decode($result, true);
echo $result["created_at"];
?>
You have a typo in your code on the first line and you're not storing the result of your json_decode anywhere.
<?php
$url = "https://api.twitch.tv/kraken/users/" . $_GET['username'] . "/follows/channels/" . $_GET['channel'];
$result = file_get_contents($url);
$result = json_decode($result, true);
echo $result["created_at"];
You have to call the page this way page.php?username=yeroise&channel=ceratia in order to output the created_at value for this user and this channel.
In your code you're using 2 different ways to get the content of the page and you only need one (either file_get_contents or using CURL), I chose file_get_contents here as the other method adds complexity for no reason in this case.

How can I make this AJAX call work if you need to be authenticated?

I would like to know how I can make AJAX calls to the Twitter API in order to get pictures of twitter users and display them back to the browser . The thing is that when I use the PHP method , it takes a VERY VERY LONG time to display profile pictures of a 100 twitter users generated by a loop .. So through this URL below that returns information about a twitter user, how could I use it with Jquery AJAX in order to fetch profile pictures images ? This requires the call to be authenticated ..
https://api.twitter.com/1.1/statuses/user_timeline.json?screen_name=twitterapi&count=2
Below is the PHP method I used , but it's SOO SLOW ..
$id = $_GET['screen_name'];
* Create a TwitterOauth object with consumer/user tokens. */
$connection = new TwitterOAuth(CONSUMER_KEY, CONSUMER_SECRET, $access_token['oauth_token'], $access_token['oauth_token_secret']);
foreach($id as $key => $value) {
$url = $connection->get('users/show', array('screen_name' => $value));
$results = json_encode($url);
$data = json_decode($results, true);
$image = '';
if(is_array($data)){
$image = $data['profile_image_url']; ;
$image_bigger = str_replace('_normal', '_bigger',$image);
}
}
?>
Try the users/lookup endpoint which will let you pull 100 profiles at a time.
https://dev.twitter.com/docs/api/1.1/get/users/lookup
Note also that you cannot make pure ajax calls directly to the new Twitter API without exposing your secret keys to the public. You'll have to proxy these calls via your own back end.

eBay API - FindItemsAdvanced call in PHP does not return any response

Im trying to perform a simple call using ebays search API, when I make a call I get no response, and the problem is with the actual call itself.
$endpoint = 'http://open.api.ebay.com/shopping?';
$responseEncoding = 'XML';
$version = '631'; // API version number
$appID = 'asdf3e6e3';
$itemType = "AllItemTypes";
$itemSort = "EndTime";
//find items advanced
$apicalla = "$endpoint"
."callname=FindItemsAdvanced"
."&version=$version"
."&siteid=0"
."&appid=$appID"
."&MaxEntries=10"
."&ItemSort=EndTime"
."&ItemType=AllItemTypes"
."&IncludeSelector=SearchDetails"
."&responseencoding=$responseEncoding";
$resp = simplexml_load_file($apicalla);
this call is the equivalent to
http://open.api.ebay.com/shopping?callname=FindItemsAdvanced&version=631&siteid=0&appid=asdf3e6e3&MaxEntries=10&ItemSort=EndTime&ItemType=AllItemTypes&IncludeSelector=SearchDetails&responseencoding=XML
My question is what am I missing to make this simple search call?
It looks like you're trying to use eBay's Shopping API, specifically the FindItemsAdvanced call which I believe was deprecated quite some time ago and may no longer be functional (I no longer see it in the call reference). What you want to do is use use findItemsAdvanced from eBay's Finding API.
First, you'll need to change your API endpoint & query string parameters a bit (see the aforementioned findItemsAdvanced call reference for the specifics, but I believe it'll look more like this (I haven't touched my findItemsAdvanced calls in at least 6 months, so I haven't tested this):
$endpoint = 'http://svcs.ebay.com/services/search/FindingService/v1?';
$responseEncoding = 'XML';
$version = '1.8.0'; // API version number (they're actually up to 1.11.0 at this point
$appID = 'asdf3e6e3';
$itemSort = "EndTimeSoonest";
//find items advanced
$apicalla = "$endpoint"
."OPERATION-NAME=findItemsAdvanced"
."&SERVICE-VERSION=$version"
."&GLOBAL-ID=EBAY-US"
."&SECURITY-APPNAME=$appID"
//."&MaxEntries=10" // look for an equivalent for this (maybe paginationInput.entriesPerPage?)
."&sortOrder=EndTimeSoonest"
//."&ItemType=AllItemTypes" // not needed AFAICT, otherwise look at itemFilterType
."&descriptionSearch=true";
."& RESPONSE-DATA-FORMAT=$responseEncoding";
$resp = simplexml_load_file($apicalla);
In addition to this, to use findItemsAdvanced, you must specify what you're searching for either by category (categoryId) or by keywords (keywords), hence the "Please specify a query!" error message.
So, you also need to add something like the following (assuming keywords):
$keywords = "something";
$apicalla .= "&keywords=" . urlencode($keywords);
Giving you the following:
$endpoint = 'http://svcs.ebay.com/services/search/FindingService/v1?';
$responseEncoding = 'XML';
$version = '1.8.0'; // API version number (they're actually up to 1.11.0 at this point
$appID = 'asdf3e6e3';
$itemSort = "EndTimeSoonest";
$keywords = "something"; // make sure this is a valid keyword or keywords
//find items advanced
$apicalla = "$endpoint"
."OPERATION-NAME=findItemsAdvanced"
."&SERVICE-VERSION=$version"
."&GLOBAL-ID=EBAY-US"
."&SECURITY-APPNAME=$appID"
//."&MaxEntries=10" // look for an equivalent for this (maybe paginationInput.entriesPerPage?)
."&sortOrder=$itemSort"
//."&ItemType=AllItemTypes" // not needed AFAICT, otherwise look at itemFilterType
."&descriptionSearch=true";
."& RESPONSE-DATA-FORMAT=$responseEncoding"
."&keywords=" . urlencode($keywords);
$resp = simplexml_load_file($apicalla);
One final note: If you want to load further details of specific items that you find in your results, you'll still want to use the Shopping API (specifically the GetSingleItem & GetMultipleItems calls). So, you may ultimately use a mix of the Shopping & Finding APIs.
It should be something like:
<?php
$url = 'http://svcs.ebay.com/services/search/FindingService/v1?OPERATION-NAME=findItemsAdvanced&SERVICE-VERSION=1.11.0&SECURITY-APPNAME=YOUR_APP_ID&RESPONSE-DATA-FORMAT=XML&REST-PAYLOAD&paginationInput.entriesPerPage=2&keywords=ipod&siteid=203&GLOBAL-ID=EBAY-IN';
$xml = file_get_contents( $url );
$xml = simplexml_load_string( $url );
?>
Log-in to your ebay developer account and click on this link: Test your calls with API Test Tool
Hope this helps.

Categories