有没有人知道post使用JSON 的正确方法Guzzle?
$request = $this->client->post(self::URL_REGISTER,array(
'content-type' => 'application/json'
),array(json_encode($_POST)));
Run Code Online (Sandbox Code Playgroud)
我收到internal server error服务器的回复.它适用于Chrome Postman.
以前在Guzzle 5.3中:
$response = $client->get('http://httpbin.org/get');
$array = $response->json(); // Yoohoo
var_dump($array[0]['origin']);
Run Code Online (Sandbox Code Playgroud)
我可以轻松地从JSON响应中获取PHP数组.现在在Guzzle 6中,我不知道该怎么做.似乎没有json()方法了.我(很快)从最新版本中读取了文档,但没有找到任何关于JSON响应的内容.我想我错过了一些东西,也许有一个我不理解的新概念(或者我没有正确阅读).
这是(下面)新方式的唯一途径吗?
$response = $client->get('http://httpbin.org/get');
$array = json_decode($response->getBody()->getContents(), true); // :'(
var_dump($array[0]['origin']);
Run Code Online (Sandbox Code Playgroud)
还是有帮手或类似的东西?
我正在尝试围绕我公司正在开发的api编写一个包装器.它很安静,使用Postman我可以发送一个帖子请求到一个端点,比如http://subdomain.dev.myapi.com/api/v1/auth/使用用户名和密码作为POST数据,我会收到一个令牌.一切都按预期工作.现在,当我尝试从PHP执行相同操作时,我会返回一个GuzzleHttp\Psr7\Response对象,但似乎无法在其中的任何位置找到令牌,就像我对Postman请求所做的那样.
相关代码如下:
$client = new Client(['base_uri' => 'http://companysub.dev.myapi.com/']);
$response = $client->post('api/v1/auth/', [
'form_params' => [
'username' => $user,
'password' => $password
]
]);
var_dump($response); //or $resonse->getBody(), etc...
Run Code Online (Sandbox Code Playgroud)
上面代码的输出看起来像(警告,传入文本墙):
object(guzzlehttp\psr7\response)#36 (6) {
["reasonphrase":"guzzlehttp\psr7\response":private]=>
string(2) "ok"
["statuscode":"guzzlehttp\psr7\response":private]=>
int(200)
["headers":"guzzlehttp\psr7\response":private]=>
array(9) {
["connection"]=>
array(1) {
[0]=>
string(10) "keep-alive"
}
["server"]=>
array(1) {
[0]=>
string(15) "gunicorn/19.3.0"
}
["date"]=>
array(1) {
[0]=>
string(29) "sat, 30 may 2015 17:22:41 gmt"
}
["transfer-encoding"]=>
array(1) {
[0]=>
string(7) "chunked"
}
["content-type"]=>
array(1) {
[0]=> …Run Code Online (Sandbox Code Playgroud) 当服务器返回4xx和5xx状态代码时,我想处理Guzzle的错误.我提出这样的请求:
$client = $this->getGuzzleClient();
$request = $client->post($url, $headers, $value);
try {
$response = $request->send();
return $response->getBody();
} catch (\Exception $e) {
// How can I get the response body?
}
Run Code Online (Sandbox Code Playgroud)
$e->getMessage返回代码信息,但不返回HTTP响应的主体.我如何获得响应机构?
如何使用GuzzleHttp(5.0版)发布帖子请求.我正在尝试执行以下操作并收到错误
$client = new \GuzzleHttp\Client();
$client->post(
'http://www.example.com/user/create',
array(
'email' => 'test@gmail.com',
'name' => 'Test user',
'password' => 'testpassword'
)
);
Run Code Online (Sandbox Code Playgroud)
PHP致命错误:未捕获异常'InvalidArgumentException',消息'没有方法可以处理电子邮件配置密钥'
我正在尝试从我正在开发的API上运行的一组测试中捕获异常,并且我使用Guzzle来使用API方法.我已经将测试包装在try/catch块中,但它仍然会抛出未处理的异常错误.按照文档中的描述添加事件侦听器似乎没有做任何事情.我需要能够检索具有500,401,400的HTTP代码的响应,实际上任何不是200的响应,因为如果系统不起作用,系统将根据调用的结果设置最合适的代码.
当前的代码示例
foreach($tests as $test){
$client = new Client($api_url);
$client->getEventDispatcher()->addListener('request.error', function(Event $event) {
if ($event['response']->getStatusCode() == 401) {
$newResponse = new Response($event['response']->getStatusCode());
$event['response'] = $newResponse;
$event->stopPropagation();
}
});
try {
$client->setDefaultOption('query', $query_string);
$request = $client->get($api_version . $test['method'], array(), isset($test['query'])?$test['query']:array());
// Do something with Guzzle.
$response = $request->send();
displayTest($request, $response);
}
catch (Guzzle\Http\Exception\ClientErrorResponseException $e) {
$req = $e->getRequest();
$resp =$e->getResponse();
displayTest($req,$resp);
}
catch (Guzzle\Http\Exception\ServerErrorResponseException $e) {
$req = $e->getRequest();
$resp =$e->getResponse();
displayTest($req,$resp);
}
catch (Guzzle\Http\Exception\BadResponseException $e) {
$req = $e->getRequest();
$resp …Run Code Online (Sandbox Code Playgroud) 我想使用Guzzle进行基本的访问认证,我对编程很新.我不知道该怎么做.我试图用curl做这个,但我的环境需要使用guzzle.
在我的应用程序中,我最初开始使用cURL从各种API中检索数据.今天,我尝试使用Guzzle来完成同样的任务.到目前为止,cURL和Guzzle似乎同样有效.
从Github来看,很多人似乎都喜欢Guzzle,但我并不真正理解为什么.
我的问题:
对于我的情况(从各种API检索数据),是否更喜欢使用Guzzle?如果我使用cURL代替Guzzle(反之亦然),我最终会后悔吗?
我正在使用PHP/Laravel.
这应该是一件容易的事情.我可以在Guzzle 3中找到很多关于如何操作的参考资料,但它们在Guzzle 5中不起作用.
到目前为止我在做什么:
$this->client = new GuzzleClient(['defaults' => [
'verify' => 'false'
]]);
Run Code Online (Sandbox Code Playgroud)
当我发送请求时,我收到此错误:
RequestException in RequestException.php line 51:
SSL CA bundle not found: false
Run Code Online (Sandbox Code Playgroud)
我在谷歌上找不到任何有用的参考这个错误.如果我可以访问curl选项,那么我可以尝试类似这里建议的解决方案(适用于Guzzle 3,因此它不起作用):http://inchoo.net/dev-talk/symfony2-guzzle -ssl-self-signed-certificate /,其相关部分是:
$req->getCurlOptions()->set(CURLOPT_SSL_VERIFYHOST, false);
$req->getCurlOptions()->set(CURLOPT_SSL_VERIFYPEER, false);
Run Code Online (Sandbox Code Playgroud) 我一直在寻找大约2个小时,我无法弄明白如何阅读最终的回应uri.
在先前版本的PHP Guzzle中,您只需致电即可$response->getEffectiveUrl()获得它.
我期望在新版本中有类似的东西,所以最终代码如下所示:
$response = $httpClient->post('http://service.com/login', [
'form_params' => [
'user' => $user,
'padss' => $pass,
]
]);
$url = $response->getEffectiveUrl();
Run Code Online (Sandbox Code Playgroud)
但是在最新版本$response中现在是一个GuzzleHttp\Psr7\Response并且没有方法可以让我检索uri.
我在这里阅读了重定向(http://guzzle.readthedocs.org/en/latest/quickstart.html#redirects),但它没有说明
谢谢@YauheniPrakopchyk