问题
I tryed to get data from the "mobile.de Search API", but it doesn't work =/ .. this error cames every time :
HTTP Status 401 - This request requires HTTP authentication ().
.. what am I doing wrong?
$authCode = base64_encode("{Benutzername}:{Passwort}");
$uri = 'http://services.mobile.de/1.0.0/ad/search?modificationTime.min=2012-05-04T18:13:51.0Z';
$ch = curl_init($uri);
curl_setopt_array($ch, array(
CURLOPT_HTTPHEADER => array('Authorization: '.$authCode,'Accept-Language: de','Accept: application/xml'),
CURLOPT_RETURNTRANSFER =>true,
CURLOPT_VERBOSE => 1
));
$out = curl_exec($ch);
curl_close($ch);
echo $out;
As far as I can tell, I have complied with the interface description fully.
回答1:
You need to set the following curl options for a correct authorization:
curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC ); // HTTP Basic Auth
curl_setopt($curl, CURLOPT_USERPWD, $username.":".$password); // Auth String
A simplified version of my implementation:
<?
class APIProxy {
/* The access proxy for mobile.de search API */
private $username;
private $password;
private $api_base;
function __construct(){
/* Auth Data */
$this->username = '{username}';
$this->password = '{password}';
$this->api_base = 'http://services.mobile.de/1.0.0/';
}
function execute($query){
/* executes the query on remote API */
$curl = curl_init($this->api_base . $query);
$this->curl_set_options($curl);
$response = curl_exec($curl);
$curl_error = curl_error($curl);
curl_close($curl);
if($curl_error){ /* Error handling goes here */ }
return $response;
}
function get_auth_string(){
/* e.g. "myusername:mypassword" */
return $this->username.":".$this->password;
}
function curl_set_options($curl){
curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC ); // HTTP Basic Auth
curl_setopt($curl, CURLOPT_USERPWD, $this->get_auth_string()); // Auth String
curl_setopt($curl, CURLOPT_FAILONERROR, true); // Throw exception on error
curl_setopt($curl, CURLOPT_HEADER, false); // Do not retrieve header
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); // Retrieve HTTP Body
}
}
$api = new APIProxy();
$result = $api->execute('ad/search?interiorColor=BLACK');
echo $result;
?>
回答2:
A very basic non object oriented approach is using file get contents with manipulated header for accessing the search API. I share it to give a very simple example how the mobile.de API can be used. However, remember file_get_contents might be 30% - 50% slower than curl.
### Set language property in header (e.g. German) ###
$opts = array(
'http'=>array(
'method'=>"GET",
'header'=>"Accept-language: de\r\n"
)
);
$baseURL = 'http://<<username>>:<<password>>@services.mobile.de/1.0.0/ad/search?';
$searchURL .= $searchString; ## provide get parameters e.g. color=red&make=bmw
##fetch your results
$file = file_get_contents($searchURL, false, $context);
Regarding the auth data. Mobile.de provides it for free for every dealer. Just generate the auth properties in your dealer dashboard.
来源:https://stackoverflow.com/questions/19871169/mobile-de-search-api-authorization-fehler-mit-php-curl