In this guide, we will go through the different PHP clients you can use to connect to the Geekflare API.
Specifically, we will be covering using the Geekflare API with the file_get_contents function, Guzzle, HTTPful, and Symfony’s HTTPS client.
What is the Geekflare API?
Geekflare provides a set of free tools that you can use to monitor the performance of your website. These tools include a broken link analyzer, time to load, and a DNS checker. These tools can be accessed online via the web interface or the API.
The API is based on HTTP and can be accessed from any programming language with an HTTP client library. The API has a generous free tier that you can start using without needing to provide payment information.
What we are going to be building
We will write a script, executable from the command line, which will compute how long it takes to load the Google website and print them to the terminal. We will implement this simple program using different PHP HTTP clients to demonstrate what using the API looks like.
Specifically, we are going to use the built-in functions – file_get_contents() and php_curl, and the extension Guzzle PHP. However simple these examples may appear, they demonstrate the core concepts of using the Geekflare API.
To follow along, you will need to know PHP beforehand and have it installed on your computer. Additionally, you will need Composer to manage extensions.
Lastly, you will also need a text editor to write code in. In my case, I am going to be using Visual Studio Code, a popular open-source text editor from Microsoft. You can download it from the Visual Studio Code website.
Geekflare API overview
The Geekflare API has different endpoints depending on what you want to do. The complete list of endpoints and their associated documentation can be found on the documentation page.
Creating a Geekflare account
To get started using the API, you will need to create an account by going to the API landing page and clicking the sign-up button. After sign-up is complete, you will be taken to the dashboard, where you will see your API key. The dashboard should be like the picture below. I have blacked out my API key for security reasons.
In every API request you make, you will need to provide this key as a request header. Shortly, you will see how this can be done.
With a Geekflare account created and PHP installed, we can start creating the project.
Creating the project folder
First, create a folder where we will store the project files. After that, create the following files
After, run the following command to install the vlucas/phpdotenv and guzzlehttp/guzzle extension
We have created a payload variable and assigned it to a JSON string that contains the url, proxyCountry and followRedirect as properties.
The url property specifies the webpage whose load time we want to check.
The proxyCountry is the location of the server we want to use to make the request. In this case, we are using the USA server, but you can choose from India, China, the UK, and France. You can read the documentation for more details.
Then followRedirect specifies whether the proxy server should follow any redirections and measure the response time of the final response or the first redirect.
Afterward, we can create options that will configure our request by adding this code:
By doing this, we have created an options object that specifies that our HTTP method is POST, and we have a header that specifies two properties, the content type as JSON and the x-api-key as the API key that you specified in the .env file and has been loaded as an environmental variable.
Next, we can make the request by creating a stream where our options will be written to:
$context = stream_context_create($options);
Next, we call the file_get_contents() method to make the request and store the response as a variable.
When you run the file using the following command:
You will get the following output
cURL is a command line utility that is used to make client-side URL requests. In PHP, it can be used using the php-curl utility. To begin using it, open the with_curl.php file and write the boilerplate PHP
// all new code will be written here
Then let us import extensions and load the API_KEY environmental variable defined in the .env file
And when you execute the script using the following command:
And you will see the response:
In this article, we went through the different clients you may want to use when creating a PHP project that will require the Geekflare API.
While the scripts in this project use the command line as the primary form of output, real-world projects could present the response on a web page or write it to a file. The example scripts in this article were simple, but they demonstrate the core concepts of using the Geekflare API. To use different APIs, you can change the endpoint and pass in different options in the request body.
Python is a very versatile language, and Python developers often have to work with a variety of files and get information stored in them for processing. One popular file format you’re bound to encounter as a Python developer is the Portable Document Format popularly known as PDF