PHP | cURL
The cURL stands for ‘Client for URLs’, originally with URL spelled in uppercase to make it obvious that it deals with URLs. It is pronounced as ‘see URL’. 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.
<?php phpinfo(); ?> |
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:
- 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.
Example:
<?php // From URL to get webpage contents. // Initialize a CURL session. $ch = curl_init(); // Return Page contents. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); //grab URL and pass it to the variable. curl_setopt($ch, CURLOPT_URL, $url); $result = curl_exec($ch); echo $result; ?> |
Output:

Reference: http://php.net/manual/en/book.curl.php
Recommended Posts:
- How to find where the URL will redirected using cURL?
- How to insert HTML content into an iFrame using jQuery?
- How to get the child node index in JavaScript?
- How to use jQuery in Angular ?
- How to restrict input box to allow only numbers and decimal point JavaScript?
- How to get the height of device screen in JavaScript ?
- How to get the elements of one array which are not present in another array using JavaScript?
- How do you get the length of a string in JQuery?
- How to determine which element the mouse pointer move over using JavaScript ?
- How to add innerHTML to print page?
- How to get the type of DOM element using JavaScript?
- How to disable dragging an image from an HTML page using JavaScript/jQuery ?
- How to check whether an object exists in javascript ?
- How to check for IP address using regular expression in javascript?
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



