[英]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.