PHP and MySQL (22 Blogs)

PHP Curl Tutorial: Everything You Need To Know

Published on Jul 29,2019 13 Views

MI-new-launch

myMock Interview Service for Real Tech Jobs

myMock-mobile-banner-bg

myMock Interview Service for Real Tech Jobs

  • Mock interview in latest tech domains i.e JAVA, AI, DEVOPS,etc
  • Get interviewed by leading tech experts
  • Real time assement report and video recording

CURL is a way from where we can hit a URL from our code to get an HTML response from it. It allows you to connect with other URLs and use their responses in our code. In this PHP CURL Tutorial we will be exploring this concept in detail.

Following pointers will be covered in this article and in detail,

So let get started with PHP CURL tutorial

PHP CURL tutorial

What is CURL?

CURL stands for Client URL and it is a library that lets you make HTTP requests in PHP

First of all, we create a curl resource with curl_init() function

$curl=curl_init();

Second step is to set curl options using curl_setopt() function

curl_setopt($curl, CURLOPT_URL, 'https://www.edureka.co');

Third step is to run curl or execute HTTP requests with curl_exec()

curl_exec($curl);

Fourth step is to close curl resource with curl_close() to free up the resources

curl_close($curl);

That’s how we create curl requests

<?php
$curl=curl_init();
/*curl is going to be datatype curl resource. when we have a curl resource, we are able to use functions that are specifically designed for that resource like curl_setopt*/
curl_setopt($curl, CURLOPT_URL, 'https://www.google.com');
curl_exec($curl);
?>

Output:

After loading this file in the browser, google is loaded using curl request

Output- Php Curl-Edureka

For the https://www.edureka.co/, they actually use the HTTPS protocol and in order to load this, we are going to set our CURLOPT_SSL_VERIFYPEER to false which is one of the options available in curl.

<?php
$curl=curl_init();
curl_setopt($curl, CURLOPT_URL, 'https://www.edureka.co');
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_exec($curl);
?>

Now go to the browser and see what happens. You can see curl.php has been loaded and edureka.co is loaded dynamically.

Output- Php Curl-Edureka

Moving on with PHP CURL tutorial let us take a look at the next bit,

Download contents of a remote website to a local file

<?php
$var = curl_init();
$url = "https://in.000webhost.com/"; 
// Set the url
curl_setopt($var, CURLOPT_URL, $url);
// It would set the result output to be a string.
curl_setopt($var, CURLOPT_RETURNTRANSFER, true);
//CURLOPT_RETURNTRANSFER enables the assignment of the data that we download from the remote site to a variable. 
$output = curl_exec($var);
curl_close($var);
echo $output;
?>

Output:

It is the local version of the website provided by us

Output- Php Curl-Edureka

With this we come to an end of this article, I hope you have learned about how to insert data into MYSQL using MySQLi Object-oriented & Procedural Procedure and also how to insert multiple records into MySQL using MySQLi Object-oriented & Procedural Procedure.

If you found this PHP CURL tutorial relevant, check out the PHP Certification Training by Edureka, a trusted online learning company with a network of more than 250,000 satisfied learners spread across the globe.

Got a question for us? Please mention it in the comments section of  this article and I will get back to you.

Comments
0 Comments

Browse Categories

Subscribe to our Newsletter, and get personalized recommendations.