我无法使用Cake PHP 3中的http Client
从API获取响应
我想发送以下GET
请求,但我无法返回结果。如果使用浏览器访问此网址,我会收到回复。
http://XX.XX.XX.XX:8020/mvapireq/?apientry=newuseru&authkey=XXXXXXXXXX&u_username=NEWUSERNAME&u_password=PASSWORD&u_name=NAME&u_email=EMAIL&u_phone=PHONE&u_currency=USD&u_country=USA&u_address=x&deviceid=DEVICEID&now=555
当我尝试使用http Client
发送相同的请求时,我的响应对象始终为null
use Cake\Network\Http\Client;
use Cake\Network\Http\Response;
public function foo(){
$http = new Client();
$data = [
'apientry' => 'newuseru',
'authkey' => 'XXXXXXXX',
'u_username' => 'NEWUSERNAME',
'u_password' => 'PASSWORD',
'u_name' => 'NAME',
'u_email' => 'EMAIL',
'u_phone' => 'PHONE',
'u_currency' => 'USD',
'u_country' => 'USA',
'u_address' => 'x',
'deviceid' => 'DEVICEID',
'now' => '555'
];
$response = $http->get('http://XX.XX.XX.XX:8020/mvapireq', $data);
debug($response->body);
debug($response->code);
debug($response->headers);
}
这是debug()
的结果,在我看来,请求没有被发送。
Notice (8): Trying to get property of non-object [APP/Controller/UsersController.php, line 1159]
/src/Controller/UsersController.php (line 1159)
null
Notice (8): Trying to get property of non-object [APP/Controller/UsersController.php, line 1160]
/src/Controller/UsersController.php (line 1160)
null
Notice (8): Trying to get property of non-object [APP/Controller/UsersController.php, line 1161]
/src/Controller/UsersController.php (line 1161)
null
/src/Controller/UsersController.php (line 1163)
null
我尝试了许多不同的结构$http
方法,并且所有方法都返回了相同的结果。我真的无法弄清楚什么是错的。任何帮助都会很棒。
答案 0 :(得分:0)
您可以在hostname
的构造函数中指定port
和Client
:
<?php
use Cake\Network\Http\Client;
// ....
public function foo(){
$http = new Client([
'hostname' => 'XX.XX.XX.XX',
'port' => '8020'
]);
$data = [
'apientry' => 'newuseru',
//....
'now' => '555'
];
$response = $http->get('/mvapireq', $data);
// ...
}
检查你的系统: