如何在锂电池中提交POST for cURL [英] How do I submit POST for cURL in Lithium
问题描述
我正在尝试为旧版API创建自定义Auth适配器。让我们调用适配器 TR42
。现在,我正在调试 TR42 :: check()
,因此我使用硬编码值:
I'm trying to create a custom Auth adapter for a legacy API. Let's call the adapter TR42
. Right now, I'm debugging TR42::check()
so I'm using hardcoded values:
<?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
看起来像这样:
The file http://localhost/tr42/mock_api_authenticate.php
looks like this:
<?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()的输出是:
Since my cURL code submits a POST request, I'd expect my $_POST to be populated in mock_api_authenticate.php
but that's not happening because the output from TR42::check()'s print_r() is:
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数组(最后一个空数组)是空的。
It's saying the request is POST but the POST array (the last empty array) is empty.
我做错了什么?
感谢阅读:)
推荐答案
我建议使用锂连接
和服务
将您的旧api连接配置(主机,端口等)提取到名为api的连接或任何内容,然后在适配器的 check()
body,写入如下:
Extract your legacy api connection configuration (host, port, etc) to a Connection named 'api' or whatever, then, in your adapter's check()
body, write something like:
public function check($credentials, array $options = []) {
return Connections::get('api')->connection->post('/tr42/mock_api_authenticate.php', $credentials);
}
这篇关于如何在锂电池中提交POST for cURL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!