Using Foursquare API it returns me the following JSON feed
{"meta":{"code":200},"notifications":[{"type":"notificationTray","item":{"unreadCount":1}},{"type":"message","item":{"message":"OK! We've got you # Place XXX. You've been here 2 times."}},{"type":"insights","item":{"insights":{"count":4,"items":[{"type":"pointsReward","image":"https://foursquare.com/img/points/discovery-venue3.png","title":"First of your friends to check into Place XXX!","shareable":false,"points":{"image":{"prefix":"https://foursquare.com/img/points/discovery-venue3_","sizes":[44,60,120],"name":".png"},"message":"First of your friends to check into Place XXX!","points":5}},{"type":"pointsReward","image":"https://foursquare.com/img/points/discovery-venue3.png","title":"First time at Place XXX.","shareable":false,"points":{"image":{"prefix":"https://foursquare.com/img/points/discovery-venue3_","sizes":[44,60,120],"name":".png"},"message":"First time at Place XXX.","points":5}},{"type":"pointsReward","image":"https://foursquare.com/img/points/category-outdoor.png","title":"First Plaza check-in of your friends!","shareable":false,"points":{"image":{"prefix":"https://foursquare.com/img/points/category-outdoor_","sizes":[44,60,120],"name":".png"},"message":"First Plaza check-in of your friends!","points":6}},{"type":"pointsReward","image":"https://foursquare.com/img/points/discovery-venue3.png","title":"First of your friends to check in here.","shareable":false,"points":{"image":{"prefix":"https://foursquare.com/img/points/discovery-venue3_","sizes":[44,60,120],"name":".png"},"message":"First of your friends to check in here.","points":2}}]}}},{"type":"leaderboard","item":{"total":18,"scores":[{"icon":"https://foursquare.com/img/points/discovery-venue3.png","message":"First of your friends to check into...
What I would like to do is to display the message "OK! We've got you # Place XXX. You've been here 2 times." which is embedded in the second "type" tag
I'm used to do get this kind of value using a foreach function but with this one I cannot get it.
I need help from an expert, thanks for your participation.
Code Im trying to use without success:
$data = json_decode($response, true); // return array not object
foreach($data['notifications']['type'][0] as $item) {
echo $item['item']['message'];
}
notifications is an array, so you can't hash into it with 'type' like you are trying to do. You should iterate through $data['notifications'] until you find an object with a "type" property of "message". Then dig into that object's "item" and get its "message". For more details about the notifications field in a response, see https://developer.foursquare.com/docs/responses/notifications.
In the sample JSON you provided, to directly access the content you want, you could use $data['notifications'][1]['item']['message']. The 1 may change depending on what API version you're passing in, but should be relatively stable as long as you keep using the same version.
Related
I'm trying to build a page that use API from another site. I'm kind noob on this so my question can be silly.
This site have a bunch of category and on each of this category there are different fields that have to be filled so you can post an item.
So instead of me creating a page for each category, and try to pass a get that is already available on their API get the variable that have Required as it value so I can pass it to my code to fill it the right way.
So if I pass:
curl -X GET -H 'Authorization: Bearer $ACCESS_TOKEN' https://api.mercadolibre.com/categories/MLA1055
It returns me a lot of results, and on some of it are:
"shipping_profile": "optional",
"show_contact_information": false,
"simple_shipping": "optional",
"stock": "required"
So my idea is if there is a way to get the the name of the result that will be stock. So I can put it on my code so the user can fill it and I don't have to do category by category...
Like I said before. I'm noob at PHP and cURL, but I have some experience with coding, is just a new language, hope you guys can help me.
First, you would have to convert the JSON string from API into an object understood by PHP, we have json_decode() for this:
//let's assume JSON data has been downloaded and stored in data.json file
$jsonString = file_get_contents(dirname(__FILE__) . '/data.json');
$jsonObject = json_decode($jsonString, true);
Next, from the URL you gave in the comment, I can see that the "required" data is stored inside settings key. You can use JSON viewer tools to visualize the data.
I'm using online JSON viewer tools
Extract it to another variable, or just loop through the elements:
$arrayRequired = array();
foreach($jsonObject['settings'] as $key => $setting){
if($setting === 'required'){
$arrayRequired[] = $key;
}
}
Your required keys now stored inside $arrayRequired variable:
print_r($arrayRequired);
Array
(
[0] => immediate_payment
[1] => price
[2] => stock
)
That's it.
I'm having trouble using the API to view a segment. I'm using PHP. My code is as follows:
$url = "https://us7.api.mailchimp.com/export/1.0/list/?apikey=$api_key&id=$list_id&$segment";
$result = file_get_contents($url);
where $segment is an array used to generate the segment.
I can get the following version working...
$segment = "segment[match]=all"
"&segment[conditions][0][field]=LNAME".
"&segment[conditions][0][op]=like".
"&segment[conditions][0][value]=smith";
which generates a segment of all subscribers to my list with a last name containing "smith".
However, what I actually want to do is generate a segment based on people who've been sent any mailer in the last 7 days. I've created this manually to get the correct fields and generated the following array based on it:
$segment = "segment[match]=all".
"&segment[conditions][0][condition_type]=Aim".
"&segment[conditions][0][field]=aim".
"&segment[conditions][0][op]=sent".
"&segment[conditions][0][value]=last7day";
but it's giving me a 300 error. This isn't listed in MailChimp's current error glossary on the export api documentation, but according to a snapshot of the old error documentation means "Campaign doesn't exist" (https://web.archive.org/web/20130209144951/https://apidocs.mailchimp.com/api/1.3/exceptions.field.php) which is weird because I'm not referencing a campaign.
What am I doing wrong?
// , Click on playground and with your API key.
Generate some responses and look at the campaign id.
It's not the integer value you see when hovering over the campaign with id=, it's another value.
Look for the id that the API needs in the response.
This will fix your issue.
I just found out that Facebook updated their graph api and now require an access token to retrieve like counts for a page to be displayed on an external website. I went through the motions of setting up an app to get the token and have the following:
$fbData = json_decode(file_get_contents('https://graph.facebook.com/myFacebookName?access_toke=MyAppId|MySecretToken'));
print_r($fbData);
It seems to only be returning objects:
stdClass Object ( [name] => My Facebook Name [id] => id number )
And that is all, giving me nothing to parse through. The name and id coming through are correct so there is a connection happening. Note that I have edited out some information so MyFacebookName and MyAppId|MySecretToken are actually populated with the correct info. Any ideas how to get the full JSON list to grab page likes? Am I missing something?
UPDATE
Thanks to Tobi and further reading the graph api, documentation I was able to get the number of likes with with following:
$fbData = json_decode(file_get_contents('https://graph.facebook.com/name?fields=likes&access_token=appId|accessToken'));
<?php echo number_format($fbData->likes);?>
You need to specifically request the fields you want in the response. In your case this would be
https://graph.facebook.com/myFacebookName?fields=id,name,likes&access_toke=MyAppId|MySecretToken
I don't understand why you say there "nothing to parse", because the object IS returned and can be used. So what's the problem?
Using echo $fbData['likes'] should give you the number of likes.
See
https://developers.facebook.com/docs/graph-api/using-graph-api/v2.4#fields
https://developers.facebook.com/docs/graph-api/reference/page#Reading
I would like to use the Google Sheets API Batch Update because the calls to update individual cells are extremely slow. The problem is that cells that have no data do not come back as part of a cell feed, so I do not have the necessary edit URL to include those cells in the Batch Update. I have seen reference to a "return-empty" parameter in the Google .NET API which will supposedly return all cells, even empty cells, in a cell feed, but I cannot find any reference to that for the "Protocol" API I am using in PHP (here is an example of a reference to the "return-empty" parameter: Writing to an empty cell in Google Spreadsheets). Does anyone know how to get the cell feed request to return all cells (including empty cells)? Or am I doomed to using the abysmally slow cell update requests?
Quick Hack
If you're using the same API I am, then in googlespraedsheet/api.php at line 152 after
$cellRangeCriteriaQuerystringList = [];
You can add:
$cellRangeCriteriaQuerystringList[] = "return-empty=true";
Better Way
There's a better way, which I have added in my own fork. I created a pull request.
If you work with my fork (or if the pull request is accepted), then you can just add 'returnEmpty' => true to your $cellRange. For example:
$cellList = $spreadsheetAPI->getWorksheetCellList(
$spreadsheetKey,
$worksheetID,
['rowStart' => $rowStart,
'rowEnd' => $rowEnd,
'returnEmpty' => true]);
I need a way to display videos from a specific channel on a page using PHP.
I have authenticated my app and I can use some methods using the advanced API. I am using the official vimeo PHP library to connect.
Below is what I am trying to do and when I dump the array I do not get anything. I can get info back from using get videos from the entire account method.
require_once('/url/vimeo/vimeo.php');
$vimeo = new phpVimeo('number', 'number');
$vimeo->setToken('number','numbers');
$videos = $vimeo->call('vimeo.channels.getVideos', array('ACCOUNT' => 'NAME'));
If I put the channel name where ACCOUNT is I will get an invalid signature error.
Is it worth using something like simple HTML parser for PHP and doing it that or worth sticking with the advanced API?
I would highly advise using the advanced api. If you parse the html, it will break any time vimeo changes their channel pages. Additionally, channels have more than one layout
eg: vimeohq and nicetype
The second parameter of the "call" function should be any querystring parameters the api method requires.
In the case of "vimeo.channels.getVideos" you can provide
channel_id
user_id
page
per_page
summary_response
full_response.
To experiment with the getVideos method, you can use the playground.
So in the end, I believe you want the function to look like this..
$videos = $vimeo->call('vimeo.channels.getVideos', array('channel_id' => 'NAME'));
where NAME is either the channel id, or the channel name (the channel name matches the url slug, so for example "nicetype" not "nice type"