Simple PHP script to test and use cURL

tl;dr I wrote a simple PHP script to test and use cURL while working with APIs.

curl is a command line tool and library to transfer data with many different types of servers with many different types of protocols. The most basic curl request is done with curl which triggers a HTTP request of method GET to URL and returns the server response in plain text.

PHP includes a module called cURL which uses the curl library to make custom HTTP requests. Therefore it is widely used to share data between websites, for example to POST data to a service or request data from an API.

Whenever I set up an API with PHP I need a simple way to test it. cURL is a straightforward way to do this. I wrote a simple script, which tests whether cURL is installed on the local system and is able to retrieve data from a given URL. A modified version of the script can be shared with collegues or clients easily as has not no further dependencies. Which makes it the perfect tool to quickly debug API connections on different enviroments.

Update: In a new revision of the script I have added an example to set a timout. The PHP process is open as long as the cURL request is not answered, so while running multiple requests this may cause a large load on your system if the remote server needs a long time to response. Terminate the request after a given amount of time to avoid this issue.

I did also add an example to not verify SSL certificates. This shouldn't be used on productive systems certainly. But it may be necessary to skip the verification when using self-signed certificates on testsystems.

Update 2: A word about best practices. As written above, this is a test script only. If you want to go ahead and integrate API requests in your app, you should use appropriate scripts. A generic and popular solution is Guzzle. A basic GET request in Guzzle looks like this:

$client = new \GuzzleHttp\Client();
$response = $client->request('GET', '');
echo $response->getBody();

⌛ Warning! This post is old. The information may be outdated.

No comments on this notepad. If you would like to add something, then dont hesitate to contact me via E-Mail or Twitter.