简体   繁体   中英

cURL with REST API from PHP

I'm trying to create connection between two system, one of them is main. The connection will be provided throught cURL. I have this function:

private function build_post($url, $params, $authentificate = true) {

    //url-ify the data for the POST

    $params_string= http_build_query($params);

    $curl = curl_init($url);

    //set the url, number of POST vars, POST data
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($curl,CURLOPT_POST, count($params));
    curl_setopt($curl,CURLOPT_POSTFIELDS, $params_string);

    if ($authentificate) {
        curl_setopt($curl, CURLOPT_HTTPHEADER, array(
            'x-api-key: key1',
            'x-api-skey: key2'
        ));
    }

    //execute post
    $curl_response = curl_exec($curl);

    if ($curl_response === false) {
        $info = curl_getinfo($curl);
        curl_close($curl);
        echo('error occured during curl exec. Additioanl info: '.var_export($info));
        }
    else
    {
        echo 'Responze OK';
        curl_close($curl);
    }
}

From the system where I am trying to connect, I only have 2insctructions:

Each request to REST API must be authenticated by providing valid access & secret key of sender in request header.

for example:

x-api-key: D6d03y3h0iEW7Iz3xW9127a0xrh1ib

x-api-skey: sc1vcOTTFLuqjFa5u08UKtKaWl48XSqlm8jMQvrnXnuPvRjqTPgIDI6P1YcR

and second instruction is how should requested post look like.

I have no idea what is going wrong, all the time i get this:

array ( 'url' => 'https://rest.api.profitee.com/public/subscribe', 'content_type' => NULL, 'http_code' => 0, 'header_size' => 0, 'request_size' => 0, 'filetime' => -1, 'ssl_verify_result' => 1, 'redirect_count' => 0, 'total_time' => 0.037060000000000002662314813051125383935868740081787109375, 'namelookup_time' => 0.0003939999999999999817472395857720357525977306067943572998046875, 'connect_time' => 0.037070999999999999785504911642419756390154361724853515625, 'pretransfer_time' => 0, 'size_upload' => 0, 'size_download' => 0, 'speed_download' => 0, 'speed_upload' => 0, 'download_content_length' => -1, 'upload_content_length' => -1, 'starttransfer_time' => 0, 'redirect_time' => 0, 'certinfo' => array ( ), 'primary_ip' => '54.229.78.229', 'redirect_url' => '', )error occured during curl exec. Additioanl info: NULL

Can someone help me make it work? Thanks

Add the following lines and see if it works.

curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);  
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 2); 

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