Using cURL With PHP To Fetch A Webpage

/ PHP / by Paul Robinson / 2 Comments
This post was published back on February 8, 2010 and may be outdated. Please use caution when following older tutorials or using older code. After reading be sure to check for newer procedures or updates to code.

To fetch a webpage or results from a webservice such as an API using cURL is actually simpler and most people think. Let’s get straight into it.

The code here is actually more complicated than normal. However I thought I would give a few extra cURL features that generally aren’t covered.

Let’s go through a few of the more complicated lines and explain what they do.

Should the server you are connecting to require a login, you can use these lines to set the username & password. If you do not need to login to the server, you can delete them.

If you are connecting to a https:// connection you can choose whether to verify the SSL certificate of the server. Again if you do not need this or you aren’t connecting to a https:// connection you can delete this line.

The rest is hopefully explained by the comments in the code above. Data from the cURL request will be stored in $content to be used however you wish. If you have any questions, feel free to drop me a comment.

2 Comments

Older Comments
Newer Comments

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.

I'll keep your WordPress site up-to-date and working to its best.

Find out more