【问题标题】:How do I submit POST for cURL in Lithium如何在 Lithium 中为 cURL 提交 POST
【发布时间】:2013-08-18 04:53:37
【问题描述】:

我正在尝试为旧版 API 创建自定义身份验证适配器。我们称适配器为TR42。现在,我正在调试TR42::check(),所以我使用的是硬编码值:

<?php
class TR42 extends \lithium\core\Object {

    public function __construct(array $config = []) {
        $defaults = [
            'scheme' => 'http',
            'host' => 'localhost/tr42/mock_api_authenticate.php',
            'action' => 'authLookup',
            'fields' => ['username', 'password'],
            'method' => 'POST'
        ];
        parent::__construct($config + $defaults);
    }

    public function check($credentials, array $options = []) {
        $postConfig = [
            /**
             * Should I be using 'body' or 'query' to submit POST fields?
             */
            'body' => [
                'username' => 'housni',
                'password' => sha1('legacyHashedPassword'),
                'action' => 'authLookup'
            ],
            'query' => 'username=housni&password=' . sha1('legacyHashedPassword') . '&action=authLookup',
        ];
        $request = new Request($postConfig + $this->_config);

        $stream = new Curl($this->_config);
        $stream->open();
        $stream->write($request);
        $response = $stream->read();
        $stream->close();

        echo '<pre>' . print_r($response, true) . '</pre>';
        die();
    }
}
?>

文件http://localhost/tr42/mock_api_authenticate.php 如下所示:

<?php
echo '<h1>This request is: ' . $_SERVER['REQUEST_METHOD'] . '</h1>';
if (!empty($_POST)) {
    echo '<h1>YAY</h1>';
} else {
    echo '<h1>NAY</h1>';
}

echo '<pre>' . print_r($_POST, true) . '</pre>';
?>

由于我的 cURL 代码提交了一个 POST 请求,我希望我的 $_POST 被填充到 mock_api_authenticate.php 但这并没有发生,因为 TR42::check() 的 print_r() 的输出是:

HTTP/1.1 200 OK
Date: Sun, 18 Aug 2013 04:46:34 GMT
Server: Apache/2.2.14 (Ubuntu)
X-Powered-By: PHP/5.4.17-1~lucid+1
Vary: Accept-Encoding
Content-Length: 63
Connection: close
Content-Type: text/html

This request is: POST

NAY

Array
(
)

表示请求是 POST,但 POST 数组(最后一个空数组)为空。

我做错了什么?

感谢阅读:)

【问题讨论】:

    标签: php post curl lithium


    【解决方案1】:

    我建议使用 Lithium ConnectionsService 类,以避免以这种方式编写您的 curl 请求。

    将您的旧 api 连接配置(主机、端口等)提取到名为“api”或其他任何内容的连接中,然后在适配器的 check() 正文中编写如下内容:

    public function check($credentials, array $options = []) {
        return Connections::get('api')->connection->post('/tr42/mock_api_authenticate.php', $credentials);
    }
    

    【讨论】:

    • 我不确定我是否理解它们是如何组合在一起的。请您详细说明一下好吗?你是说 TR42 应该驻留在extensions\adapter\security\auth\TR42apitype = Httpadapter = TR42 的连接应该实施该检查吗?我想让我失望的是连接如何有一个post() 方法,它似乎来自Service。我对锂的某些部分还是很陌生:)
    • 另外,这个TR42适配器只是一个身份验证API,不会用作数据源。
    • 据我了解,您将拥有一个实现 check() 方法的 TR42 身份验证适配器,您需要将凭据数据发布到某个地方的 http 端点。您可以在那里实例化一个服务对象并进行调用,或者创建一个 HTTP 数据源,您将为其配置一个连接(使用 Connections::add)。 HTTP 数据源包含一个 Service 实例,可让您调用 post() 方法(或 send('post', ...))。见github.com/UnionOfRAD/lithium/blob/master/data/source/…
    • 嗯,我明白了。我会走Creating Data Sources的路线。谢谢你的建议。我会发布一些代码,一旦我完成(可能明天),以防它帮助某人。
    猜你喜欢
    • 2018-09-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-04-05
    • 2016-09-15
    • 1970-01-01
    相关资源
    最近更新 更多