[英]How do I submit POST for cURL in Lithium
我正在尝试为旧版API创建自定义Auth适配器。 我们将其称为适配器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数组(最后一个空数组)为空。
我究竟做错了什么?
谢谢阅读 :)
我建议使用Lithium Connections
和Service
类,以避免以这种方式编写curl请求。
将您的旧版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.