Installation

In this section, I am going to discuss the development environment we will be using to develop bots, using simple PHP command-line applications, PHP error reporting and . In the next section, we will take a look at HTTP GET requests and HTTP responses in detail and begin executing these types of requests.

Step 1 – setting the development environment

While you should already be familiar with basic PHP functions and logic, I am going to outline the basic development environment, which you should use when reading and using the code in this book. As stated earlier, I will be using PHP 5.4 coding standards in all of the code examples in this book. Therefore, you should use a web server equipped with a PHP 5.4 basic . Also, during the course of this book, I will be using command lines to execute PHP applications using a web server with a Linux operating system (Ubuntu 12 to be exact).

On my Ubuntu server I would install PHP with the following command-line support using:

# sudo apt-get install php5 libapache2-mod-php5 php5-cli

You can check the PHP version installed on your web server in one of two ways. First, if you have a PHP CLI (Command Line Interface) SAPI (Server API) installed on your web server, you can use a command line to get the PHP Version. Here is a command line example on a Linux web server:

# php –v

This will print something like:

PHP 5.4.6-1ubuntu1.1 (cli) (built: Nov 15 2012 01:18:34)Copyright (c) 1997-2012 The PHP GroupZend Engine v2.4.0, Copyright (c) 1998-2012 Zend Technologies

In the previous example, my web server is prepared with PHP 5.4.6 installed.

The second way to check the PHP Version on your web server is to set a PHP script will display PHP web server information on a web page. To do this carry out the following steps:

  1. Create a file called info.php on your web server in the /var/www directory, and add the following content to the file:
    <?php
    /**
    * Display PHP version/info
    */
    phpinfo();
  2. Save the /var/www/info.php file and open the web page in a web browser. For example, the URL to the web page might look something like:
    http://localhost/info.php
  3. Once you load the web page in a web browser, you should see the PHP Version at the top of the page. It will look something like this:
    Step 1 – setting the development environment

You can see that—using this method—my web server has PHP Version 5.4.6 installed.

PHP error reporting

If you are not familiar with PHP error reporting (www.php.net/manual/en/function.error-reporting.php) and displaying PHP errors (www.php.net/manual/en/errorfunc.configuration.php#ini.display-errors), I would suggest reading on these two topics. Any proficient PHP programmer should be accustomed to PHP error reporting and displaying PHP errors.

I would highly recommend that you develop the bots of this book, and all other code of this book, in a development environment, and not a production environment. In your development environment, I would suggest allowing PHP to display errors, warnings, and notices. Without these turned on, you might get lost with some of the examples in this book.

Step 2 – command-line applications

Although you can develop bots and test them using typical web browser-based PHP applications, sometimes it is useful to develop and test bots using PHP command-line applications. I prefer using command-line applications to develop bots, and test them, because command-line applications allow us better memory usage, real-time messages/alerts, and the ability to fire slave processes (an advanced topic, outside the scope of this book).

In this book, I will be using typical web browser-based PHP applications because it will be easier for most programmers. However, I will cover using PHP command-line applications here in case you would rather develop and test your bots using command-line applications.

Creating a PHP command-line application is very simple. So, if you have never created one before, don't worry about having to learn a lot of new logic. Let's create a simple PHP command-line application now. In order to create a PHP command-line application and execute it on your web server, you will need a PHP CLI. So, install this before attempting the execution of a PHP command-line application. On my web server (Linux, Ubuntu), I can install a PHP CLI using the following command line:

# sudo apt-get install php5-cli

Save a file called 01_command_line_app.php on your web server, in a directory that you will use for all the code in this book () and add the following code to the project_directory/01_command_line_app.php file:

#!/usr/bin/env php
<?php
echo "Hello world
";

Now we can run our simple PHP command-line application using the executable file from the command line:

# /var/www/project_directory/01_command_line_app.php
Hello world

You may need to allow the file to be executed by the operating systemthis can be done using the following command:

# sudo chmod +x /var/www/project_directory/01_command_line_app.php

Note

I am using Linux/Unix commands, so if you are using a different operating system please use the appropriate commands.

The first thing you might notice is the difference between the PHP command-line application and a typical web browser-based PHP application, which is in the first line of code #!/usr/bin/env php. This line is called a shebang and will probably be familiar to most Linux/Unix users. This is a simple way of notifying the operating system what interpreter program should be used to execute the code in the file. In this example, we are telling the operating system to use the PHP interpreter.

You can find the required full path of the PHP interpreter program on your Linux/Unix web server by using the which command:

# which php
/usr/bin/php

We can use a shebang so that we don't have to manually tell the operating system which interpreter program to use, but we don't have to. For example, we could remove the shebang from the project_directory/01_command_line_app.php file:

<?phpecho 

"Hello world
";

And now to run the application as a command-line application we will use :

# /usr/bin/php /var/www/project_directory/01_command_line_app.php
Hello world

Or, if your web server is set up like mine is, you can simply use:

# php /var/www/project_directory/01_command_line_app.php
Hello world

You should use whatever method you find easiest for executing your PHP command-line applications.

And that's it!

There are different methods that can be used when making HTTP GET and POST requests in PHP. The two methods that I am going to discuss are the PHP built-in function file_get_contents() and the cURL library (www.php.net/manual/en/book.curl.php). In this book, I am going to use the PHP function file_get_contents() when executing HTTP requests, because I find it the simplest way to learn and teach. However, cURL is a powerful library that you may find useful when you continue to develop bots on your own.

Once you've installed the cURL library with your PHP installation you can use cURL in your code to send HTTP requests. Here is an example:

<?php
// ensure PHP cURL library is installed
if(function_exists('curl_init'))
{
  // set cURL resource
  $curl = curl_init('http://www.google.com'),

  // set return transfer as string to true
  curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);

  // set variable with response string
  $request_response = htmlentities(curl_exec($curl));

  // close cURL resource
  curl_close($curl);

  // display response string
  echo '<pre>' . print_r($request_response, true) . '</pre>';
}
else // PHP cURL library not installed
{
  echo 'Please install PHP cURL library';
}

In the preceding code, we used the cURL library to create a simple HTTP GET request (the default request type for the cURL library is a GET). As you can see, this is a fairly simple way to execute an HTTP GET request in PHP. However, as I stated earlier, in this book, I will be using the file_get_contents() PHP function to perform HTTP requests.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset