Дайджест-аутентификация с использованием запроса PHP или любого другого в codeigniter

я использовал
http://requests.ryanmccue.info/ а также https://github.com/rmccue/Requests

Я использую библиотеку запросов, но любая другая библиотека также может быть предложена.

Мой код для CodeIgniter

class Home extends CI_Controller{
public function index(){
$this->load->library('PHPRequest');
$this->rest_client();
}
function rest_client(){
$user = 'myusername';
$pass = 'mypass';
$BaseApiUrl  = 'myurl';

$headers = array('Accept' => 'application/json');
$options = array('auth' => new Requests_Auth_Basic(array($user, $pass)));
$request = Requests::get($BaseApiUrl, $headers, $options);
var_dump($request->status_code);
var_dump($request->body);

}
}

Но я получаю следующую ошибку:

int(401) string(28) "HTTP Digest: Access denied. "

0

Решение

Использование Curl PHP сейчас

$options = array(
CURLOPT_URL            => $url,
CURLOPT_HEADER         => false,
CURLOPT_VERBOSE        => true,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_SSL_VERIFYPEER => false,    // for https
CURLOPT_USERPWD        => $username . ":" . $password,
CURLOPT_HTTPAUTH       => CURLAUTH_DIGEST

);
$ch = curl_init();
curl_setopt_array( $ch, $options );
try {
$raw_response  = curl_exec( $ch );
// validate CURL status
if(curl_errno($ch))
throw new Exception(curl_error($ch), 500);
// validate HTTP status code (user/password credential issues)
$status_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
if ($status_code != 200)
throw new Exception("Response with Status Code [" . $status_code . "].", 500);
} catch(Exception $ex) {
if ($ch != null) curl_close($ch);
throw new Exception($ex);
}
if ($ch != null) curl_close($ch);
return json_decode($raw_response);
0

Другие решения

Других решений пока нет …

По вопросам рекламы ammmcru@yandex.ru
Adblock
detector