Perform a simple cURL HTTP request with PHP

CURL is a popular tool for transferring data over various protocols, like http, ftp and imap to name a few. In fact it supports a lot more protocols.

PHP supports cURL as well, in the form of libcurl. A cURL library.

In this tutorial I’m going to show how you can implement cURL in your PHP project, to perform HTTP get requests.

The first thing we have to do to be able to use cURL, is – of course – initialise a new cURL session.

Next, we “CURLOPT_RETURNTRANSFER” to true. Because of this option, our cURL handle ($ch) will return the transfer as a string of the return value.

After setting this option, it’s time to set the request URL.

And it’s time to send out the http get request. The $content variable will hold the returned value. Which we can use later on. In this example, I just echo the returned value.

And for your convenience, this is the final code:

If you’re interested in performing a http get request with Python, read this tutorial.

1 Comment

  1. I have to thank you for the efforts you have put in penning this blog.
    I really hope to check out the same high-grade blog posts
    by you in the future as well. In fact, your creative writing abilities has inspired me to get my very
    own website now 😉

Leave a Reply

Your email address will not be published. Required fields are marked *

Protected with IP Blacklist CloudIP Blacklist Cloud