Cómo usar la autorización básica en PHP curl


Estoy teniendo problemas con la solicitud de PHP curl con autorización básica.

Aquí está el curl de la línea de comandos:

curl -H "Accept: application/product+xml" "https://{id}:{api_key}@api.domain.com/products?limit=1&offset=0"

He intentado establecer el encabezado curl de las siguientes maneras, pero no funciona

Authorization: Basic id:api_key
or 
Authorization: Basic {id}:{api_key}

Recibo la respuesta "el parámetro de autenticación en la solicitud falta o no es válido", pero he utilizado id y api_key adecuados que funcionan en la línea de comandos curl (he probado)

Por favor ayúdame.

Author: SolarBear, 2013-11-19

2 answers

Intente el siguiente código:

$username='ABC';
$password='XYZ';
$URL='<URL>';

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$URL);
curl_setopt($ch, CURLOPT_TIMEOUT, 30); //timeout after 30 seconds
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
curl_setopt($ch, CURLOPT_USERPWD, "$username:$password");
$result=curl_exec ($ch);
$status_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);   //get status code
curl_close ($ch);
 115
Author: Suhel Meman,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2016-11-28 19:00:05

Puedes probar esto,

 $ch = curl_init($url);
 ...
 curl_setopt($ch, CURLOPT_USERPWD, $username . ":" . $password);  
 ...

REF: http://php.net/manual/en/function.curl-setopt.php

 5
Author: Krish R,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2013-11-19 06:01:42