What is cURL? How to enable or disable cURL? How to Use cURL in Php

What is cURL? How to enable or disable cURL? How to Use cURL in Php

cURL Introduction

  • What Is cURL: cURL is a Command line tool And its used to fetch and transfer the file across the website.cURL library support in PHP so we can use in PHP.User can easy to find cURL version like curl_version(); This is Also supported approx 20 protocol like FTP, Http, Https, Telnet ext.

cURL Enable Method

Method 1:-How to enable cURL Using Php.ini file.

  1.  Find PHP.ini file

(your Wamp Install Drive/wamp/bin/apache/apache(version name like 2.2.17)/bin/php.ini file)

  1. Open the PHP.ini in any editor like Notepad++,Notepad.
  2. Search or find the following: ‘;extension=php_curl.dll’
  3. Uncomment this by removing the semi-colon ‘;’ (Like extension=php_curl.dll)
  4. Save and Close PHP.ini
  5. Restart Apache

Method 2:-How to enable cURL Using WAMP

  1. Left-click on the WAMP server icon in the bottom right of the screen
  2. PHP -> PHP Extensions -> php_curl

Check Curl Enable or not Using Phpinfo();

  • goto localhost page and Click phpinfo() Page.
  • after open phpinfo() page Find cURl Support enable or not. If enable cUrl Running your system otherwise enable it.

How does the cURL work?

Step 1: Curl Initialize with a command line.

$ch = curl_init();

Step 2 : Set your Url in $url variable.

(like i want to access google.com so $url=’https://www.google.com’)

curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

(Note :- if CURLOPT_RETURNTRANSFER set to true means curl give the return value and  CURLOPT_RETURNTRANSFER set to false means curl don’t

return any value.)

See This: How to Create osTicket Through API ?

curl_setopt($ch, CURLOPT_POSTFIELDS, $body);

  • $body=firstname=”test firstname”& lastname=”testlast name”.

(Note : if You want to POST any data through Curl used above command.)

curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);

  • CURLOPT_SSL_VERIFYPEER verify the peer’s SSL certificate.
  • This option determines whether curl verifies the authenticity of the peer’s certificate. A value of 1 means curl verifies; 0 (zero) means it doesn’t verify.curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);In this Command, the user set the maximum time( in seconds) taken for connecting to the server.

Example:-

$timeout=60;//60 second

curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);

This example curl Connection to your URL within 60 seconds if connection not established successfully then exit and give error Timeout.

curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);

The main difference between CURLOPT_CONNECTTIMEOUT and CURLOPT_TIMEOUT is Connection timeout is used for the Connecting to the server time and CURLOPT_TIMEOUT is how much time connection for data GET, POST or processing data on the server after that time Disconnection to the server.

curl_setopt($ch, CURLOPT_USERAGENT, $userAgent);

  • The user can manually write the user agent some server many time disable the request if you don’t write any user agent.

Example of Useragent:

1) Chrome : Mozilla/5.0(Windows NT 6.1)AppleWebKit/537.2(KHTML,like Gecko)Chrome/22.0.1216.0 safari/537.2

2) internet Explorer 10: Mozilla/1.22(compatible;MSIE 10.0;Windows 3.1).

More Info About User-agent visit site. http://thisinterestsme.com/php-set-curl-user-agent/

Step 4: Execute Curl.

$data = curl+exec($ch);

  • Above command Execute the curl request and stored output to your appropriate variable(if CURLOPT_RETURNTRANSFER, true or no any error).

Step 5: Exit cURL.

  • After run cURL, you can exit through the above command.

Errors In cURL:-

Leave a comment

PORTFOLIO