我對 PHP 和 API 非常陌生,我的公司被要求在 PHP 網站上整合 API 呼叫。
這是客戶提供給我們的API資訊:
API手冊
一般說明:
API URL:https
://www.somedomain.com/api/ apiKey:123qwe456rty678yui
HTTP Verb:POST
Content-Type:application/x-www-form-urlencoded
1. {EMPLOYEES category}
POST /en/1001/cat/10
POST /en/1001/cat/10/page/[999]
觀察:
? 串列回傳 20 條記錄
? 獲取重新挖掘記錄: /en/1001/cat/10/page/ 2; /en/1001/cat/10/page/3; ...
? 資訊以 JSON 格式回傳
? 所有請求都應通過 POST 方法發出,api 密鑰應在請求正文中發送
輸入變數:
? apiKey => 應用密鑰
回傳引數:
retCode = “error”
retCode = “ok”
(...)
2. {NEWS category}
POST /en/2002/cat/20
POST /en/2002/cat/20/page/[999]
觀察:
? 串列回傳 20 條記錄
? 獲取重命名記錄:/en/2002/cat/20/page/2;/en/2002/cat/20/page/3; ...
? 資訊以 JSON 格式回傳
輸入變數:
? apiKey => App key
回傳引數:
retCode = “error”
retCode = “ok”
(...)
我已經制作了一個簡單的 PHP 腳本,但我得到了一個空的回傳值:
<?php
//API URL: https://www.somedomain.com/api/
//This is to test the Employees category - /en/1001/cat/10
$curl = curl_init();
$apiKey = urlencode("123qwe456rty678yui");
//$apiKey = "123qwe456rty678yui";
//it doesn't matter if this variable is urlencoded or not, as the result is always empty...
curl_setopt_array($curl, array(
CURLOPT_URL => "https://www.somedomain.com/api/en/1001/cat/10",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_SSL_VERIFYPEER => "false",
CURLOPT_POSTFIELDS => "apiKey=$apiKey",
CURLOPT_HTTPHEADER => array(
"content-type: application/x-www-form-urlencoded"
),
));
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err) {
echo "cURL Error #:" . $err;
} else {
echo "Response: " . $response;
}
?>
就像我說的,我對 PHP 和 API 很陌生,而且客戶端不是很有幫助。
基于我擁有的手動 API,我做錯了什么?$response 始終為空,即使我輸入了錯誤的 API 密鑰,它也不會出錯,而且我什至不知道我是否以正確的方式呼叫它...
提前致謝!
uj5u.com熱心網友回復:
這是一個應該從手冊作業的函式:https : //www.php.net/manual/en/function.curl-setopt-array.php#89850
<?php
function get_web_page($url, $curl_data )
{
$options = array(
CURLOPT_RETURNTRANSFER => true, // return web page
CURLOPT_HEADER => false, // don't return headers
CURLOPT_FOLLOWLOCATION => true, // follow redirects
CURLOPT_ENCODING => "", // handle all encodings
CURLOPT_USERAGENT => "spider", // who am i
CURLOPT_AUTOREFERER => true, // set referer on redirect
CURLOPT_CONNECTTIMEOUT => 120, // timeout on connect
CURLOPT_TIMEOUT => 120, // timeout on response
CURLOPT_MAXREDIRS => 10, // stop after 10 redirects
CURLOPT_POST => 1, // i am sending post data
CURLOPT_POSTFIELDS => $curl_data, // this are my post vars
CURLOPT_SSL_VERIFYHOST => 0, // don't verify ssl
CURLOPT_SSL_VERIFYPEER => false, //
CURLOPT_VERBOSE => 1 //
);
$ch = curl_init($url);
curl_setopt_array($ch,$options);
$content = curl_exec($ch);
$err = curl_errno($ch);
$errmsg = curl_error($ch) ;
$header = curl_getinfo($ch);
curl_close($ch);
// $header['errno'] = $err;
// $header['errmsg'] = $errmsg;
// $header['content'] = $content;
return $header;
}
$curl_data = "var1=60&var2=test";
$url = "https://www.example.com";
$response = get_web_page($url,$curl_data);
print '<pre>';
print_r($response);
?>
uj5u.com熱心網友回復:
您還可以嘗試使用file_get_contents函式來獲取此 JSON 資料。
像這樣的事情可以完成這項作業:
// API URL
$apiUrl = 'https://www.somedomain.com/api/en/2002/cat/20';
// Request options
$opts = [
"http" => [
'method' => 'POST',
'header' => [
'Content-Type: application/x-www-form-urlencoded',
'apiKey: 123qwe456rty678yui'
]
]
];
// Request context
$context = stream_context_create($opts);
// Get JSON
$json = file_get_contents($apiUrl, false, $context);
試試看你能不能適應它。
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/334805.html
