سؤال

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.

I found that Zend has a Zend_Http_Client() method which seems that it can be used for what I need, but I can't figure out how to apply it with my REST.

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

Has anyone used the Zend_Rest_Client() to connect to the REST, as well as the Zend_Http_Client() for Authentication?

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

المحلول

After countless hours, I finally managed to figure it out!

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);
...

Hopefully this can help others who encounter the same problem eventually.

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