通过身份验证获取请求

时间:2015-07-03 15:19:36

标签: php json rest

我的代码可以使用身份验证将JSON发布到API。我需要使用身份验证进行GET,但大多数示例都不提供此信息。

POST

public function putTest() {
    $username    = "user";
    $password    = "pass";
    $json_url    = "http://api.of.site.com";
    $json_string = "[]";

    $ch      = curl_init( $json_url );
    $options = array(
        CURLOPT_RETURNTRANSFER => true,
        CURLOPT_USERPWD        => "{$username}:{$password}",
        CURLOPT_HTTPHEADER     => array( "Content-type: application/json" ),
        CURLOPT_POSTFIELDS     => $json_string
    );
    curl_setopt_array( $ch, $options );

    $result = curl_exec( $ch );
}

GET(不工作)

public function getTest() {
    $username    = "user";
    $password    = "pass";
    $json_url    = "http://api.of.site.com";

    $ch      = curl_init( $json_url );
    $options = array(
        CURLOPT_RETURNTRANSFER => true,
        CURLOPT_USERPWD        => "{$username}:{$password}",
        CURLOPT_HTTPHEADER     => array( "Content-type: application/json" ),
    );
    curl_setopt_array( $ch, $options );

    $result = curl_exec( $ch );
}

我的GET中缺少什么?另外,我想保持这种风格,而不是使用curl_setopt

修改 作为参考,这是命令行版本的工作

curl -X GET -H "Content-Type: application/json" -H "Accept: application/json" -u "username:pass" "http://api.site.name.com/thingtoget"

3 个答案:

答案 0 :(得分:1)

您需要使用CURLOPT_HTTPAUTH

  

使用CURLOPT_HTTPAUTH指定HTTP的身份验证方法   基础连接

http://curl.haxx.se/libcurl/c/CURLOPT_USERPWD.html

答案 1 :(得分:1)

您需要添加:

curl_setopt($curl_handle, CURLOPT_USERPWD, $username . ':' . $password);

一个例子:

public function call(array $data)
{
    $payload = json_encode($data['payload']);

    // Initialise the session
    $curl = curl_init();

    // Set API URL
    curl_setopt($curl, CURLOPT_URL, $data['url']);
    // Return the transfer as a string of the return value of curl_exec() instead of outputting it out directly
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
    // Include the header in the output
    curl_setopt($curl, CURLOPT_HEADER, true);
    // Authentication
    curl_setopt($curl_handle, CURLOPT_USERPWD, 'your_username:and_password');
    // Set request method
    curl_setopt($curl, CURLOPT_CUSTOMREQUEST, $data['method']);

    if ($data['method'] == 'POST') {
        // Do a regular HTTP POST
        curl_setopt($curl, CURLOPT_POST, true);
    }

    if ($data['method'] == 'POST' || $data['method'] == 'PUT') {
        // The full data to post in a HTTP POST operation
        curl_setopt($curl, CURLOPT_POSTFIELDS, $payload);
        curl_setopt($curl, CURLOPT_HTTPHEADER, array(
            'Content-Type: application/json',
            'Content-Length: ' . strlen($payload)
        ));
    }

    // Execute the request
    $output = curl_exec($curl);
    // Close curl resource to free up system resources
    curl_close($curl);

    // If connection failed
    if (! $output) {
        return 'Curl connection failure.';
    }

    // Output the profile information, includes the header
    return $output;
}

答案 2 :(得分:1)

所以POST需要CURLOPT_HTTPHEADER => array( "Content-type: application/json" ),,而GET需要CURLOPT_HTTPHEADER => array( "Accept: application/json" ),现在读它是有意义的。

public function getTest() {
    $username    = "user";
    $password    = "pass";
    $json_url    = "http://api.of.site.com";

    $ch      = curl_init( $json_url );
    $options = array(
        CURLOPT_SSL_VERIFYPEER => false,
        CURLOPT_RETURNTRANSFER => true,
        CURLOPT_USERPWD        => "{$username}:{$password}",
        CURLOPT_HTTPHEADER     => array( "Accept: application/json" ),
    );
    curl_setopt_array( $ch, $options );

    $result = curl_exec( $ch );
}