2015-07-03 195 views
1

我有代码可以使用身份验证将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" 

回答

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