简体   繁体   English

使用Curl在Https服务器(PHP)中发布数据

[英]Using Curl to post data in Https server (PHP)

I tried to hit the api of https server using Curl in php. 我试图在php中使用Curl来访问https服务器的api。 I don't know why I am not being able to access it and I get null as a response. 我不知道为什么我无法访问它,并且得到null作为响应。

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. 好吧,当我尝试通过chrome的POSTMAN应用程序将数据发布到服务器时,我能够获得响应。 I am unable to find the solution for this issue using CURL in php. 我在php中使用CURL找不到此问题的解决方案。 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. 您可以从此处下载cacert.pem-保存到站点中目录根目录之外。

In the curl request you would do something like: 在curl请求中,您将执行以下操作:

$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 );

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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