How to make cURL requests with PHP using CCB API

Today’s tutorial is not necessarily one that is CCB API focused. However, you’ll find it quite helpful and insightful when reading the CCB API Documentation.

If you’ve perused the CCB API documentation, then you’ve certainly run across the term “curl” or “Curl”.  For the non-technical, the official term is cURL, but most likely you’ll see curl or Curl used interchangeably.

We don’t have time to waste.  Let’s get started with today’s tutorial and learn how a few techniques for making curl calls in PHP.

What is cURL and why use it?

Simply put, curl, supported by PHP, is a tool for transferring files and data with URL syntax via terminal or command line interface.  Whoa there…

Okay, breath. Hang in there with me before you run off and get scared because I said terminal and command line.  It’ll be okay, I promise. We’re going to make using curl easy to understand.

The interesting thing about curl is that it can be used to accomplished the following exhaustive action list:

  • download a single file
  • access files on the web
  • save cURL output to a file
  • fetch multiple files at a time
  • follow http location headers with -L option
  • continue/resume a previous download
  • limit the rate of data transfer
  • download a file only if it is modified before/after the given time
  • pass http authentication in cURL
  • download files from FTP server
  • list/download using ranges
  • upload files to ftp server
  • more information using verbose and trace option
  • get definition of a word using DICT Protocol
  • using Proxy to download a file
  • send Mail using SMTP Protocol

In addition to using curl, there are also other simple methods for fetching the content of files.

I recommend using the bulleted list of methods when authentication, making form posts, uploading files, and error handling are NOT needed.

Unfortunately, rather fortunately, CCB uses each of the aforementioned items in the previous sentence to provide secure accessing of your CCB data.

Learn how to access CCB data using PHP cURL

Well, ready or not, we’re covering two examples showing how to make curl calls or requests in PHP.

But before we’re knee deep into examples and code, you’ll need to understand the basic structure of a curl call or request. A curl call is typically comprised of the following 5 structural items:

  • curl_init – starting or initializing the curl method
  • curl_setopt – the setting of curl options; there are too many to list, but do review curl_setopt options
  • curl_exec – execute the request and fetch the results
  • curl_error – use error handling when making calls so you’re not in the dark should request fail
  • curl_close – shut’er down and free up the curl handle and memory

There is a BONUS curl option, and that option is curl_getinfo. It’s optional step only because it contains more information that you’ll ever want to know about a given curl call.

I’m not reviewing or going into great detail for each call. For the sake of time, please review the links above to understand exactly what’s going on when using each method.

Now that you have the basic structure, let’s move into the examples of GET and POST curl calls, and how to make those calls in PHP.

And that’s today’s tutorial. If you would like more information, or you would like to see curl in action and integrated with CCB API, then please review this tutorial.

Well, that’s it for this tutorial and all for now. See you back here next week at the same bat channel and time.

Making a curl GET call using php


Making a curl POST call using php


Please comment if this tutorial has helped you.