Php Curl Post



Hello friends, Today we will learn about php CURL. CURL is used to make HTTP requests

If you wish to pass PHP POST array variables without submitting a form, using the PHP Curl library to another URL, you first need to have a an Array which holds all the values for the POST array. Once we have the array set, we create the variables for the destination URL and POST fields. It is important to notice that when using curl to post form data and you use an array for CURLOPTPOSTFIELDS option, the post will be in multipart format 'John', 'surname' = 'Doe', 'age' = 36) $defaults = array(CURLOPTURL = 'CURLOPTPOST = true, CURLOPTPOSTFIELDS = $params,); $ch = curlinit.

cURL is a library that lets you make HTTP requests in PHP. Everything you need to know about it (and most other extensions) can be found in the PHP manual.

The cURL stands for ‘Client for URLs’, originally with URL spelled in uppercase to make it obvious that it deals with URLs. The cURL project has two products libcurl and curl.

  • libcurl: A free and easy-to-use client-side URL transfer library, supporting FTP, TPS, HTTP, HTTPS, GOPHER, TELNET, DICT, FILE, and LDAP. libcurl supports TTPS certificates, HTTP POST, HTTP PUT, FTP uploading, kerberos, HTTP based upload, proxies, cookies, user & password authentication, file transfer resume, HTTP proxy tunneling and many more. libcurl is free, thread-safe, IPv6 compatible, feature rich, well supported and fast.
  • curl: A command line tool for getting or sending files using URL syntax. Since curl uses libcurl, it supports a range of common internal protocols, currently including HTTP, HTTPS, FTP, FTPS, GOPHER, TELNET, DICT, and FILE.

What is PHP cURL?
The module for PHP that makes it possible for PHP programs to access curl functions within PHP. cURL support is enabled in PHP, the phpinfo() function will display in its output. You are requested to check it before writing your first simple programme in PHP.

Simple Uses: The simplest and most common request/operation made using HTTP is to get a URL. The URL itself can refer to a webpage, an image or a file. The client issues a GET request to the server and receives the document it asked for.

Some basic cURL functions:

Php Curl Post
  • The curl_init() function will initialize a new session and return a cURL handle.
  • curl_exec($ch) function should be called after initialize a cURL session and all the options for the session are set. Its purpose is simply to execute the predefined CURL session (given by ch).
  • curl_setopt($ch, option, value) set an option for a cURL session identified by the ch parameter. Option specifies which option is to set, and value specifies the value for the given option.
  • curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1) return page contents. If set 0 then no output will be returned.
  • curl_setopt($ch, CURLOPT_URL, $url) pass URL as a parameter. This is your target server website address. This is the URL you want to get from the internet.
  • curl_exec($ch) grab URL and pass it to the variable for showing output.
  • curl_close($ch) close curl resource, and free up system resources.Hello friends, Today we will learn about php CURL. CURL is used to make HTTP requestsThe curl_init() function will initialize a new session and return a cURL handle.
  • curl_exec($ch) function should be called after initialize a cURL session and all the options for the session are set. Its purpose is simply to execute the predefined CURL session (given by ch).
  • curl_setopt($ch, option, value) set an option for a cURL session identified by the ch parameter. Option specifies which option is to set, and value specifies the value for the given option.
  • curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1) return page contents. If set 0 then no output will be returned.
  • curl_setopt($ch, CURLOPT_URL, $url) pass URL as a parameter. This is your target server website address. This is the URL you want to get from the internet.
  • curl_exec($ch) grab URL and pass it to the variable for showing output.
  • curl_close($ch) close curl resource, and free up system resources.

Get Request Example

Output:

Post Request Example

Recommended Posts:

When a HTTP request has been received by a server, typically a response is sent back to the client, and the server response will usually consist of two parts; the Response Header and the Response Body.

Post

The header part of the response contains all the response headers, including cookies (if any) and information about the mime type of the content. The response body contains the content, which should match the mine-type in the headers. For example, common mime-types might be text/html for HTML pages, and text/css for external StyleSheets.

To show the response headers to a given request, we can simply cut out the headers using a combination of the options and functions. A more detailed explanation of this is available in the next section of the article, for those interested.

A quick full example is included below:

The above will output something like:

The response code is always the first line that is returned in the response head.

Php Curl Post

Obtaining headers as key : value pairs

Php Curl Post

As discussed in the article on parsing response headers in PHP, we can also create an associative array containing key : value pairs. Since we already got the headers, we could do like this:

Retrieving the response headers

There is no build-in way to only return the response headers using cURL in PHP. However, we can still 'cut' them from the full response. To do this, we first determine the size of the response header, and then simply cut it from the response using the substr() function.

First, we set the CURLOPT_HEADER option true. Doing this will include the headers in the response downloaded by cURL. Next, we will need to cut out the headers.

Php Curl Post Parameters

Using this method, we can return both the body and header part of the response. However, before we can separate the components of the response, we need to get the size of the header. This can be done using curl_getinfo() with the CURLINFO_HEADER_SIZE option, as shown below:

Finally, we may use the value returned by curl_getinfo() with substr() to seperate the headers from the response message.

Create an array containing each header

In order to better work with the individual headers, we should place them in an array. HTTP Headers are separated by a Carriage Return and a Line Feed, also known as CRLF (Sometimes also represented as: [CR][LF]). Knowing this, we can create an array from the raw headers using the PHP explode() function.

As you can see, we also have a couple of empty entries. To remove those, we can pass the array to the array_filter() function:

Finally, we can easily work on the array in a loop.

Php Curl Postfields

Finished example:

Tools:

Php Curl Post File Multipart/form-data

You can use the following API endpoints for testing purposes: