可以一起使用Zend_Http_Client()和Zend_Rest_Client() [英] Possible to use Zend_Http_Client() and Zend_Rest_Client() together

查看:71
本文介绍了可以一起使用Zend_Http_Client()和Zend_Rest_Client()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在使用 Zend Framework 在PHP中的 REST 客户端上工作,但遇到了问题.

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

我目前正在测试两个虚拟服务;一个是"noauth",它不需要身份验证,只返回一些JSON,另一个是"auth",它需要身份验证,并返回更特定的JSON.直接从网络访问这两个文件都没有问题,例如,导航到'https://example.org/example/dummy/noauth'会在网页上显示JSON,而'https://example.org/example/dummy/auth'会显示一个对话框,要求用户登录.

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.

我的目标是能够从主Web应用程序访问那些不同的服务.所需的第一件事是用户登录到此Web应用程序.然后,他们将能够从该Web应用程序调用不同的服务.我可以使用以下命令轻松地运行noauth:

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.

我发现Zend有一个Zend_Http_Client()方法,它似乎可以用于我需要的方法,但是我不知道如何在我的REST中应用它.

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

有人使用Zend_Rest_Client()连接到REST以及Zend_Http_Client()进行身份验证吗?

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!

您必须在REST对象上调用getHttpClient(),然后再调用通常在Zend_Http_Client()中所需的函数.

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.

这是一个简单的例子:

$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.

这篇关于可以一起使用Zend_Http_Client()和Zend_Rest_Client()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆