Facebook Twitter Instagram
    TecAdmin
    • Home
    • Ubuntu 20.04
      • Upgrade Ubuntu
      • Install Java
      • Install Node.js
      • Install Docker
      • Install LAMP Stack
    • Tutorials
      • AWS
      • Shell Scripting
      • Docker
      • Git
      • MongoDB
    • FeedBack
    • Submit Article
    • About Us
    Facebook Twitter Instagram
    TecAdmin
    Home»General Articles»How to Force Use TLS 1.2 with cURL PHP

    How to Force Use TLS 1.2 with cURL PHP

    RahulBy RahulAugust 11, 20191 Min ReadUpdated:September 29, 2020

    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.

    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
    Previous ArticleHow to Test TLS version used for PHP
    Next Article How To Install mod_cloudflare for Apache on Ubuntu

    Related Posts

    Filesystem Hierarchy Structure (FHS) in Linux

    Updated:July 1, 20222 Mins Read

    What is CPU? – Definition, Types and Parts

    3 Mins Read

    How to Install Ionic Framework on Ubuntu 22.04

    3 Mins Read

    What is the /etc/hosts file in Linux

    Updated:June 27, 20222 Mins Read

    How to Install Composer on Ubuntu 22.04

    Updated:June 24, 20223 Mins Read

    Creating DMARC Record for Your Domain

    Updated:June 29, 20223 Mins Read

    Leave A Reply Cancel Reply

    Recent Posts
    • Filesystem Hierarchy Structure (FHS) in Linux
    • How to accept user input in Python
    • What is difference between var, let and const in JavaScript?
    • What is CPU? – Definition, Types and Parts
    • What is the /etc/aliases file
    Facebook Twitter Instagram Pinterest
    © 2022 Tecadmin.net. All Rights Reserved | Terms  | Privacy Policy

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