Facebook Twitter Instagram
    TecAdmin
    • Home
    • FeedBack
    • Submit Article
    • About Us
    Facebook Twitter Instagram
    TecAdmin
    You are at:Home»General Articles»How to Force Use TLS 1.2 with cURL PHP

    How to Force Use TLS 1.2 with cURL PHP

    By RahulSeptember 29, 20201 Min Read

    Most of the Web/API services providers are shifting their environments to TLS 1.2 or greater. So to consume their services via PHP applications, you also need to force your application to use TLS 1.2 during making a connection. This tutorial will help you, how to use TLS 1.2 with PHP cURL.

    Advertisement

    Using TLS 1.2 with PHP CURL Forcefully

    You can add the following code to your curl requests to use TLS 1.2. Use 6 as the value of CURLOPT_SSLVERSION forces cURL to use TLS 1.2.

    Below is the sample code to force use tls 1.2 with php curl:

    1
    curl_setopt ($ch, CURLOPT_SSLVERSION, 6);

    For the example, I am using a sample script from our another articlesubmitting JSON data with cURL and PHP. In that script, we will add code to forece use of tls 1.2.

    Below is the sample script:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    <?php
    // A sample PHP Script to POST data using cURL
    // Data in JSON format
    $data = array(
        'username' => 'tecadmin',
        'password' => '012345678'
    );
    $payload = json_encode($data);
     
    $ch = curl_init('https://api.example.com/api/1.0/user/login');
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLINFO_HEADER_OUT, true);
     
    curl_setopt ($ch, CURLOPT_SSLVERSION, 6);  //Force requsts to use TLS 1.2
    curl_setopt($ch, CURLOPT_POST, true);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $payload);
     
    curl_setopt($ch, CURLOPT_HTTPHEADER, array(
        'Content-Type: application/json',
        'Content-Length: ' . strlen($payload))
    );
    $result = curl_exec($ch);
    curl_close($ch);
    ?>

    You can execute above script in webbrowser or from the command line interface.

    Conclusion

    In this tutorial, you have learned to use tls 1.2 with PHP/cURL forcefully.

    curl PHP TLS 1.2
    Share. Facebook Twitter Pinterest LinkedIn Tumblr Email WhatsApp

    Related Posts

    How to check if a file does not exist in Bash

    How to Clone All Remote Branches in Git Repository

    Setting Up Redis for PHP Session Storage on Linux

    Add A Comment

    Leave A Reply Cancel Reply

    Advertisement
    Recent Posts
    • How to check if a file does not exist in Bash
    • How to Clone All Remote Branches in Git Repository
    • Setting Up Redis for PHP Session Storage on Linux
    • Top 10 JQ Commands Every Linux Developer Should Know
    • Practical Examples of JSON Processing with JQ in Linux
    Facebook Twitter Instagram Pinterest
    © 2023 Tecadmin.net. All Rights Reserved | Terms  | Privacy Policy

    Type above and press Enter to search. Press Esc to cancel.