在Cake 3中创建http客户端 [英] Creating a http Client in Cake 3

查看:258
本文介绍了在Cake 3中创建http客户端的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在使用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

 使用Cake \Network\Http \Client; 
使用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);
}


$ b

这是来自 debug / code>在我看来,请求不发送。

 注意非对象的属性[APP / Controller / UsersController.php,line 1159] 

/src/Controller/UsersController.php(行1159)

null

注意(8):尝试获取非对象的属性[APP / Controller / UsersController.php,第1160行]

/src/Controller/UsersController.php(第1160行)

null

注意(8):尝试获取非对象的属性[APP / Controller / UsersController.php,行1161]

/ src / Controller / UsersController.php(line 1161)

null

/src/Controller/UsersController.php(line 1163)

null

我尝试了许多不同的结构化方法 $ http ,并且它们都返回了相同的结果。我真的不知道什么是错的。

解决方案

您可以指定 hostname code>:

port > <?php
使用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);
// ...
}

并检查您的系统: p>


  • 防火墙(os)

  • allow_url_fopen(在您的php运行时配置中)


I'm having trouble getting a response from an API using a http Client in Cake PHP 3.

I want to send the following GET request but I cannot manage to return a result. If visit this url with a browser i get a response.

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

When I try to send the same request with http Client my reponse object is always 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);
}

This is the results from the debug() it seems to me like the request is not being sent.

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

I've tried many different ways of structuring $http and all of them have returned the same result. I really can't figure out whats going wrong. Any help would be great.

解决方案

You can specify hostname and port in the constructor of the 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);
 // ...
}

And check your system:

  • firewall (os)
  • allow_url_fopen (in your php runtime configuration)

这篇关于在Cake 3中创建http客户端的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆