[英]cURL with REST API from PHP
我正在嘗試在兩個系統之間建立連接,其中一個是主要的。 連接將通過cURL提供。 我有這個功能:
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);
}
}
從我嘗試連接的系統,我只有2個結構:
必須通過在請求標頭中提供發件人的有效訪問和密鑰來驗證對REST API的每個請求。
例如:
x-api-key:D6d03y3h0iEW7Iz3xW9127a0xrh1ib
x-api-skey:sc1vcOTTFLuqjFa5u08UKtKaWl48XSqlm8jMQvrnXnuPvRjqTPgIDI6P1YcR
和第二條指令是如何要求的帖子看起來像。
我不知道出了什么問題,我總是得到這個:
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
有人可以幫助我使它工作嗎? 謝謝
添加以下行,看看它是否有效。
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 2);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.