$ curl 'https://api.follow.net/v3/domains/google.com' \
-u sk-4f8ae86206e39419k2h:
我有上面的參考。我不知道如何應用這個使用PHP。如何在PHP中使用Get url方法?
$ curl 'https://api.follow.net/v3/domains/google.com' \
-u sk-4f8ae86206e39419k2h:
我有上面的參考。我不知道如何應用這個使用PHP。如何在PHP中使用Get url方法?
The documentation for the API states that the api uses `Basic Auth` for all authentication calls.
<?php
function pre($data){
echo '<pre>',print_r($data,true),'</pre>';
}
$key='sk-4f8ae86206e39419k2h';
$headers = array(
'Content-Type:application/json',
'Authorization: Basic ' . $key . ':'
);
$url='https://api.follow.net/v3/domains/google.com';
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL,$url);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_HEADER,true);
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($curl);
curl_close($curl);
$curl=null;
pre($response);
?>
This yields
-----------
HTTP/1.1 401 Unauthorized
Date: Fri, 05 May 2017 06:21:09 GMT
Content-Type: application/json
Transfer-Encoding: chunked
Connection: keep-alive
Set-Cookie: __cfduid=d2d6c9eadce16654ddfb49d4b2546f0121493965269; expires=Sat, 05-May-18 06:21:09 GMT; path=/; domain=.follow.net; HttpOnly
Access-Control-Allow-Credentials: true
Access-Control-Allow-Headers: Content-Type, mock
Access-Control-Allow-Methods: POST,GET,PUT,PATCH,OPTIONS,DELETE
Access-Control-Allow-Origin:
Cache-Control: no-cache
Vary: Accept-Encoding,User-Agent
Server: cloudflare-nginx
CF-RAY: 35a19915adde136b-LHR
{
"error": {
"type": "invalid_request_error",
"message": "Unauthorized Request - Invalid API Key",
"premium_data_points_required": null,
"premium_data_points_cost": null
}
}
您可以使用file_get_contents(),但我不認爲它有提供外部參數的選項,例如您使用-u添加的參數。 所以更好的解決方案是使用PHP的CURL模塊。以下是示例代碼: -
<?php
$url = 'https://api.follow.net/v3/domains/google.com';
$user = "sk-4f8ae86206e39419k2h";
$pass = "xdffggg"
//open connection
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_USERNAME, $user);
curl_setopt($ch, CURLOPT_USERPWD, $pass);
//execute post
$result = curl_exec($ch);
curl_close($ch);
// Use $result as per your requirement like
var_dump($result);
還有許多其他支持的選項。
編輯1
交互模式啓用
php > $url = 'https://api.follow.net/v3/domains/google.com';
php >
php >
php > $user = "sk-4f8ae86206e39419k2h";
php >
php >
php > $pass = "";
php > $ch = curl_init();
php >
php > curl_setopt($ch, CURLOPT_URL, $url);
php > curl_setopt($ch, CURLOPT_USERNAME, $user);
php > curl_setopt($ch, CURLOPT_USERPWD, $pass);
php > $result = curl_exec($ch);
{
"error": {
"type": "invalid_request_error",
"message": "Unauthorized Request - No API Key",
"premium_data_points_required": null,
"premium_data_points_cost": null
}
}
php > curl_close($ch);
php > var_dump($result);
bool(true)
php >
現在我在終端得到相同的輸出: -
[email protected]: /temp_path $ curl 'https://api.follow.net/v3/domains/google.com' -u sk-4f8ae86206e39419k2h:
{
"error": {
"type": "invalid_request_error",
"message": "Unauthorized Request - Invalid API Key",
"premium_data_points_required": null,
"premium_data_points_cost": null
}
或許閱讀PHP手冊會一個開始的好地方? – RamRaider
是的,但我沒有得到任何想法回合,其次是價值 - > sk-4f8ae86206e39419k2h這是API密鑰。但不知道如何申請。我遇到過php.net – Ramesh