TechvBlogs
Published in

TechvBlogs

PHP Curl Get Request with Parameters Example

Mar 31, 2022, Originally published at techvblogs.com ・2 min read

What is cURL in PHP?

The Client for URLs is shortly called cURL, which was originally pronounced with a URL in uppercase to emphasize that it deals with URLs. It’s pronounced as: see URL.

cURL, which stands for client URL, is a command-line tool that developers use to transfer data to and from a server. At the most fundamental, cURL lets you talk to a server by specifying the location (in the form of a URL) and the data you want to send. cURL supports several different protocols, including HTTP and HTTPS, and runs on almost every platform. This makes cURL ideal for testing communication from almost any device (as long as it has a command line and network connectivity) from a local server to most edge devices.

cURL is also the name of the software project, which encompasses both the curl command-line tool and the libcurl development library.

The curl_exec command in PHP is a bridge to use curl from the console. curl_exec makes it easy to quickly and easily do GET/POST requests, receive responses from other servers like JSON and download files.

Role of cURL in PHP

This is a PHP module that allows PHP programs to use curl functions. When PHP’s cURL support is turned on, the phpinfo() function’s output will include cURL information. Before you write your first basic PHP program, you can double-check it.

Example:

There will be times that you will need to pull out data from a web service using PHP’s GET method. This tutorial will demonstrate how you can make a GET request using cURL.

A cURL is software you can use to make various requests using different protocols. PHP has the option to use cURL, and in this article, we’ll show several examples. This tutorial will see how we can get API data using curl to get requests.

Have a look at some built-in curl function:

curl_init();      // initializes a cURL session
curl_setopt(); // changes the cURL session behavior with options
curl_exec(); // executes the started cURL session
curl_close(); // closes the cURL session and deletes the variable made by curl_init();

PHP cURL Request Example Code

You can test cURL in your local server as it’s the same as using a standard form with an action.

$url = "https://api.github.com/users/hadley/orgs";//  Initiate curl
$ch = curl_init();
// Disable SSL verification
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
// Will return the response, if false it print the response
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
// Set the url
curl_setopt($ch, CURLOPT_URL,$url);
// Execute
$result=curl_exec($ch);
// Closing
curl_close($ch);
// Print the return data
print_r(json_decode($result, true));

Use json_decode if the return is in JSON format.

Thank you for reading this blog.

--

--

--

We are sharing Blogs that are related to 🌍 Web and 📱 Mobile Applications issues and examples of Development. We are share blogs related to Laravel, PHP, CodeIgniter, HTML, CSS, Bootstrap, Javascript, jQuery, MySQL, MongoDB, Node.js, Vue.js, Nuxt.js.

Recommended from Medium

Squash, the definitive cloud-native debugging tool

“Make it work, make it right, make it fast.” — Kent Beck

How to write complex SQL queries with universal quantification

Build Your Own Cloud Management Platform

Pastry and Tapestry

Race Conditions, Locks, Semaphores, and Deadlocks

Self-replicating and growing code in Python

How to Create Django Like Choices Field in Flask SQLAlchemy

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Smit Pipaliya

Smit Pipaliya

I am Senior Developer at ServerAvatar Technology.

More from Medium

How to upload files to Google cloud storage from a Laravel application

Custom authentication guard in Laravel Sanctum

Laravel 9 Yajra Server Side Datatables Tutorial

How To Test Mail and Database Notifications in Laravel 8