[英]How do I submit POST for cURL in Lithium
I'm trying to create a custom Auth adapter for a legacy API. 我正在尝试为旧版API创建自定义Auth适配器。 Let's call the adapter
TR42
. 我们将其称为适配器
TR42
。 Right now, I'm debugging TR42::check()
so I'm using hardcoded values: 现在,我正在调试
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();
}
}
?>
The file http://localhost/tr42/mock_api_authenticate.php
looks like this: 文件
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>';
?>
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: 由于我的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
(
)
It's saying the request is POST but the POST array (the last empty array) is empty. 意思是请求是POST,但POST数组(最后一个空数组)为空。
What am I doing wrong? 我究竟做错了什么?
Thanks for reading :) 谢谢阅读 :)
I suggest the use of Lithium Connections
and Service
classes, to avoid writing your curl request that way. 我建议使用Lithium
Connections
和Service
类,以避免以这种方式编写curl请求。
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: 将您的旧版api连接配置(主机,端口等)提取到名为“ api”或任何其他名称的连接中,然后在适配器的
check()
主体中编写如下内容:
public function check($credentials, array $options = []) {
return Connections::get('api')->connection->post('/tr42/mock_api_authenticate.php', $credentials);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.