In PHP CURL POST tutorial, I have explained how to send HTTP GET / POST requests with PHP CURL library.
Php Curl -f
PHP provides a rich set of functions to work with APIs, and we will discuss these functions in more detail in this article. Browse the Best Free APIs List. Overview of cURL. To work with the APIs, we will use cURL and a free app that allows making HTTP requests in PHP. The cURL project offers two sub-projects.
- This tutorial explains how we can install curl and php-curl in various Linux distributions. Ubuntu 18.04, 17.10 and Debian 9.3. Login as root and update your Ubuntu system first. Apt-get install curl. Install php-curl.
- Home → PHP → Sending POST form data with php CURL This brief guide explains 2 different reasons for wanting to send POST data with curl and how to do it. It is very handy to have the abililty to arbitrarily send POST data from a form to a script.
Below are the examples covered in this article.
1) Send HTTP GET Request with CURL
2) Send HTTP POST Requests with CURL
3) Send Random User-Agent in the Requests
4) Handle redirects (HTTP 301,302)
5) Handle Errors.
Why we need PHP CURL ?
To send HTTP GET requests, simply we can use file_get_contents() method.
But sending POST request and handling errors are not easy with file_get_contents().
Sending HTTP requests is very simple with PHP CURL.You need to follow the four steps to send request.
step 1). Initialize CURL session
step 2). Provide options for the CURL session
Using Curl In Php
CURLOPT_URL -> URL to fetch
CURLOPT_HEADER -> to include the header/not
CURLOPT_RETURNTRANSFER -> if it is set to true, data is returned as string instead of outputting it.
For full list of options, check this PHP Documentation.
Php Curl Class
step 3). Execute the CURL session
step 4). Close the session
Scratch download for mac. Note: You can check whether CURL enabled/not with the following code.
1.PHP CURL GET Example
You can use the below code to send GET request.
Php Curl Get Request
2.PHP CURL POST Example
You can use the below code to submit form using PHP CURL.
How to use the function:
3.Send Random User-Agent in the Requests
You can use the below function to get Random User-Agent.
Using CURLOPT_USERAGENT, you can set User-Agent string.
4.Handle redirects (HTTP 301,302)
To handle URL redirects, set CURLOPT_FOLLOWLOCATION to TRUE.Maximum number of redirects can be controlled using CURLOPT_MAXREDIRS.
5.How to handle CURL errors
we can use curl_errno(),curl_error() methods, to get the last errors for the current session.
curl_error($ch) -> returns error as string
curl_errno($ch) -> returns error number
You can use the below code to handle errors.
For the full list of errors, refer CURL errors
cURL is a great library and tool. Quite often we'll use it to interact and fetch data from third-party API's. However, sometimes debugging requests made with cURL, specifically from a PHP application, can be tricky.
Let's say you have an integration with a third-party API and the service becomes unstable. Some requests respond as expected, while others do not. How can we determine where the code is failing?
Use Logs, Not Exceptions
Just recently, we ran into a situation where one of our cURL integrations was failing. In production environments we capture and log all exceptions using an app called Sentry. Unfortunately, in this particular situation, the exception output did not reveal very much about what was happening with our cURL requests.
To get detailed information we needed to save detailed information to a log file. This required a few additions and updates to our code.
Using Output Buffer
We start by turning on the PHP output buffer (OB) and strting the output stream. This allows us to write verbose information from cURL to the output buffer. We must do this before starting the cURL handler.
Then we needed to turn on verbose output information and define the output stream.
After the curl_exec() call you can close the stream and fetch the information from the output buffer. To correctly capture all the information from cURL you must do it in this order - first close the stream and then get the contents.
Now the $debug variable will hold a string containing data like this:
Optionally, you can append the response body to the $debug variable. We did that to see what the service was returning when a unexpected error occurred. Apps youtube for mac.
We hope you found this article helpful. Please leave a comment below if you have any questions!