Facebook API GET recent post ID - php

I'm currently working on a little PHP 'script' that will automatically grab the Post ID from the most recent post on a Facebook page.
I have so far got this:
$status = $facebook->api("/645017715510822/feed?fields=id&limit=1&access_token=".$token, 'GET');
When I run this through my browser as https://graph.facebook.com/645017715510822/feed?fields=id&limit=1&access_token=XXXXXX then it will display the information that I need:
{
"data": [
{
"id": "645017715510822_1484080338478440",
"created_time": "2014-04-07T12:15:32+0000"
}
],
"paging": {
"previous": "https://graph.facebook.com/645017715510822/feed?fields=id&limit=1&access_token=XXXXXX&since=1396872932&__previous=1",
"next": "https://graph.facebook.com/645017715510822/feed?fields=id&limit=1&access_token=XXXXXX&until=1396872931"
}
}
What I am needing is for it to then grab only the 'id' and possibly print it?
My knowledge of PHP isn't wonderful and I've already tried searching related posts on here, but can't seem to find anything for my exact request, any assistance would be greatly appreciated.
Thanks.

Try this-
if( !empty($status['data']) )
{
foreach($status["data"] as $s)
{
$id = $s["id"];
}
}
else
{
//error
}

Related

Facebook graph api comments pagination

I'm trying to get all the comments of post by using the following api url request:
$fb->get($yourPage.'/feed?fields=comments.limit(100)&limit=25&since=2017-7-01&until=2017-7-31', $accessToken);
But some posts have more than 500 comments which yields pagination '(next)' in the api response. Now I want to retrieve all the comments, so how do I paginate through the "comments" because I know how to paginate through posts through this code:
$response = $fb->get($yourPage.'/feed?fields=comments.limit(500)&limit=25&since=2017-7-01&until=2017-7-31', $accessToken);
$comments = $response->getGraphEdge();
$totalcomments = array();
if ($fb->next($comments)) {
//Do something if there is pagination in the posts
}
So how do I do the same for comments? I've searched alot on stack and google but no questions point to this... Thanks
Each comments object includes a paging object for that:
{
"comments": {
"data": [
{
"created_time": "2017-09-01T12:06:19+0000",
"from": {
"name": "xxx",
"id": "1234"
},
"message": "xxx",
"id": "1234"
},
...
],
"paging": {
"cursors": {
"before": "NzkZD",
"after": "NzgZD"
},
"next": "https://graph.facebook.com/v2.10/cccccc/comments?access_token=xxx&pretty=0&limit=2&after=NzgZD"
}
},
"id": "xxx"
}
For every post you get, you need a subroutine (for example, with a recursive function) to use the "next" parameter in a separate API call to get all comments.

CodeIgniter: Select the same like parameter on JSON list

I'm trying to use tokeninput from jQuery Token input, but the data is from the API. I already got the data from API and made a JSON list (see below).
When a user inputs in my token input, it will select from the JSON list, like user/auto_unit?queryParam=q for example. It already gets the user input correctly, but it still returns all data, even those that do not match the user input.
What I want is when the user searches for "Sosiologi", the only values that would show are those string which have "sosiologi" in them.
Is it possible to get only the same values and how can I do that? Thanks in advance!
My JSON list:
// 20170401095401
// http://exp.uin-suka.ac.id/aspirasi/user/auto_unit?queryParam=Filsafat%20Agama
[
{
"id": "UA000001",
"name": "Filsafat Agama"
},
{
"id": "UA000002",
"name": "Perbandingan Agama"
},
{
"id": "UA000003",
"name": "Ilmu Al-Qur'an dan Tafsir"
},
{
"id": "UA000004",
"name": "Sosiologi Agama"
},
{
"id": "UA000005",
"name": "Matematika"
},
{
My JSON code to get the list
function auto_unit() {
$data['unit'] = $this->m_simpeg->getAllUnit();
foreach ($data['unit'] as $key ){
$row['id']= $key['UNIT_ID'];
$row['name']= $key['UNIT_NAMA'];
$row_set[] = $row;
}
echo json_encode($row_set);
}
Model to get API M_simpeg.php:
public function getAllUnit(){
return $this->s00_lib_api->post_api(
1001, 1, null,
URL_API_SIMPEG.'simpeg_mix/data_view'
);
}
Check your server side code as it is not filtering the array.
Codeigniter sample code
<?php
function filter(){
$queryParam=$this->input->get('queryParam');
$res=$array.filter($queryParam);
return $res;
}
?>

How to add multiple track to stream in alexa?

I am working on alexa for the first time and I am developing a music app. I need to add multiple track of one artist and play it continuously. I am unable to do so. However, one song is working properly but unable to add and play multiple song.
Here is my code,
$response = '{
"version" : "1.0",
"response" : {
"outputSpeech": {
"type": "PlainText",
"text": "Playing song for Acon"
},
'.$card.',
"directives": [
{
"type": "AudioPlayer.Play",
"playBehavior": "REPLACE_ALL",
"audioItem": {
"stream": {
"token": "track1",
"url": "https://p.scdn.co/mp3-preview/9153bcc4d7bef50eb80a809fa34e694f2854e539?cid=null",
"offsetInMilliseconds": 0
}
}
}
],
"shouldEndSession" : true
}
}';
You need to wait for the AudioPlayer.PlaybackNearlyFinished request from Alexa. At that point, you can enqueue the next track to be played. It will come near the conclusion of the playback of the currently playing track.
Information on it is here:
https://developer.amazon.com/public/solutions/alexa/alexa-skills-kit/docs/custom-audioplayer-interface-reference#playbacknearlyfinished-request
When replying with a directive to PlaybackNearlyFinished, be sure that:
You set the playBehavior to ENQUEUE
This will cause the next track to start after the current one finishes
You do NOT include the outputSpeech field
outputSpeech is not allowed when out of session. The session ends when the first stream begins playback.
This blog post that I wrote goes into more detail on approaches to developing and testing for the AudioPlayer interface:
https://bespoken.tools/blog/2016/10/10/unit-testing-alexa-skills
Follow the above doc:
https://developer.amazon.com/public/solutions/alexa/alexa-skills-kit/docs/custom-audioplayer-interface-reference#playbacknearlyfinished-request
and then do this to enqueue the second song
In PHP, the way you are handling other requests, you can handle AudioRequest too. For example.
$data = file_get_contents("php://input");
$jsonData = json_decode($data);
if($jsonData->request->type === "AudioPlayer.PlaybackNearlyFinished")
{
$response = '{
"version" : "1.0",
"response" : {
"directives": [
{
"type": "AudioPlayer.Play",
"playBehavior": "ENQUEUE",
"audioItem": {
"stream": {
"token": "track2",
"expectedPreviousToken": "track1",
"url": "Your URL",
"offsetInMilliseconds": 3
}
}
}
],
"shouldEndSession" : true
}
}';
echo $response;
}
This is the way you can handle all the AudioRequest.

PHP Trying to get property of non-object

First of all i know stackoverflow is full from this kind of erros but none of them is like mine so im posting this.
Im trying to get a JSON response from the api and as im trying to echo it im getting the Trying to get property of non-object error.
$apicalldata = file_get_contents("https://api.digitalocean.com/v1/droplets/?client_id=6356465465363546&api_key=f9a702abe442198a4168346435366436cc4cd2138dfc");
$call = json_decode($apicalldata);
echo $call->droplets->id;
This is the code im using. From this i'm expecting a response like this:
{
"status": "OK",
"droplets": [
{
"id": 100823,
"name": "test222",
"image_id": 420,
"size_id":33,
"region_id": 1,
"backups_active": false,
"ip_address": "127.0.0.1",
"private_ip_address": null,
"locked": false,
"status": "active",
"created_at": "2013-01-01T09:30:00Z"
}
]
}
Any ides why am i having this problem? Also is the $call->droplets->id correct?
Thanks for your time
It looks like droplets is an array. Give this a try.
$droplets = $call->droplets;
$myDroplet = $droplets[0];
$myDropletID = $myDroplet->id;
echo $myDropletID;
* Update after your comment *
$droplets = $call->droplets;
foreach($droplets as $droplet)
{
$dropletID = $droplet->id;
echo $dropletID;
}
try this
$call = json_decode($apicalldata);
foreach($call->Droplet as $adm)
{
echo "ID ".$adm->id."<br/>";
}

Error on Restler response in Restangular

I've been at this for a couple days and can't seem to get this to work. My issue is that I'm using Restler (version 3) for an API and Restangular on my front end and I'm getting the following error:
Error: can't convert undefined to object restangularizeBase#http://localhost/vendor/restangular/src/restangular.js:436 restangularizeCollection#http://localhost/vendor/restangular/src/restangular.js:552 createServiceForConfiguration/fetchFunction/<#http://localhost/vendor/restangular/src/restangular.js:610 Qc/e/j.promise.then/h#http://localhost/vendor/angular/angular.min.js:78 Qc/g/<.then/<#http://localhost/vendor/angular/angular.min.js:78 e.prototype.$eval#http://localhost/vendor/angular/angular.min.js:88 e.prototype.$digest#http://localhost/vendor/angular/angular.min.js:86 e.prototype.$apply#http://localhost/vendor/angular/angular.min.js:88 e#http://localhost/vendor/angular/angular.min.js:95 p#http://localhost/vendor/angular/angular.min.js:98 Yc/</t.onreadystatechange#http://localhost/vendor/angular/angular.min.js:99
Here are the relevant code snippets that so you can see what I'm doing:
Restler setup to set up my API
$r = new Restler();
$r->addAPIClass('User');
$r->handle();
User class object I'll be accessing for my API (for now I'm just returning an example for testing)
class User{
public function index() {
return array(
array(
'first_name'=>'John',
'last_name'=>'Smith',
'role'=>'supervisor',
),
array(
'first_name'=>'Matt',
'last_name'=>'Doe',
'role'=>'employee',
),
);
}
}
Finally my app.js file
'use strict';
var app = angular.module('cma',['restangular']);
app.config(function(RestangularProvider) {
RestangularProvider.setBaseUrl('/api');
RestangularProvider.setExtraFields(['name']);
RestangularProvider.setResponseExtractor(function(response,operation) {
return response.data;
});
});
app.run(['$rootScope','Restangular',function($rootScope,Restangular) {
var userResource = Restangular.all('session');
$scope.test = userResource.getList(); // This is where the error is happening
}]);
The API is returning the following JSON response (taken from Firebug:
GET http://localhost/api/user 200 OK 96ms
):
[
{
"first_name": "John",
"last_name": "Smith",
"role": "supervisor"
},
{
"first_name": "Matt",
"last_name": "Doe",
"role": "employee"
}
]
I can't see anything that is going on that would be causing an issue. Any help would be greatly appreciated!
I'm the creator of Restangular :)
The problem is that you're using a responseInterceptor and actually returning an array.
So, your server is returning an array. Your responseInterceptor gets it and then returns the data variable of your array. As that's undefined, undefined is sent to Restangular and therefore you get that error.
Remove the responseInterceptor and everything will start working :).
Bests

Categories