PHP

Curl Example In PHP.

Curl Example In PHP.

What is cURL?

The cURL stands for “CLIENT URL”. It is a library that allows clients to access the data from the Remote Server by an HTTP request in PHP. We can easily communicate with different websites and domains with the help of cURL in PHP. 

How To Enable cURL In PHP.

To use the cURL in PHP it should be enabled. You can check it by using phpinfo(). If it is not enabled open the php.ini file and search for php_curl.dll and uncomment it by removing the semicolon(;) in front of it.  

<?php phpinfo(); ?>

Some Basic cURL Function In PHP.

S.No. Function Name Description
1. curl_init()  It starts a new cURL session.
2. curl_setopt()  It defines various options for the cURL session.
3. curl_setopt($ch, option, value)  It defines the value and option for a cURL session by the “ch”  parameter.
4. curl_exec()  It executes the cURL session, actual data transfer, and HTTP request.
5. curl_close()  It is used to close the session.

Basic Example.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<?php
$url = "https://www.phpforever.com/";
$ch = curl_init();
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_URL, $url);
$res = curl_exec($ch);
echo $res;
?>
<?php $url = "https://www.phpforever.com/"; $ch = curl_init(); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_URL, $url); $res = curl_exec($ch); echo $res; ?>
<?php 
$url = "https://www.phpforever.com/"; 
$ch = curl_init(); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
curl_setopt($ch, CURLOPT_URL, $url); 
$res = curl_exec($ch); 
echo $res; 
?>

HTTP GET request to grab a webpage

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<?php
$url = 'https://domain-name/';
$curl = curl_init($url);
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_HEADER, false);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($curl);
curl_close($curl);
print $response;
?>
<?php $url = 'https://domain-name/'; $curl = curl_init($url); curl_setopt($curl, CURLOPT_URL, $url); curl_setopt($curl, CURLOPT_HEADER, false); curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); $response = curl_exec($curl); curl_close($curl); print $response; ?>
<?php 
$url = 'https://domain-name/';
$curl = curl_init($url);
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_HEADER, false);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($curl);
curl_close($curl);
print $response; 
?>

 

Auto Save Example In PHP.

Event Calendar Example In Laravel.

Quiz App In Angular.

Get Geolocation from IP Address Using PHP

 

Related Articles

Leave a Reply

Back to top button