Someone
Someone

Reputation: 10575

How to pass the values (username and password) for HTTP basic authentication in php

Iam Trying to access The Foto Flexer API in PHP. This API requires the image url, cancelurl, callbackurl.

So i have raised a request to this API. This API requires to access the image in the browser without any problem.But My site has the HTTP basic Authentication involved. Now my question is How do i pass the user name and password to the API so that it can open the Image ..

Upvotes: 1

Views: 1084

Answers (1)

David Stockton
David Stockton

Reputation: 2251

Basic auth is going to be the username and password combined with a colon (:) and then base-64 encoded and passed with a header like so:

Authorization: Basic QWxhZGRpbjpvcGVuIHNlc2FtZQ==

For PHP to generate the header you can do:

$username = 'your username';
$password = 'your password';
$authpart = $username . ':' . $password;
$authpart = base64_encode($authpart);
$header   = 'Authorization: Basic ' . $authpart;

If you are using a library like Zend, they have methods which will generate and send this already. For curl, you can send a custom header with the CURLOPT_HEADER option to curl_setopt.

<?php
require_once 'Zend/Http/Client.php';

$uri = 'address to api';

$client = new Zend_Http_Client();
$client->setUri($uri);
$client->setAuth($user, $password, Zend_Http_Client::AUTH_BASIC);

$response = $client->request(Zend_Http_Client::GET);
?>

This should get your started with Zend.

Upvotes: 1

Related Questions