如何在PHP中使用Get url方法?

时间:2017-05-05 05:57:30

标签: php curl

$ curl 'https://api.follow.net/v3/domains/google.com' \
-u sk-4f8ae86206e39419k2h:

我有上面的参考。我不知道如何使用php来应用它。

2 个答案:

答案 0 :(得分:0)

您可以使用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 > 

现在我在终端上得到相同的输出: -

deepak@deepak-PC: /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
}

答案 1 :(得分:0)

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
        }
    }