سؤال

I'm currently working on a REST client in PHP using Zend Framework and ran into a problem.

I currently have two dummy services that I'm testing with; one is 'noauth' which requires no authentication and just returns some JSON, the other is 'auth' which required authentication and returns more specific JSON. Accessing both directly from the web works without a problem, for example navigating to 'https://example.org/example/dummy/noauth' displays the JSON on the webpage and 'https://example.org/example/dummy/auth' displays a dialog box asking for the user to log in.

My goal is to be able to access those different services from my main web application. The first thing that is required is for the user to log on to this web app. Then they'll be able to call the different services from that web application. I was able to get the noauth working quite easily using this:

$base_url = 'https://example.org';
$client = new Zend_Rest_Client($base_url);
$endpoint = '/example/dummy/noauth';
$response = $client->restGet($endpoint);

But I am unable to get the 'auth' one working without it asking for the user's username and password every time. Currently, I'm using hard coded values for this, but eventually, it'll be the values that the user used to log in that are stored in the session. Here are some of the things I've tried:

$user = base64_encode('username:password');
$client->setHeaders('Authorization: Basic '.$user);

And also:

$client = new Zend_Http_Client();
$client->setAuth('username', 'password', Zend_Http_Client::AUTH_BASIC);

None of these seem to be working. Does anyone have any ideas on how to achieve what I need?

هل كانت مفيدة؟

المحلول

You have to call the getHttpClient() on the REST object, and then call the function you need that's normally in the Zend_Http_Client() after that.

Here's a quick example:

$client = new Zend_Rest_Client($base_url);
$client->getHttpClient()->setAuth($username, $password, Zend_Http_Client::AUTH_BASIC);
...

By doing this, it'll use the username and password in the variables not ask the user every time they want to access the page.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top