Facebook API, PHP SDK getRequestForNextPage() - php

i'm developing a web app (with PHP SDK) that basically take user posts of last year and do some kind of analysis on it.
Untill now i used to load the data with a recursive function that everytime get the content from the paging link and call itself again with that data.
i've just discovered that i can use getRequestForNextPage(); so im tring to use this function but i got some troubles , seems that getRequestForNextPage() do not respect request's parameters. I know my explanation is pretty confused, here a simplified version of my code:
$thedate= strtotime('now ,-1 year');
$today = strtotime('now');
$richiesta ="/me/posts?since=".$thedate."&until=".$today;
$request = new FacebookRequest( // richiedo i miei statuses
$session,
'GET',
$richiesta
);
$respons = $request->execute();
function looper($response){
$graphObject = $response->getGraphObject();
$nextPageRequest = $response->getRequestForNextPage();
$respy = $nextPageRequest->execute();
$x = $graphObject->getProperty('data');
$y = $x->asArray();
$counter = 0 ;
foreach ($y as $el){
//do something
}
if ( $respy != false ){
looper($respy);
} else {
return;
}
}
looper($respons);`
it seems that that function just keep to iterate even if the posts are older than 1 year.
do getRequestForNextPage() just dont care about request parameters? it just check if there is a "next" paging link in the graph object? Thanks in advance for any suggestion

Related

Navigating forms and grabbing DOM-Elements from a website using Guzzle/Goutte (PHP, Debugging)

I'm fairly new to PHP and playing around with Goutte/Guzzle to grab some basic information from a website after filling out a few forms.
However I have problems finding issues (there might be a ton of them) since I could not find a way to display or console log any of the results or problems. The script finishes with Code 0 but does not return anything. A tip on how to print out what is currently stored in $client would already go a long way.
Here is the whole code I'm trying to run with a bunch of comments for clarification. I'm sorry for using such a large block, but any of this could have issues.
<?php
use Goutte\Client;
use GuzzleHttp\Client as GuzzleClient;
class grabPlate
{
// WKZ
public function checkPlate
{
$goutteClient = new Client();
$guzzleClient = new GuzzleClient(array(
'cookies' => true,
'timeout' => 60,
));
$goutteClient->setClient($guzzleClient);
$crawler = $goutteClient->request('GET', 'https://kfz-portal.berlin.de/kfzonline.public/start.html?oe=00.00.11.000000');
//Click the first "Start" in the top left
$link = $crawler
->filter('a:contains("Start")')
->eq(0)
->link()
;
$crawler = $client->click($link);
//Check checkbox, fill in name and press the button
$buttonCrawlerNode = $crawler->selectButton('Weiter');
$form = $buttonCrawlerNode->form();
$form['gwt-uid-1']->tick();
$form['select2-hidden-accessible']->select('Herr');
$form['gwt-uid-4'] = 'John';
$form['gwt-uid-5'] = 'Doe';
$client->submit($form);
//Fill some Data into the forms and search
$buttonCrawlerNode = $crawler->selectButton('Button-3616');
$form = $buttonCrawlerNode->form();
$form['.kfzonline-KennzeichenGrossEb'] = 'AB';
$form['.kfzonline-KennzeichenGrossEn'] = '123';
$client->submit($form);
//Extract collection
$info = $crawler->extract('.collection');
//return 1 if something is inside collection, 0 if it's empty
if($info == NULL) {
return 1;
} else {
return 0;
}
}
}
?>
As I said just running the script in PHPStorm returns the status 0. However when plugging it into an API and accessing it, I get a server timeout response.
You should either use a Debugging. Install xDebug to do so in PHP. This is also easy to integrate into Phpstorm.
Alternatively use var_dump() for printing out debug information about variables of any type to console.
You can display the requested page inside your php page, you have to add this snippet :
$client = new GuzzleHttp\Client();
$res = $client->request('GET', 'https://www.facebook.com/');
echo $res->getBody();

Can't get destination URL of Ad by Adwords API

I want to get the destination URL by using Google Adwords API(v201509).
Cording with PHP.
In the following code, I'm trying to get the url by using 'get' method of AdGroupAdService.
As a result, I could get ad->displayUrl properly but couldn't get ad->url and ad->finalUrls (null given).
What am I doing wrong?
adwords.php with the following code -
$adGroupAdService = $user->GetService('AdGroupAdService', ADWORDS_VERSION);
// Create selector.
$selector = new Selector();
$selector->fields = array('Headline', 'Id');
$selector->ordering[] = new OrderBy('Headline', 'ASCENDING');
// Create paging controls.
$selector->paging = new Paging(0, AdWordsConstants::RECOMMENDED_PAGE_SIZE);
do {
// Make the get request.
$page = $adGroupAdService->get($selector);
// Display results.
if (isset($page->entries)) {
foreach ($page->entries as $adGroupAd) {
array_push($googleAccountStructure, $adGroupAd);
//var_dump($adGroupAd);
}
}
// Advance the paging index.
$selector->paging->startIndex += AdWordsConstants::RECOMMENDED_PAGE_SIZE;
} while ($page->totalNumEntries > $selector->paging->startIndex);
Please update your selector fields with this one
$selector->fields = array('Headline', 'Id', 'CreativeFinalUrls', 'Url');
As per adwords api doc if you use Upgraded URLs you need to pass Final URLs in selector fields
https://developers.google.com/adwords/api/docs/reference/v201509/AdGroupAdService.Ad#finalUrls

How should I properly handle non-cursor pagination with the Facebook PHP SDK?

I am using a few of the Facebook Graph API methods that have pagination successfully using cursor-based pagination, similar to this:
echo '<ul>';
$params = array('limit' => 10);
do {
$groups = (new FacebookRequest(
$session, 'GET', '/me/groups', $params
))->execute()->getGraphObject();
if (null !== $groups->getProperty('paging') && null != $groups->getProperty('paging')->getProperty('next')) {
$params = array('limit' => 10, 'after' => $groups->getProperty('paging')->getProperty('cursors')->getProperty('after'));
} else {
$params = null;
}
foreach ($groups->getProperty('data')->asArray() as $group) {
echo '<li>' . $group->name . '</li>';
}
} while ($params !== null);
echo '</ul>';
This simple code will grab all the groups of the current user. It checks that the paging and paging/next properties are present and if so uses the cursor to setup another iteration of the loop. I realise now this could probably have been done better as the cursor isn't always available. When I use the /{group-id}/feed API endpoint there are the previous and next links but no cursor.
So, how am I supposed to make paginated requests when there is no cursor with the Facebook PHP SDK?
I see other answers suggesting using cURL or even file_get_contents to grab the next and previous URLs but that seems very silly considering I'm using the PHP SDK here - surely there's a built-in way?
I'm using facebook/php-sdk-v4 with Composer - there doesn't seem to be the (old?) $facebook->api(...) functionality availble here either.
Have a look at
https://developers.facebook.com/docs/php/FacebookResponse/4.0.0
There is a method getRequestForNextPage() in the PHP SDK v4.0.0.
// A FacebookResponse is returned from an executed FacebookRequest
try {
$response = (new FacebookRequest($session, 'GET', '/me'))->execute();
// You can get the request back:
$request = $response->getRequest();
// You can get the response as a GraphObject:
$object = $response->getGraphObject();
// You can get the response as a subclass of GraphObject:
$me = $response->getGraphObject(GraphUser::className());
// If this response has multiple pages, you can get a request for the next or previous pages:
$nextPageRequest = $response->getRequestForNextPage();
$previousPageRequest = $response->getRequestForPreviousPage();
} catch (FacebookRequestException $ex) {
echo $ex->getMessage();
} catch (\Exception $ex) {
echo $ex->getMessage();
}
By looking at the source code at
https://github.com/facebook/facebook-php-sdk-v4/blob/4.0-dev/src/Facebook/FacebookResponse.php#L164
it just handles the next property:
return $this->handlePagination('next');
IMHO, using the next property as a default should be fine, opposed to cursors. Furthermore, I don't even see a cursors property when querying a sample group's feed, so this might be obsolete.
References:
https://developers.facebook.com/docs/graph-api/using-graph-api/v2.3#paging

Accessing Properties/Methods in Facebook PHP Graph API

I am using Facebook's graph API in PHP to access a user's feed.
Specifically, I want to record the names of people who posted and people who were tagged in the post.
I have been able to retrieve the name of people who posted by using the methods:
getProperty('from')->getProperty('name')
But, for whatever reason, I have not been successful in getting who the was tagged (despite similarly using getProperty('to') ).
As well, I have been able to accomplish this in javascript (so I believe I am correctly interpreting the structure of the data), but not PHP.
If anyone can help me retrieve the name of who is tagged in the feed by accessing the property with the getProperty methods that I have been using or by accessing the name property another way, that would be greatly appreciated.
Below is my best attempt at coding this section:
/* make the API call to get home*/
$request = new FacebookRequest(
$session,
'GET',
'/me/home'
);
$response = $request->execute();
$graphObject = $response->getGraphObject();
//array of the 25 most recent posts
$dataPostArray = $graphObject->getPropertyAsArray('data');
$i = 0;
while($dataPostArray[$i])
{
//print_r($r->getProperty('name'));
if($dataPostArray[$i]->getProperty('from') )
{
echo $dataPostArray[$i]->getProperty('from')->getProperty('name');//works as intended
}
if($dataPostArray[$i]->getProperty('to') )
{
$temp = $dataPostArray[$i]->getProperty('to');
$dataToArray = $temp->getPropertyAsArray('data');
echo $dataToArray[0]->getProperty('name');
}
$i++;
}
I think your issue may be that the getPropertyAsArray method is not an option for when you are getting the data property. As an alternative to the getProperty methods. You could do it like such:
$graphObject = $response->getGraphObject()->asArray();
$i = 0;
while($graphObject['data'][$i])
{
if($graphObject['data'][$i]->from )
{
//echo $graphObject['data'][$i]->from->name;//will work just as you have above
}
$j = 0;
if($graphObject['data'][$i]->to->data[$j]->name )
{
echo '<pre>' . print_r( $graphObject['data'][$i]->to->data[$j]->name, 1 ) . '</pre>';
}
$i++;
}
}

SOAP error. Not getting correct structure returned

I have a website that contains a form that makes various SOAP requests at certain points. One of these requests gets a list of induction times returned and displays them to the user in order for them to pick one.
I am getting results returned fine from the SOAP service but unfortunately it seems to be not showing information correctly and even not displaying returned object keys at all.
I have liased with one of the devs at the SOAP end and he says the service is fine and spitting out the cirrect information. He has provided a screentshot:
Here is my code to pull call the method I need for this information:
public function getInductionTimes($options) {
$client = $this->createSoapRequest();
$inductionTimes = $client->FITinductionlist($options);
//die(print_r($inductionTimes));
return $inductionTimes;
}
private function createSoapRequest() {
$url = 'https://fitspace.m-cloudapps.com:444/FITSPACE/MHservice.asmx?WSDL';
$options["connection_timeout"] = 25;
$options["location"] = $url;
$options['trace'] = 1;
$options['style'] = SOAP_RPC;
$options['use'] = SOAP_ENCODED;
$client = new SoapClient($url, $options);
//die(print_R($client->__getFunctions()));
return $client;
}
As you can see I print_r the code right after I have received it to check what I am getting returned and it is this:
As you can see this IDdtstring field is getting completely ignored.
Does anyone have any ideas as to why this may be happening? Is it something to do with encoding? I can't seem to get anywhere on this issue!
Thanks
I was able to retrieve the fields correctly, including IDdtstring, using your basic code. Perhaps you are not sending the parameters correctly?
function getInductionTimes($options) {
$client = createSoapRequest();
$inductionTimes = $client->FITinductionlist($options);
die(print_r($inductionTimes));
return $inductionTimes;
}
function createSoapRequest() {
$url = 'https://fitspace.m-cloudapps.com:444/FITSPACE/MHservice.asmx?WSDL';
$options["connection_timeout"] = 25;
$options["location"] = $url;
$options['trace'] = 1;
$options['style'] = SOAP_RPC;
$options['use'] = SOAP_ENCODED;
$client = new SoapClient($url, $options);
//die(print_R($client->__getFunctions()));
return $client;
}
getInductionTimes(array("IDDate" => "2013-06-28T13:00:00+01:00", "GYMNAME" => "Bournemouth"));
I managed to solve this issue by adding the line of code into my SOAP options array which I then presume was an issue with my WSDL being cached in PHP:
$options['cache_wsdl'] = WSDL_CACHE_NONE;

Categories