i made simple PHP code to show uniquePageViews for my website. The main code was for 7days, but i want to make it lifetime and when i made start-date: "2017-10-5" it start shows me errors. But documentation says that i can place date in that type.
Work variant:
try {
$result = $service->data_ga->get( $GA_VIEW_ID, '100daysAgo', 'today','ga:uniquePageviews');
$count = $result->totalsForAllResults['ga:uniquePageviews'];
echo $count;
} catch(Exception $e) {
var_dump($e);
}
Fail variant:
try {
$result = $service->data_ga->get( $GA_VIEW_ID, '2017-10-5', 'today','ga:uniquePageviews');
$count = $result->totalsForAllResults['ga:uniquePageviews'];
echo $count;
} catch(Exception $e) {
var_dump($e);
}
From the documentation you linked (Emphasis mine) :
All Analytics data requests must specify a date range. If you do not include start-date and end-date parameters in the request, the server returns an error. Date values can be for a specific date by using the pattern YYYY-MM-DD or relative by using today, yesterday, or the NdaysAgo pattern. Values must match [0-9]{4}-[0-9]{2}-[0-9]{2}|today|yesterday|[0-9]+(daysAgo).
Which means your day must be displayed with two digits. Try 2017-10-05 as your start date.
Related
I am trying to check for toll-free numbers, and it is working as expected. However, my problem is that some countries don't have the TollFree numbers.
Using the same code on these countries throws a 404 error and stops the code there.
The only way I could think of is making a massive if statement and adding each country manually which offers toll-free option, but I don't like this solution at all as it will be hardcoded. Is there a way to overcome this issue, so it works for the countries that has the .json and ignore the ones that doesn't (instead of crashing the code)?
$twilio = new Client(env('TWILIO_ID'), env('TWILIO_TOKEN'));
$iso = 'CY';
$params = ["excludeLocalAddressRequired" => "true"];
$tollFreeNumbers = $twilio->availablePhoneNumbers($iso)->tollFree->read($params);
This is the response:
"[HTTP 404] Unable to fetch page: The requested resource /2010-04-01/Accounts/ACxxxxx/AvailablePhoneNumbers/CY/TollFree.json was not found"
Using this code will crash with CY but will work with UK, US, CA and many more. Should I add an if statement with hardcoded countries? (I really dislike this solution, but this is what I can think of). What I mean is:
if ($iso == 'GB' || $iso == 'US' || $iso == 'CA') { // and many more
$tollFreeNumbers = $twilio->availablePhoneNumbers($iso)->tollFree->read($params);
}
Twilio developer evangelist here.
Rather than guarding up front with a conditional (which could become out of date as we add toll free numbers in other countries in the future), why not catch the error and return a message to the user to say that toll free numbers are not available in the country they are searching in.
Something like:
try {
$tollFreeNumbers = $twilio->availablePhoneNumbers($iso)->tollFree->read($params);
} catch (Exception $e) {
$tollFreeNumbers = [];
$message = "Toll free numbers are not available in this country.";
}
Let me know if that helps at all.
Why not just wrap it in a try catch?
try {
$tollFreeNumbers = $twilio->availablePhoneNumbers($iso)->tollFree->read($params);
} catch(\Exception $e) {
$tollFreeNumbers = [];
}
I want to show "Stay Tuned..." if there are no Upcoming Events.
Sure there is a simple solution here, I've just never worked with the combination of PHP and shortcodes before.
For reference: http://docs.wp-event-organiser.com/shortcodes/events-list/
Upcoming Events
[eo_events event_start_after=today showpastevents=false] %event_title% on %start{jS M Y}{ g:i:a}% [/eo_events]
PastĀ Events
[eo_events event_end_before=today] %event_title% on %start{jS M Y}{ g:i:a}% [/eo_events]
Be aware that the showpastevents parameter will be deprecated some time soon, so might be good to leave it out.
There are a few ways to achieve this. Here's some raw code that will achieve the desired results (using the shortcode; of course you could do the same thing just using functions):
// GET FUTURE ------------------
if (function_exists("eo_get_events")) {
$events = eo_get_events(array(
'event_start_after'=>'today',
));
if ($events) {
echo do_shortcode("[eo_events event_start_after=\"today\"]%event_title% on %start{jS M Y}{ g:i:a}% [/eo_events]");
} else {
echo "Stay Tuned....";
}
}
// GET PAST ------------------
if (function_exists("eo_get_events")) {
$events = eo_get_events(array(
'event_end_before'=>'today'
));
if ($events) {
echo do_shortcode("[eo_events event_end_before=\"today\"]%event_title% on %start{jS M Y}{ g:i:a}% [/eo_events]");
} else {
echo "No Past Events....";
}
}
I am trying to remove a number from my Twilio account using the following code:
// remove the number from Twilio
$client = new Services_Twilio(TWILIO_SID, TWILIO_TOKEN);
try {
// Remove the number
$purchaseNumber = $client->account->incoming_phone_numbers->delete(array(
"PhoneNumber" => "+447903000000"
));
} catch (Exception $e) {
echo 'Error = '.$e->getMessage();
}
Which throws the following exception:
Error = The requested resource /2010-04-01/Accounts/jhsdkfjhdsjhf32370685sjhgfjhdsjfgsdjh/IncomingPhoneNumbers/Array.json was not found
I assume that the format of the request to delete the number is wrong but there doesn't seem to be any examples in the documentation on how to do it - can anyone shed any light on where I am going wrong?
So it seems that you cannot delete a number using the number - you have to use the SID which is returned when you purchase the number. Details here:
http://web.onassar.com/blog/2012/06/16/twilio-incoming-phone-numbers-releasing-deletion/
Using the following code I am able to get the logs of calls and SMS's. How do I modify this code to only search between certain dates using PHP?
// Instantiate a new Twilio Rest Client
$client = new Services_Twilio($AccountSid, $AuthToken, $ApiVersion);
// http://www.twilio.com/docs/quickstart...
try {
// Get Recent Calls
foreach ($client->account->calls as $call) {
echo "Call from $call->sid : $call->from to $call->to at $call->start_time of length $call->duration $call->price <br>";
}
}
catch (Exception $e) {
echo 'Error: ' . $e->getMessage();
}
You will want to add a code snippet that looks something like this:
$client = new Services_Twilio('AC123', '123');
foreach ($client->account->calls->getIterator(0, 50, array(
'StartTime>' => '2012-04-01',
'StartTime<' => '2012-05-01'
)) as $call) {
echo "From: {$call->from}\nTo: {$call->to}\nSid: {$call->sid}\n\n";
}
If you want to filter the list, you have to construct the iterator yourself with the getIterator command. There's more documentation here: Filtering Twilio Calls with PHP
User search terms StartTime> and StartTime< for this. First one means call start time is greater than and last one means call start time is less than.
To find every calls that started between 4th and 6th July of 2009 add search term
array(
'StartTime>' => '2009-07-04',
'StartTime<' => '2009-07-06'
)
See example 4 on the twilio doc.
Also note you can always ask twilio support. They usually help gladly.
I am running subscriptions on my website.
I have a 1,3,6 and 12 months of subscription, and I would like the user to be able to change that subscription whenever they feel like.
However, I need to calculate the amount of money the user had to pay had he or she signed up for the shorter term, rather the relatively cheap,longer one.
I made this function optimized_subscription_total($active_sub_time,$arr_sub_values) so that it returns that sum of money exactly.
<?php
function optimized_subscription_total($active_sub_time,$arr_sub_values)
{
// This function takes a row from the DB where prices for each period of time usage is listed. there are prices for 1 month, 3 months,6 and 12 months.
// when the user has subscribed for 12 months, and the user asks for a refund, after they used 9 months and 6 days for example, the system treats the refund as if they subscribed for (in months) COST_6 + COST_3 + (COST_1/30)*6
// the result of the function is then subtracted from the amount they actually paid and is considered the refund.
// $arr_sub_values is the associative row from the DB, containing the prices
// $active_sub_time is measured in months and is a double
$result=0;
while(($active_sub_time-12)>=0)
{
$active_sub_time-=12;
$result+=($arr_subscription_values['COST_12']);
}
while(($active_sub_time-6)>=0)
{
$active_sub_time-=6;
$result+=($arr_subscription_values['COST_6']);
}
while(($active_sub_time-3)>=0)
{
$active_sub_time-=3;
$result+=($arr_subscription_values['COST_3']);
}
while(($active_sub_time-1)>=0)
{
$active_sub_time-=1;
$result+=($arr_subscription_values['COST_1']);
}
if($active_sub_time>0)
$result+=($active_sub_time)*($arr_subscription_values['COST_1']);
return $result;
}
$datetime1 = date_create('2009-12-11');
$datetime2 = date_create('2010-11-09');
$interval = date_diff($datetime1, $datetime2);
$num_of_months = ($interval->format('%y'))*12+($interval->format('%m'))+($interval->format('%a'))/30;
echo "<br />";
$v = array('COST_1'=>'3.99','COST_3'=>'9.99','COST_6'=>'15.99','COST_12'=>'25.99');
echo "OPT value for $num_of_months months=" . optimized_subscription_total($num_of_months, $v);
?>
Strangely I get the bug appearing only after 7 to 10 times after refreshing this code.
So I got:
OPT value for 10 months=M.97
as a result here. I think I need to get a float number, no ?
I was expecting the result of the function that should be "OPT value for 10 months=29.97", as it should take COST_6 + COST_3 + COST_1... but I get that weird M.97, and sometimes things like POKHHHG.97
I would change the logic to the following and see if error is still produced. I think this is a little bit more clear and easy to debug. It is the same as your though just explained differently.
while($active_sub_time>=12)
{
$active_sub_time-=12;
$result+=($arr_subscription_values['COST_12']);
}
while($active_sub_time>=6)
{
$active_sub_time-=6;
$result+=($arr_subscription_values['COST_6']);
}
while($active_sub_time>=3)
{
$active_sub_time-=3;
$result+=($arr_subscription_values['COST_3']);
}
while($active_sub_time>=1)
{
$active_sub_time-=1;
$result+=($arr_subscription_values['COST_1']);
}
What I would also do is added debug cod at the top inside function.
echo "<br>$active_sub_time" // debug code include at the top
This will give you an idea, if any garbage being is being sent to the function itself.
Also I would add this test function in all while blocks if the above does not resolve the issue.
if (!is_numeric($result))
{
echo"<br> Bug occurred";break; // print other values if necessary
}