Results 1 to 1 of 1

Thread: What is PHP CURL? Installation and Usage

  1. #1
    Administrator yilmaz's Avatar

    Info

    What is PHP CURL? Installation and Usage

    It contains information about the installation, use and downloading of the CURL library, which is used for obtaining information from web pages, making bots, data extraction.

    What is CURL?
    CURL (Client URL) is a software project that allows data exchange between various protocols.


    CURL is divided into two as Libcurl and CURL.


    Libcurl C, C++, Java, C# etc. It provides data exchange from various protocols by acting as an interface for programming languages.


    CURL, on the other hand, uses the Libcurl library to exchange data from the command interpreter (CMD, PowerShell, Terminal, etc.) of the operating system.


    PHP CURL installation
    You can use the commands below to check the CURL installation.

    PHP Code:
    1. <?php
    2. if (extension_loaded("curl")) {
    3.     echo "Curl is installed.";
    4. } else {
    5.     echo "Curl is not installed.";
    6. }
    7. ?>
    Demo: https://www.jdoodle.com/a/5yFC
    After enabling the CURL library, you can complete the installation by restarting the server software for the settings to take effect.

    If there is nothing of the CURL library in the PHP settings file, you can obtain the necessary files for CURL installation at https://curl.se/ .

    Using PHP CURL
    Within the PHP CURL structure, functions are defined for various operations.

    With PHP, curl_init , curl_setopt , curl_exec , curl_close functions are used in CURL operations.

    curl_init

    The function is used to start the CURL operation. Depending on the use of the function, it defines the address it receives with the parameter as the address to be connected.

    curl_setopt

    It is an important function that determines the settings to be used during CURL operations. The function provides different operations such as downloading files, extracting data, sending data.

    curl_exec

    Runs the CURL session with the settings and acts according to the CURL settings.

    curl_close

    Terminates the CURL session.

    The following example shows the site link with CURL.

    PHP Code:
    1. <?php
    2. $ch curl_init('https://www.yilmaztv.com/demo/test.html');
    3. curl_exec($ch);
    4. curl_close($ch);
    5. ?>
    Demo: https://www.jdoodle.com/a/5yFH
    The above example is made with the curl_setopt function.

    PHP Code:
    1. <?php
    2. $ch curl_init();
    3. curl_setopt($chCURLOPT_URL'https://www.yilmaztv.com/demo/test.html');
    4. curl_exec($ch);
    5. curl_close($ch);
    6. ?>
    We said that the most important process is to make the appropriate settings with curl_setopt .

    In the example below, the value retrieved is assigned to a variable.

    PHP Code:
    1. <?php
    2. $ch curl_init();
    3. curl_setopt($chCURLOPT_URL'https://www.yilmaztv.com/demo/test.html');
    4. curl_setopt($chCURLOPT_RETURNTRANSFERtrue);
    5. $result curl_exec($ch);
    6. curl_close($ch);
    7. echo htmlspecialchars($result);
    8. ?>
    Demo: https://www.jdoodle.com/a/5yFJ
    In the example , the curl_setopt function and the CURLOPT_RETURNTRANSFER setting are enabled so that the curl_exec function returns the received value.

    With the return value, it is possible to print to a file or to get the desired values ​​with the HTML parser (HTML Parser).

    CURL settings may vary depending on the operation and protocol.

    PHP CURL file download
    We said that the appropriate CURL setting should be determined according to the process in the PHP CURL usage title.

    Downloading files with CURL can be done simply as follows.

    PHP Code:
    1. <?php
    2. $ch curl_init();
    3. curl_setopt($chCURLOPT_URL'https://www.yilmaztv.com/demo/test.html');
    4. curl_setopt($chCURLOPT_RETURNTRANSFERtrue);
    5. $result curl_exec($ch);
    6. curl_close($ch);
    7. file_put_contents('test.html'$result);
    8. ?>
    Similarly, it can be done with various CURL settings and PHP file processing function.

    PHP Code:
    1. <?php
    2. $ch curl_init();
    3. $dosya fopen('test.html''w');
    4. curl_setopt($chCURLOPT_URL'https://www.yilmaztv.com/demo/test.html');
    5. curl_setopt($chCURLOPT_RETURNTRANSFERtrue);
    6. curl_setopt($chCURLOPT_FILE$dosya);
    7. $result curl_exec($ch);
    8. curl_close($ch);
    9. fclose($result);
    10. ?>
    When downloading files with CURL, it should be noted that the process will take a long time depending on the file size and the PHP command execution time limit specified in the PHP settings will be exceeded.

    When downloading files with CURL, it will be useful to change the command execution time in the PHP settings.

    I wish you a good day.


Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Members who have read this thread: 0

There are no members to list at the moment.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •