简体   繁体   中英

Using Curl to post data in Https server (PHP)

I tried to hit the api of https server using Curl in php. I don't know why I am not being able to access it and I get null as a response.

My basic code is like,

<?php

    $url = "https://my_website";
     $data = array(
        'groupName' => 'Test',
        'units' => 1,
        'principalEmail' => 'todd.bailey+onboard@rentmoola.com',
        'principalFirstName' => 'Todd',
        'principalLastName' => 'Bailey',
        'principalPhone' => 1212,
        'principalSSN' => 2121,
    );

    $jsonStringPOST = json_encode($data);
    $login = array('username'=>'some_user_name','password'=>'some_password');
    $headers = array('Accept: application/json','Content-Type: application/json');

    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_POST, TRUE);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); //for solving certificate issue
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); 

    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
    curl_setopt($ch, CURLOPT_USERPWD, $login['username'] . ':' . $login['password']);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $jsonStringPOST);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);

    $resultPOST = curl_exec($ch);

    if($resultPOST === false)
    {
        var_dump('Curl error: ' . curl_error($ch));

    } 

?>

The error I get is like:

'Curl error: error: 1408D13A:SSL routines: SSL3_GET_KEY_EXCHANGE:unable to find ecdh parameters.

Well, I was able to get response when trying to post data into server via POSTMAN application of chrome. I am unable to find the solution for this issue using CURL in php. Any suggestions or answers will be very appreciable.

Thank You.

You can download cacert.pem from here - save to your site, outside of the directory root.

In the curl request you would do something like:

$cacert=realpath('/path/to/cacert.pem');
curl_setopt( $ch, CURLOPT_CAINFO, $cacert );
curl_setopt( $ch, CURLOPT_SSL_VERIFYHOST, 1 );
curl_setopt( $ch, CURLOPT_SSL_VERIFYPEER, false );

curl_setopt( $ch, CURLOPT_USERAGENT, 'Mozilla-da-gorilla' );
curl_setopt( $ch, CURLOPT_AUTOREFERER, true );

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM