0

I have basic authentication enabled on an Apache server. The server is hosting an API that I implemented and I want to do is call this API from my PHP script. I got as far as figuring out how to create a header:

$user = 'my_name_api';
$pwd = 'xxxxxxxxx';
$auth_string = $user . ':' . $pwd;
$auth_b64 = base64_encode($auth_string);
$header   = 'Authorization: Basic ' . $auth_b64;

How do I include the $header in my API calls? I am looking for something other than cURL, and I am NOT using any environments like zend, etc. (saw some example for Zend, etc., but I am not using any of those).

5
  • Are you running this PHP code on the same server as the API? You'd be better off invoking the php code directly, rather than doing full-blown local http call. Commented Nov 13, 2012 at 19:41
  • cURL is not available on all PHP installations. Commented Nov 13, 2012 at 19:41
  • cUrl is not an option for my server Commented Nov 13, 2012 at 19:48
  • I am running this on the same server as the API. I am not sure what you mean by invoking the code directly. Commented Nov 13, 2012 at 19:49
  • @user1765002: instead of doing an http request to the same server, which is highly inefficient, simply load up the api code directly with include() or whatever, and call whatever functions you need directly. Commented Nov 13, 2012 at 21:00

2 Answers 2

1

Have you tried fopen / file_get_contents?

Start here: http://php.net/manual/en/function.fopen.php

or here: http://www.php.net/manual/en/function.file-get-contents.php

and let me know how it goes...

airyt

Sign up to request clarification or add additional context in comments.

Comments

1

If you don't need SSL or proxy support, you can use file_get_contents() with a stream context. The stream context can contain HTTP headers:

$opts = array
(
  'http'=>array
  (
    'method' => "GET",
    'header' => "Authorization: ..."
  )
);
$context = stream_context_create($opts);
$file = file_get_contents('http://www.example.com', false, $context);

More on this here.

3 Comments

I don't have a proxy issue, but the server requires SSL. Can't I replace example.com with example.com?
Only if you have the openssl lib installed with PHP.
I checked with the server admin, and he said that openssl is built into the standard apache release, however I believe that http is still available

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.