Php CURL, What is CURL and how to use it?

Hello friends, Today we will learn about php CURL. CURL is used to make HTTP requests

cURL is a library that lets you make HTTP requests in PHP. Everything you need to know about it (and most other extensions) can be found in the PHP manual.

The cURL stands for ‘Client for URLs’, originally with URL spelled in uppercase to make it obvious that it deals with URLs. The cURL project has two products libcurl and curl.

  • libcurl: A free and easy-to-use client-side URL transfer library, supporting FTP, TPS, HTTP, HTTPS, GOPHER, TELNET, DICT, FILE, and LDAP. libcurl supports TTPS certificates, HTTP POST, HTTP PUT, FTP uploading, kerberos, HTTP based upload, proxies, cookies, user & password authentication, file transfer resume, HTTP proxy tunneling and many more. libcurl is free, thread-safe, IPv6 compatible, feature rich, well supported and fast.
  • curl: A command line tool for getting or sending files using URL syntax. Since curl uses libcurl, it supports a range of common internal protocols, currently including HTTP, HTTPS, FTP, FTPS, GOPHER, TELNET, DICT, and FILE.

What is PHP cURL?
The module for PHP that makes it possible for PHP programs to access curl functions within PHP. cURL support is enabled in PHP, the phpinfo() function will display in its output. You are requested to check it before writing your first simple programme in PHP.

<?php phpinfo(); ?> 

Simple Uses: The simplest and most common request/operation made using HTTP is to get a URL. The URL itself can refer to a webpage, an image or a file. The client issues a GET request to the server and receives the document it asked for.

Some basic cURL functions:

  • The curl_init() function will initialize a new session and return a cURL handle.
  • curl_exec($ch) function should be called after initialize a cURL session and all the options for the session are set. Its purpose is simply to execute the predefined CURL session (given by ch).
  • curl_setopt($ch, option, value) set an option for a cURL session identified by the ch parameter. Option specifies which option is to set, and value specifies the value for the given option.
  • curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1) return page contents. If set 0 then no output will be returned.
  • curl_setopt($ch, CURLOPT_URL, $url) pass URL as a parameter. This is your target server website address. This is the URL you want to get from the internet.
  • curl_exec($ch) grab URL and pass it to the variable for showing output.
  • curl_close($ch) close curl resource, and free up system resources.Hello friends, Today we will learn about php CURL. CURL is used to make HTTP requestsThe curl_init() function will initialize a new session and return a cURL handle.
  • curl_exec($ch) function should be called after initialize a cURL session and all the options for the session are set. Its purpose is simply to execute the predefined CURL session (given by ch).
  • curl_setopt($ch, option, value) set an option for a cURL session identified by the ch parameter. Option specifies which option is to set, and value specifies the value for the given option.
  • curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1) return page contents. If set 0 then no output will be returned.
  • curl_setopt($ch, CURLOPT_URL, $url) pass URL as a parameter. This is your target server website address. This is the URL you want to get from the internet.
  • curl_exec($ch) grab URL and pass it to the variable for showing output.
  • curl_close($ch) close curl resource, and free up system resources.

Get Request Example

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://w3path.com');
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
$body = curl_exec($ch);
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);

echo 'Status code: ', $httpCode;
echo "<br />";
echo "<br />";
echo 'Body content:';
echo "<br />";
print_r($body);

Output:

Post Request Example

<?php 


$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://reqres.in/api/users');
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);


$postData = http_build_query([
    "name" => "morpheus",
    "job" => "leader"
]);

curl_setopt($ch,CURLOPT_POST,true);
curl_setopt($ch,CURLOPT_POSTFIELDS, $postData);

$body = curl_exec($ch);
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);

// $response = curl_exec($ch);
echo 'Status code: ', $httpCode;
echo "<br />";
echo "<br />";
echo 'Body content:';
echo "<br />";
print_r($body);

Recommended Posts:

7 Comments

  1. Roberto said:

    Thanks a bunch for sharing this with all people you really understand what
    you are speaking approximately! Bookmarked.

    Please additionally visit my site =). We could have a hyperlink alternate contract among us

    Here is my webpage – cheap flights

    January 31, 2021
    Reply
  2. Berry said:

    I am in fact thankful to the holder of this web page who has shared this
    enormous post at at this place.

    Stop by my web page; cheap flights

    February 1, 2021
    Reply
  3. Dana said:

    Thank you for another fantastic post. The place else may just anyone get
    that type of information in such a perfect manner of writing?
    I’ve a presentation subsequent week, and I’m on the look for such info.

    Here is my site – cheap flights

    February 1, 2021
    Reply
  4. Hans said:

    Write more, thats all I have to say. Literally, it seems as though you relied on the video to make your point.
    You definitely know what youre talking about, why throw away
    your intelligence on just posting videos to your site when you could be giving us something informative to
    read?

    my homepage; cheap flights [tinyurl.com]

    February 2, 2021
    Reply
  5. Bryce said:

    You could definitely see your skills within the work you write.
    The sector hopes for more passionate writers like you
    who aren’t afraid to say how they believe. Always go after your heart.

    Feel free to visit my homepage … cheap flights

    February 3, 2021
    Reply
  6. Deangelo said:

    Actually when someone doesn’t be aware of afterward its up to other users that they will assist, so here it
    takes place.

    Feel free to visit my blog post :: cheap flights [tinyurl.com]

    February 3, 2021
    Reply
  7. Allison said:

    I always used to study article in news papers but now as I am a user of web so from now I am using net for content,
    thanks to web.

    my homepage – cheap flights – tinyurl.com

    February 12, 2021
    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *