4 PHP CURL Basic Examples – Fetch, Save, POST, GET

Welcome to a quick tutorial and examples of PHP CURL – CURL is short for client URL and in basic terms, used to make server-to-server calls.

The most basic PHP CURL process only involves opening and closing a connection to another server.

  1. $ch = curl_init();
  2. curl_setopt($ch, CURLOPT_URL, "HTTP://SITE.COM/");
  3. curl_exec($ch);
  4. curl_close($ch);

Yep, but that does absolutely nothing productive. Let us walk through a few common uses of CURL in this guide – Read on!

ⓘ I have included a zip file with all the source code at the start of this tutorial, so you don’t have to copy-paste everything… Or if you just want to dive straight in.

 

 

REALLY QUICK TUTORIAL

 

TABLE OF CONTENTS

Download & Notes PHP CURL Useful Bits & Links
The End

 

DOWNLOAD & NOTES

Firstly, here is the download link to the example code as promised.

 

EXAMPLE CODE DOWNLOAD

Click here to download all the example source code, I have released it under the MIT license, so feel free to build on top of it or use it in your own project.

 

QUICK NOTES

If you spot a bug, please feel free to comment below. I try to answer questions too, but it is one person versus the entire world… If you need answers urgently, please check out my list of websites to get help with programming.

 

 

BASIC PHP CURL EXAMPLES

All right, let us now get into the common examples of how CURL is used.

 

1) CURL FETCH RESULTS

1-fetch.php
<?php
// (A) INIT CURL
$ch = curl_init();

// (B) CURL OPTIONS
curl_setopt($ch, CURLOPT_URL, "https://en.wikipedia.org/wiki/Portal:Solar_System");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

// (C) CURL FETCH
$result = curl_exec($ch);
if (curl_errno($ch)) {
  // (C1) CURL FETCH ERROR
  echo curl_error($ch);
} else {
  // (C2) CURL FETCH OK
  $info = curl_getinfo($ch);
  echo $result; // Whatever Wikipedia returns
  print_r($info); // More information on the transfer
}

// (D) CLOSE CONNECTION
curl_close($ch);

This is pretty much a “slightly improved” version of the introduction snippet.

  • CURL has now been configured to return the results – curl_setopt($ch, CURLOPT_RETURNTRANSFER, true).
  • So when we do $result = curl_exec($ch), $result will contain whatever the remote server reponds.

 

2) CURL SAVE OR DOWNLOAD

2-save.php
<?php
// (A) INIT CURL + CREATE LOCAL FILE
$ch = curl_init();
$fh = fopen("saved.html", "w");

// (B) CURL OPTIONS
curl_setopt($ch, CURLOPT_URL, "https://en.wikipedia.org/wiki/Portal:Solar_System");
curl_setopt($ch, CURLOPT_FILE, $fh);

// (C) CURL SAVE
curl_exec($ch);
if (curl_errno($ch)) {
  // (C1) CURL SAVE ERROR
  echo curl_error($ch);
} else {
  // (C2) CURL SAVE OK
  $info = curl_getinfo($ch);
  print_r($info);
}

// (D) CLOSE CONNECTION + FILE
curl_close($ch);
fclose($fh);

Of course, we can also create a local file $fh = fopen("saved.html", "w"), and pass it into CURL curl_setopt($ch, CURLOPT_FILE, $fh). So when CURL executes, whatever results will be saved into the file – We can use this to pretty much download anything.

 

 

3) CURL POST

3-post.php
<?php
// (A) INIT CURL
$ch = curl_init();

// (B) CURL OPTIONS
curl_setopt($ch, CURLOPT_URL, "http://localhost/0-dummy.php");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, [
  "KEY" => "VALUE"
]);

// (C) CURL FETCH
$result = curl_exec($ch);
if (curl_errno($ch)) {
  // (C1) CURL FETCH ERROR
  echo curl_error($ch);
} else {
  // (C2) CURL FETCH OK
  $info = curl_getinfo($ch);
  echo $result; // Whatever Wikipedia returns
  print_r($info); // More information on the transfer
}

// (D) CLOSE CONNECTION
curl_close($ch);

CURL works both ways too – We can send data to the remote server using POST.

 

4) CURL GET

4-get.php
<?php
// (A) URL WITH QUERY STRING
$url = "http://localhost/0-dummy.php";
$params = ["KEY" => "VALUE"];
$url .= "?";
foreach ($params as $k=>$v) { $url .= urlencode($k)."=".urlencode($v)."&"; }
$url = substr($url, 0, -1);

// (B) INIT CURL
$ch = curl_init();

// (C) CURL OPTIONS
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

// (D) CURL FETCH
$result = curl_exec($ch);
if (curl_errno($ch)) {
  // (D1) CURL FETCH ERROR
  echo curl_error($ch);
} else {
  // (D2) CURL FETCH OK
  $info = curl_getinfo($ch);
  echo $result; // Whatever Wikipedia returns
  print_r($info); // More information on the transfer
}

// (E) CLOSE CONNECTION
curl_close($ch);

Lastly, here is another example of sending data using GET instead.

 

 

USEFUL BITS & LINKS

That’s all for the main tutorial, and here is a small section on some extras and links that may be useful to you.

 

INFOGRAPHIC CHEAT SHEET

Basic CURL Examples (click to enlarge)

 

LINKS & REFERENCES

 

THE END

Thank you for reading, and we have come to the end. I hope that it has helped you to better understand, and if you want to share anything with this guide, please feel free to comment below. Good luck and happy coding!

1 thought on “4 PHP CURL Basic Examples – Fetch, Save, POST, GET”

Leave a Comment

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