Categories
alexwebdevelop.com

PHP tip 5: retrieve a remote resource with fopen()

[et_pb_section fb_built=”1″ _builder_version=”3.0.93″][et_pb_row custom_padding=”27px|0px|27px|0px” _builder_version=”3.0.93″][et_pb_column type=”4_4″ _builder_version=”3.0.47″ parallax=”off” parallax_method=”on”][et_pb_image src=”https://alexwebdevelop.com/wp-content/uploads/2018/06/remote-resource-fopen.jpg” alt=”Retrieve a remote resource with fopen()” title_text=”Retrieve a remote resource with fopen()” align=”center” _builder_version=”3.7″ custom_margin=”||5px|” custom_padding=”|||”][/et_pb_image][/et_pb_column][/et_pb_row][et_pb_row custom_padding=”27px|0px|27px|0px” _builder_version=”3.0.93″][et_pb_column type=”4_4″ _builder_version=”3.0.47″ parallax=”off” parallax_method=”on”][et_pb_text disabled_on=”on|off|off” _builder_version=”3.7″]

 

[/et_pb_text][et_pb_text _builder_version=”3.7″]

RETRIEVE A REMOTE RESOURCE WITH fopen()

[/et_pb_text][et_pb_text disabled_on=”on|off|off” _builder_version=”3.7″]

 

[/et_pb_text][et_pb_text _builder_version=”3.7″ custom_margin=”||0px|” custom_padding=”|||”]

 

Sometimes you may need to connect to a REST service to retrieve some data, or simply download a file through a standard HTTP or FTP connection.

 

The PHP fopen() function is incredibly helpful because it doesn’t just work for local files, but you can also use it to read data from remote locations using different protocols.

In fact, fopen() supports a large number of wrappers, or protocol handlers, that let it connect to local or remote resource through different communication protocols always using the same syntax.

 

You can use fopen() to get a webpage content and edit it, a common requirement in work environments.

In the following example we change the Google homepage’s title:

[/et_pb_text][/et_pb_column][/et_pb_row][et_pb_row custom_padding=”27px|0px|27px|0px” _builder_version=”3.5.1″][et_pb_column type=”4_4″ _builder_version=”3.0.47″ parallax=”off” parallax_method=”on”][et_pb_text _builder_version=”3.7″ text_font=”||||||||” text_font_size=”13px”]

<?php

/* Remote resource address, with protocol */
$google = 'http://www.google.com';

/* Open the remote resource */
$resource = fopen($google, 'rb');

/* Get the content as a string */
$content = stream_get_contents($resource);

/* Now we change the HTML title */
$content = str_replace('<title>Google</title>', '<title>My new page</title>', $content);

/* Echo the result (of course we don't have Javascript, CSS etc.) */
echo $content;

[/et_pb_text][et_pb_text _builder_version=”3.7″ custom_margin=”||0px|” custom_padding=”|||”]

 

 

You can also use some “shortcut functions” like file() or file_get_contents(), that automatically store the resource data into an array or into a string.

You can see how I use file() to download a remote HTTP file in this Time Handling tutorial.

[/et_pb_text][et_pb_text _builder_version=”3.7″ custom_margin=”|||”]

 

 

If you have any questions, feel free to ask in the comments below or on my Facebook Group: Alex PHP café.

If this tip has been helpful to you, please spend a second of your time and share it using the buttons below… thanks!

[/et_pb_text][et_pb_text _builder_version=”3.2.2″]

 

Alex

[/et_pb_text][et_pb_code _builder_version=”3.10.1″][/et_pb_code][/et_pb_column][/et_pb_row][/et_pb_section]

Categories
alexwebdevelop.com

PHP tip 4: profiling with time and microtime

[et_pb_section fb_built=”1″ _builder_version=”3.22″ fb_built=”1″ _i=”0″ _address=”0″][et_pb_row _builder_version=”3.25″ custom_padding=”27px|0px|27px|0px” _i=”0″ _address=”0.0″][et_pb_column type=”4_4″ _builder_version=”3.25″ custom_padding=”|||” _i=”0″ _address=”0.0.0″ custom_padding__hover=”|||”][et_pb_image src=”https://alexwebdevelop.com/wp-content/uploads/2018/06/profiling-time-microtime.jpg” alt=”Profiling with time() and microtime()” title_text=”Profiling with time() and microtime()” align=”center” align_tablet=”center” align_last_edited=”on|desktop” _builder_version=”3.23″ custom_margin=”||5px|” custom_padding=”|||” _i=”0″ _address=”0.0.0.0″][/et_pb_image][/et_pb_column][/et_pb_row][et_pb_row _builder_version=”3.25″ custom_padding=”27px|0px|27px|0px” _i=”1″ _address=”0.1″][et_pb_column type=”4_4″ _builder_version=”3.25″ custom_padding=”|||” _i=”0″ _address=”0.1.0″ custom_padding__hover=”|||”][et_pb_text disabled_on=”on|off|off” _builder_version=”3.7″ _i=”0″ _address=”0.1.0.0″]

 

[/et_pb_text][et_pb_text _builder_version=”3.7″ _i=”1″ _address=”0.1.0.1″]

PROFILING WITH TIME() AND MICROTIME()

[/et_pb_text][et_pb_text disabled_on=”on|off|off” _builder_version=”3.7″ _i=”2″ _address=”0.1.0.2″]

 

[/et_pb_text][et_pb_text _builder_version=”3.7″ custom_margin=”||0px|” custom_padding=”|||” _i=”3″ _address=”0.1.0.3″]

 

Code profiling is the analysis of various metrics related to code performance and efficiency.

If a PHP script is taking too much time to complete, it’s a good idea to profile the script to find out the cause.

 

The first thing to do is to isolate the piece of code (or the different pieces of code) responsible for the long execution time.

It could be an unoptimized function working on a large set of data, a slow SQL query, or a wrong iteration going out of control.

 

A very easy and straightforward way to perform this analysis is to use the time() and microtime() functions.

Both these functions return the current Unix Timestamp. time() returns it in seconds, while microtime() returns a float with microseconds too.

 

To check the execution time of a piece of code you need to:

  1. Save the current time with time() or with microtime() before the code you want to profile.
  2. After the code, call time() or microtime() again and calculate the difference from the previously saved value.
  3. That is the execution time in seconds 😉

If the execution time is more than a few seconds you can use time(), otherwise it’s usually more useful to use microseconds.

 

This is a practical example:

[/et_pb_text][/et_pb_column][/et_pb_row][et_pb_row _builder_version=”3.25″ custom_padding=”27px|0px|27px|0px” _i=”2″ _address=”0.2″][et_pb_column type=”4_4″ _builder_version=”3.25″ custom_padding=”|||” _i=”0″ _address=”0.2.0″ custom_padding__hover=”|||”][et_pb_text _builder_version=”3.27.3″ text_font=”||||||||” text_font_size=”13px” hover_enabled=”0″ _i=”0″ _address=”0.2.0.0″]

<?php

$start = microtime(TRUE);

/* Start of the code to profile */
for ($a = 0; $a < 10000000; $a++)
{
	$b = $a*$a;
}
/* End of the code to profile */

$end = microtime(TRUE);
echo "The code took " . ($end - $start) . " seconds to complete.";

[/et_pb_text][et_pb_text _builder_version=”3.7″ custom_margin=”|||” _i=”1″ _address=”0.2.0.1″]

 

 

 

If you have any questions, feel free to ask in the comments below or on my Facebook Group: Alex PHP café.

If this tip has been helpful to you, please spend a second of your time and share it using the buttons below… thanks!

[/et_pb_text][et_pb_text _builder_version=”3.2.2″ _i=”2″ _address=”0.2.0.2″]

 

Alex

[/et_pb_text][et_pb_code _i=”3″ _address=”0.2.0.3″][/et_pb_code][/et_pb_column][/et_pb_row][/et_pb_section]

Categories
alexwebdevelop.com

PHP tip 3: Date operations with mktime

[et_pb_section fb_built=”1″ _builder_version=”3.0.93″][et_pb_row custom_padding=”27px|0px|27px|0px” _builder_version=”3.0.93″][et_pb_column type=”4_4″ _builder_version=”3.0.47″ parallax=”off” parallax_method=”on”][et_pb_image src=”https://alexwebdevelop.com/wp-content/uploads/2018/06/date-operations-mktime.jpg” alt=”Date operations with mktime()” title_text=”Date operations with mktime()” align=”center” _builder_version=”3.7″ custom_margin=”||5px|” custom_padding=”|||”][/et_pb_image][/et_pb_column][/et_pb_row][et_pb_row custom_padding=”27px|0px|27px|0px” _builder_version=”3.0.93″][et_pb_column type=”4_4″ _builder_version=”3.0.47″ parallax=”off” parallax_method=”on”][et_pb_text disabled_on=”on|off|off” _builder_version=”3.7″]

 

[/et_pb_text][et_pb_text _builder_version=”3.7″]

DATE OPERATIONS WITH MKTIME()

[/et_pb_text][et_pb_text disabled_on=”on|off|off” _builder_version=”3.7″]

 

[/et_pb_text][et_pb_text _builder_version=”3.7″ custom_margin=”||0px|” custom_padding=”|||”]

 

mktime() is a very versatile function.

Its main purpose is to get the Unix Timestamp of a specific date-time (to learn more about time and PHP, you can read my PHP Time Handling guide: part 1, part 2 and part 3).

 

But mktime() has another, less well known and incredibly useful feature: it can perform date-time operations.

Each of the function’s arguments represents a date-time component: hour, minute, second, month, day and year.

 

mktime() accepts any integer number for each component, including values outside of the valid range of that component. Even negative integers are accepted.

 

 

[/et_pb_text][et_pb_code _builder_version=”3.7″][easy-tweet tweet=”Alex’s PHP tip: Date-time operations with mktime()” via=”no” hashtags=”PHP”][/et_pb_code][et_pb_text _builder_version=”3.7″ custom_margin=”||0px|” custom_padding=”|||”]

 

[/et_pb_text][et_pb_text _builder_version=”3.7″ custom_margin=”||0px|” custom_padding=”|||”]

 

When a value is outside of the valid range, the function calculates the difference from the nearest valid value and then adds, or subtracts, an equal number of component units to the date-time before returning the Unix Timestamp.

 

It’s really easier to see how it works with a few examples:

 

[/et_pb_text][et_pb_text _builder_version=”3.7″ text_font=”||||||||” text_font_size=”13px”]

<?php

/* Get the Unix Time of the beginning of the next year */
mktime(0, 0, 0, 1, 1, date('Y') + 1);

/* Yesterday at midnight;
   without worrying about day and month number or daylight saving time */
mktime(0, 0, 0, date('n'), date('j') - 1);

/* 2 months from now, same day and time;
   without worrying about the number of days or daylight saving time */
mktime(date('H'), date('i'), date('s'), date('n') + 2);

/* Midnight, last day of the last month;
   simply as the "day 0" of the current month */
mktime(0, 0, 0, date('n'), 0);

/* 72 hours before now;
   without worrying about day/month/year changes or daylight saving time */
mktime(date('H') - 72);

[/et_pb_text][et_pb_text _builder_version=”3.7″ custom_margin=”||0px|” custom_padding=”|||”]

 

Adding or subtracting date components can be really useful.

 

Suppose that you need to get the Unix Timestamp of “last day of the previous monthâ€.

If you had to provide mktime() with the exact date-time components, you would need to get the current month number and subtract 1 from it, then check if it is 0 (meaning that we are in January, so you would have to set it to 12 and decrease the year by 1), then get the current day number and check how many days the previous month has…

It’s long and boring.

 

But thanks to mktime(), you can just set 0 as the day value to achieve the same result:

[/et_pb_text][/et_pb_column][/et_pb_row][et_pb_row custom_padding=”27px|0px|27px|0px” _builder_version=”3.5.1″][et_pb_column type=”4_4″ _builder_version=”3.0.47″ parallax=”off” parallax_method=”on”][et_pb_text _builder_version=”3.7″ text_font=”||||||||” text_font_size=”13px”]


$time = mktime(0, 0, 0, date('n'), 0, date('Y'));

[/et_pb_text][et_pb_text _builder_version=”3.7″ custom_margin=”|||”]

 

 

 

If you have any questions, feel free to ask in the comments below or on my Facebook Group: Alex PHP café.

If this tip has been helpful to you, please spend a second of your time and share it using the buttons below… thanks!

[/et_pb_text][et_pb_text _builder_version=”3.2.2″]

 

Alex

[/et_pb_text][et_pb_code _builder_version=”3.10.1″][/et_pb_code][/et_pb_column][/et_pb_row][/et_pb_section]