How to use CURL in PHP?

CURL- Client URL Library
CURL connects different types of servers and communicates with different types of protocols like http, https and ftp.

In order to use CURL, you need to install the libcurl package.

You can check whether it is installed or not using the phpinfo function. For this you can create a test php page like test.php

The following code will be included in that page and run on your server:

<?php
 phpinfo(); 
?> 

If CURL is installed, you will see the following:

If CURL package is enabled, then no need to install and you can directly use CURL function. Otherwise you need to install it or you can contact your hosting provider to install CURL package.

curl_init() function is used to start the CURL session. After initializing the CURL session, you have to define the CURL options via curl_setopt() function.
After defining the options, curl_exec() function is executed and it returns the response (TRUE on success or FALSE on failure).
curl_close($ch) function is used to close the CURL session.

For an example, you can refer the code snippets as below:

<?php 
 $chinit = curl_init("www.mysite.com/curl.php?option=test");
 curl_setopt($chinit, CURLOPT_HEADER, 0); 
 curl_setopt($chinit, CURLOPT_POST, 1); 
 url_setopt($chinit, CURLOPT_RETURNTRANSFER, 1); 
 $result = curl_exec($chinit); curl_close($chinit); 
 echo $result; 
?> 
This entry was posted in PHP and tagged , , , , . Bookmark the permalink.

Leave a Reply