How to get last page url in twilio pagination php - php

$page = isset($input['page'])?$input['page']:0;
$perPageRecord = 10;
$calls = $this->twilio->calls->page(["to" => "+919876543210"],$perPageRecord,'',$page);
$data = [];
echo $calls->getNextPageUrl;
exit;
I am using above code to get next page url and it print successfully. But i want to print last page url while In php twilio.
Anyone can tell me how can i get last page url using twilio php.
Thanks

It looks like you will need to programmatically extract a returned range and manipulate the resulting data to get the X most recent results (last page).
Replacing Absolute Paging and Related Properties
Usage and Migration Guide for Twilio's PHP Helper Library 5.x

Related

Meet cookie error when crawl website that use php session

I want to crawl the following page: https://db.aa419.org/fakebankslist.php with search word "sites".
I'm using requests package in python. No plan to try selenium b/c there is no javascript in this page, neither do I need to click any button. I think requests package should have the ability to crawl.
For the website itself, I guess it send query words using php. So I created a php session using requests.post() and retrieve cookies using response.cookies, then feed the cookies to the site in the following post requests. The code structure is below:
#crawl 1st page with search word in url
url='https://db.aa419.org/fakebankslist.php?psearch=sites&Submit=GO&psearchtype='
response = requests.post(url)
cookies= response.cookies
print(cookies)
#crawl page 2-4
for i in range(2, 5):
url = 'https://db.aa419.org/fakebankslist.php?start={}'.format(str(1+20*(i-1)))
response = requests.post(url, cookies=cookies)
cookies= response.cookies #update cookie for each page
print(cookies)
However, it only works for the first 2 pages. After the loop begin to crawl page 3, the cookie becomes empty: <RequestsCookieJar[]>. I checked the response of page 3 and found it's some random page irrelevant to my query words "sites".
Could anyone explain whats's going on with this situation? How can I keep crawling the following pages? Thanks in advance!
I am not certainly sure what you are trying to obtain from that website but I will try to help.
First page with results can be obtained through this url:
https://db.aa419.org/fakebankslist.php?psearch=essa&Submit=GO&start=1
Value 1 for start key indicates the first result that apears on page. Since there are 19 results on each page to view second page you need to switch '1' to '21' :
https://db.aa419.org/fakebankslist.php?psearch=essa&Submit=GO&start=21
The second thing is that your requests should be made using GET method.
I checked the response of page 3 and found it's some random page irrelevant to my query words "sites"
I believe this is related to broken search engine of the website.
I hope this code helps:
#crawl page 1-5
s = requests.Session()
for i in range(0, 5):
url = 'https://db.aa419.org/fakebankslist.php?psearch=essa&Submit=GO start='+str(1+i*20)
response = s.get(url)
cookies= s.cookies #update cookie for each page
print('For page ', i+1, 'with results from', 1+i*20, 'to', i*20+20, ', cookies are:', str(cookies))

How can I increase the limit or get data from next resultpage using the facebook graph api

I am getting events from a page and noticed some were missing, this is because they are not in the first result but on another "page" (in the graph api explorer tool I can click 'next' at the bottom of my result.
How can I loop through everything there is?
Do I first need to create a loop for how many pages there are, and then create 1 big object/array containing all data?
Currently I have the following:
$json_object = file_get_contents("https://graph.facebook.com/v2.10/mypageid/events?fields=owner,start_time,cover,description,name,place&access_token=mytoken");
$feedarray = json_decode($json_object);
$f = 0;
$facebookfeed = '';
//Reversing to show newest events first
$facebookevents = array_reverse($feedarray->data);
foreach ($facebookevents as $key => $feed_data)
{
loop here
}
I tried to find out what changes when clicking on another page, but the only parameters that are added are: pretty=0 and limit=25 but this is the same on every page I click. Changing the limit does not work if I increase it.
How can I get ALL my data in one object/array?

PHP - Twilio Recording Duration Value Issue

So I'm creating an application that allows users to record a message through Twilio and I'm attempting to store the RecordingSID, the date it was created, and the duration of the recording in a MySQLi database right after the recording has been made. I've managed to get the RecordingSID by taking the last 34 digits off the RecordingURL using the substr() function and am simply getting whatever today's date is for the date created field in my database table. However, seemingly regardless of how long the actual recording is, I'm continually getting a value of 8 when attempting to get the recording duration. Here's what I've got right now (with database inserts omitted since they work):
<?php
$recordingURL = $_REQUEST['RecordingUrl'];
$recordingSID = substr($recordingURL, -34);
date_default_timezone_set('EST');
$dateCreated = date("Y-m-d");
$duration = $_REQUEST['RecordingDuration'];
?>
Any help with this matter would be fantastic! Thanks!
Edit: I've also tried the following solution in place of the last line in my previous code snippet:
<?php
$recordings = $client->account->recordings->getIterator(0, 50, array('Sid' => $recordingSID,));
foreach ($recordings as $recording)
{
$duration = $recording->duration;
}
?>
Based on that code sample you've pasted in, you could be doing a couple of things incorrectly. Correct me if I'm wrong, but I believe you are trying to request a Recording resource back from the Twilio API after you've submitted one with the Twilio js and their TwiML?
If so, twilio actually has a nice little demo of exactly your use case.
You shouldn't see anything in the $_REQUEST['RecordingDuration'], I'm not sure why you are even getting a value of 8 returned. Basically what you want to do is find the users recordings by using the Twilio REST API.
here is an example snippet:
<?php
// Get the PHP helper library from twilio.com/docs/php/install
require_once('/path/to/twilio-php/Services/Twilio.php'); // Loads the library
// Your Account Sid and Auth Token from twilio.com/user/account
$sid = "ACda6f132a3c49700934481addd5ce1659";
$token = "{{ auth_token }}";
$client = new Services_Twilio($sid, $token);
// Loop over the list of recordings and echo a property for each one
foreach ($client->account->recordings as $recording) {
echo $recording->duration;
}
The response from the API call will return a Recording resource.
Here is some more examples from their docs

How to load multiple pages from a REST API in PHP

Below is a simple example of code to fetch results from a REST XML API.
This is just a small portion I have extracted from my real PHP class for this question.
in the API URL which returns an XML document, I am curious about how I could fetch all the results from 1 page and then move on to fetch from the next page.
$this->api_page sets the page that the API returns data from.
Looking at the basic code below using SimpleXMLElement how could I for example return the data from 10 pages or all pages in the API starting at a page number, loading the results for that page and then fetching the next page an moving on.
Right now I am doing it with JavaScript and PHP by passing a Page number in the URL to my script using $_GET['page'] the problem with this is it requires a user to load the page and it's kind of sloppy.
My real API script will be ran from a Cron job on the server, so with that in mind, how could I fetch all pages?
I ask this question based on this example code below but also because it is a task that I often have to do on other projects and I don't know a good way of doing this?
<?php
$this->api_url = 'http://api.rescuegroups.org/rest/?key=' .$this->api_key.
'&type=animals&limit=' .$this->api_limit.
'&startPage='. $this->api_page;
$xmlObj = new SimpleXMLElement($this->api_url, NULL, TRUE);
foreach($xmlObj->pet as $pet){
echo $pet->animalID;
echo $pet->orgID;
echo $pet->status;
// more fields from the Pet object that is returned from the API call
// Save results to my own Database
}
?>
Based on the assumption that you run on a pretty stable environment you could loop through the pages like this:
<?php
$this->base_url = 'http://api.rescuegroups.org/rest/?key=' .$this->api_key.
'&type=animals&limit=' .$this->api_limit.
'&startPage=';
$start_page = $this->api_page;
$end_page = 10; //If you have a value for max pages.
// sometimes you might get the number of pages from the first returned XML and then you could update the $end_page inside the loop.
for ($counter = $start_page; $counter <= $end_page; $counter++) {
try {
$xmlObj = new SimpleXMLElement($this->base_url . $counter, NULL, TRUE);
foreach($xmlObj->pet as $pet){
echo $pet->animalID;
echo $pet->orgID;
echo $pet->status;
// more fields from the Pet object that is returned from the API call
// Save results to my own Database
}
} catch (Exception $e) {
// Something went wrong, possibly no more pages?
// Please Note! You wil also get an Exception if there is an error in the XML
// If so you should do some extra error handling
break;
}
}
?>

how to : define and get a custom report from google analytics using gapi

I have a custom report called my-newsletters in Google analytics. I want to fetch this report with a php call to $ga->requestReportData(...) and then parse the response and format it up.
First I made an account to collect all my newsletter open and click hits - each time someone opens a newsletter or clicks on a link in the newsletter I capture that with a call to the __utm.gif on Google. That part is working and I include in the call ( in the Landing Page aka utmp parameter) some data such as the word 'open' and 'click' to distinguish the events and also some other data i hope to parse out later, plus i use the campaign field and maybe I should do something with the source field too - now I just dup the utmp field. So far that part seems to work.
Now I need help to define a report that will return that utmp and campaign field info and the number of hits each has taken, sorted by date of hit I guess. then i need to call that report from my php and then later parse it - the parsing part I'm not worried about yet.
PS: here is the code I use to generate the utm url
function getGoogleUtmUrl($source='Emails', $referer='opens', $estid='0',$mailid='0', $campaign){
$stat_id='MO-xxx31982-1';
$var_utmcs=urlencode( 'UTF-8');
$var_utmac = $stat_id;
$var_utmhn = 'mysite.com'; //enter your domain
$var_utmn = rand(1000000000,9999999999); //random request number
$var_cookie = rand(10000000,99999999); //random cookie number
$var_random = rand(1000000000,2147483647); //number under 2147483647
$var_today = time(); //today
$var_referer = $referer; //referer url
$utm_source = 'my_newsletter';
$utm_medium = 'Emails';
$utm_campaign = $campaign;//$_GET['url'];
$var_uservar = $estid.'_'.$mailid; //enter your own user defined variable
$var_utmp = 'mysite.com/newsletters/'.$referer.'/'.$estid.'/'.$mailid;//.$estid;//$_GET['url']; //this example adds a fake file request to the (fake) tracker directory (the image/pdf filename).
$urchinUrl1 = 'http://www.google-analytics.com/__utm.gif?utmwv=4.3&utmn='.$var_utmn.'&utmsr='.$referer.'&utmcs='.$var_utmcs.
'&utmul=en&utmje=0&utmfl=-&utmdt='.$utm_campaign.'&utmhn='.$var_utmhn.
'&utm_source='.$var_utmp.'&utm_medium='.$utm_medium.'&utm_campaign='.$utm_campaign.'&utmr='.$var_referer.
'&utmp='.$var_utmp.'&utmac='.$var_utmac.
'&utmcc=__utma%3D'.$var_cookie.'.'.$var_random.'.'.
$var_today.'.'.$var_today.'.'.$var_today.
'.2%3B%2B__utmb%3D'.$var_cookie.'%3B%2B__utmc%3D'.
$var_cookie.'%3B%2B__utmz%3D'.$var_cookie.'.'.$var_today.
'.2.2.utmccn%3D(direct)%7Cutmcsr%3D(direct)%7Cutmcmd%3D(none)%3B%2B__utmv%3D'.
$var_cookie.'.'.'%3B';
// Now fire off the HTTP request
echo "urchinURL1 == ".$urchinUrl1.' '.__FILE__.' '.__LINE__.'<br/>';
return $urchinUrl1;
seems like over kill to me but it works, I tried the code at https://developers.google.com/analytics/devguides/collection/other/mobileWebsites and it doesn't work - the opens and clicks do not register in analytics - at least not on the real time page.
Please help.
I suggest that you build your report query first, I recommend that you use Google Analytics Query Explorer for that.
And next use the reporting API from PHP to transpose the resulting query and extract the data from within your app.

Categories