Free cookie consent management tool by TermsFeed Generator PHP curl | Amir Kamizi
AMIR KAMIZI
Home Blog Courses Books Newsletter Store Membership Buy me a coffee
PHP curl

PHP curl

Last Updated on Mar 22, 2023

Introduction

Sometimes we need to send requests to another url to get data, maybe another API. So how can we do that in PHP?

There are many ways but today we are going to talk about curl.

Curl

In order to work with curl in php you need to know 4 functions

  1. Curl_init
  2. Curl_setopt_array
  3. Curl_exec
  4. Curl_close

1. Curl_init

initializes the curl request and returns an object that you would need for sending the request.

2. Curl_setopt_array

Then you should add options to your request with curl_setopt_array function. The first argument is the curl object that was returned from the first function and the second argument is an array of all the options

Some of the most common options are:

CURLOPT_URL: url that you are sending the request to

CURLOPT_TIMEOUT: maximum seconds that you are willing to wait. Default is 30 seconds

CURLOPT_CUSTOMREQUEST: what type of request are you sending. Post, get, delete, etc.

CURLOPT_HTTPHEADER: headers like accept and content type goes here.

3. Curl_exec

After setting the options it’s time to send the request. You can do it by running curl_exec. This will get the returned object from the first function as an argument. This function returns the response you’ve received.

4. Curl_close

Then you should close  the connection by running curl_close. Again it need the curl object as the argument.

Now let’s see all of it together.

// 1. initialize
$curl = curl_init();
$headers = [
//    'Accept: application/json',
//    'Content-Type: application/xml'
];
$requestType = 'POST'; // GET POST DELETE etc.
$turl = 'https://example.com';
curl_setopt_array($curl, array(
    CURLOPT_URL => $url,
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_ENCODING => '',
    CURLOPT_MAXREDIRS => 10,
    CURLOPT_TIMEOUT => 30,
    CURLOPT_FOLLOWLOCATION => false,
    CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
    CURLOPT_CUSTOMREQUEST => $requestType,
    CURLOPT_POSTFIELDS => '',
    CURLOPT_HTTPHEADER => $headers,
));

$response = curl_exec($curl);

curl_close($curl);
echo $response;

https://youtu.be/YCTz-QEbAJk

Conclusion

Now you know about curl in PHP.

I recommend you to open a PHP files and try send a request to any website. see if you can get a response.

If you have any suggestions, questions, or opinions, please contact me. I’m looking forward to hearing from you!

Key takeaways

  • send request
  • curl
  • curl init
  • curl open and curl close
  • curl exec

Category: programming

Tags: #php

Join the Newsletter

Subscribe to get my latest content by email.

I won't send you spam. Unsubscribe at any time.

Related Posts

PHP Operators
Mar 21, 2023 programming

PHP Operators

Today we are going to learn about Operators in PHP including arithmetic, comparison and logic operators. You will be using all of these operators during your career a lot. ...

5 Min Read Read More
PHP Conditionals
Mar 21, 2023 programming

PHP Conditionals

Today we are going to learn about conditionals in PHP. Conditionals are very useful in any programming language. So important that we can argue that the foundation for all the technologies around us are conditions. ...

7 Min Read Read More
PHP Beginner to Advanced 2025: The Complete Guide to Mastering PHP and Building Modern Web Applications
Jul 17, 2025 programming

PHP Beginner to Advanced 2025: The Complete Guide to Mastering PHP and Building Modern Web Applications

PHP is often called the engine room of the modern web. Despite debates about newer technologies like Node.js, Go, or Python’s Django, PHP has remained a consistent favorite among developers. In fact, as of 2025, PHP powers over 75% of all websites that use a known server-side programming language. ...

9 Min Read Read More
Laravel 12 Beginner to Advanced: Complete Guide
Jul 17, 2025 programming

Laravel 12 Beginner to Advanced: Complete Guide

Laravel is a name that resonates throughout the PHP community. With its elegant syntax, robust ecosystem, and developer-friendly tools, Laravel has become the go-to framework for modern web development. Laravel 12, released on February 24, 2025, continues this tradition, streamlining workflows and boosting performance with support for the latest PHP advancements. ...

20 Min Read Read More

Recommended Courses

Introduction to Machine Learning in PHP

Introduction to Machine Learning in PHP

Learn to Build Different Machine Learning Models Easily ...

PHP Tutorial Beginner to Advanced

PHP Tutorial Beginner to Advanced

Learn everything you need to start a successful career as a PHP developer ...