相关疑难解决方法(0)

Guzzle 6:没有更多的json()方法用于响应

以前在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)

还是有帮手或类似的东西?

php guzzle

153
推荐指数
5
解决办法
9万
查看次数

没有得到Guzzle的预期回应

我正在尝试构建一个端点,使用Slim PHP框架将传递给它的数据转发到API,但我无法从Guzzle请求中获取响应.

$app->map( '/api_call/:method', function( $method ) use( $app ){
    $client = new GuzzleHttp\Client([
        'base_url' => $app->config( 'api_base_url' ),
        'defaults' => [
            'query'   => [ 'access_token' => 'foo' ],
        ]
    ]);

    $request = $client->createRequest( $app->request->getMethod(), $method, [
        'query' => $app->request->params()
    ]);

    var_dump( $client->send( $request )->getBody() );

})->via( 'GET', 'POST', 'PUT', 'PATCH', 'DELETE' )->conditions( [ 'route' => '.+?' ] );`
Run Code Online (Sandbox Code Playgroud)

然后这给了我......

object(GuzzleHttp\Stream\Stream)[59]
  private 'stream' => resource(72, stream)
  private 'size' => null
  private 'seekable' => boolean true
  private 'readable' => boolean true …
Run Code Online (Sandbox Code Playgroud)

php curl slim guzzle

17
推荐指数
3
解决办法
2万
查看次数

Mailchimp addListMember 在已经存在时返回客户端错误 400 错误请求

我正在使用他们的 MailchimpMarketing\ApiClient() composer 资源向网站添加一个基本的 Mailchimp 订阅表单。添加用户似乎工作正常,但是当尝试添加已经存在的人时,我希望只有一个很好的 json 响应,以便我可以捕获该错误并将其显示给用户,但我得到以下 GuzzleHttp\Exception \客户端异常:

Client error: `POST https://us10.api.mailchimp.com/3.0/lists/xxxxxxxxxx/members` resulted in a `400 Bad Request` response:
{"type":"http://developer.mailchimp.com/documentation/mailchimp/guides/error-glossary/","title":"Member Exists","status" (truncated...)
Run Code Online (Sandbox Code Playgroud)

该文档似乎并没有真正解释太多,我也必须找到正确的方法来从堆栈溢出中捕获该错误,因为 Mailchimps 文档是......缺乏!这是代码:

try {
    $response = $mailchimp->lists->addListMember($this->settings_helper->get('mailchimp_list_id'), [
        "email_address" => $form->get_field_value('email'),
        "status" => "subscribed",
        "merge_fields" => [
            "FNAME" => $first_name,
            "LNAME" => $last_name
         ]
    ]);
    
    if ($response->getId()) {
        $this->add_json_success($this->settings_helper->get('mailchimp_success_message'));
    }
} catch (MailchimpMarketing\ApiException $e) {
    $errors[] = $e->getMessage();
} catch (ClientErrorResponseException $e) {
    $errors[] = $e->getMessage();
} catch (GuzzleHttp\Exception\ClientException $e) {
    $errors[] = $e->getMessage();
} …
Run Code Online (Sandbox Code Playgroud)

php api exception mailchimp guzzle

7
推荐指数
1
解决办法
1704
查看次数

如何在 Guzzle 中获取响应体

当我使用 Guzzle 发布请求时如何获得响应我使用"guzzle/guzzle": "^3.9", 我发布一些 url 请求并且我需要响应正文,但是使用 Guzzle 我没有发现这是响应,在使用标准 php 函数的地方file_get_contents我有响应正文。Guzzle het反应体如何?

$guzzle = new Client();
$postCell = $guzzle
    ->post($url, [], $addProtectedRangeJson)
    ->addHeader('Authorization', 'Bearer ' . $arrayAccessTokenClient)
    ->addHeader('Content-type', 'application/json')
;

$postCell
    ->send()
    ->getBody(true)
;

$contents = (string) $postCell->getBody(); // get body that I post -> my request, not response
$contents = $postCell->getBody()->getContents(); // not find getContents, ask me did you mean to call getContentMd5 
$answer = json_decode($postCell->getResponse()->getBody(), true);

return $answer;
Run Code Online (Sandbox Code Playgroud)

现在$answer

result = {Guzzle\Http\EntityBody} [7] …
Run Code Online (Sandbox Code Playgroud)

php guzzle

2
推荐指数
1
解决办法
1万
查看次数

从 Guzzle 响应访问特定属性?

我正在使用 Guzzle 来获取 HTTP 响应。如果我这样做:

$response = $res->getBody();
Run Code Online (Sandbox Code Playgroud)

我得到一个带有“电子邮件”作为属性之一的对象。但如果我这样做:

$email = $res->getBody()->email;
Run Code Online (Sandbox Code Playgroud)

或者

$email = $response->email
Run Code Online (Sandbox Code Playgroud)

我收到“电子邮件无价值”错误。我错过了什么??如何访问响应对象中的特定属性?

php guzzle

2
推荐指数
1
解决办法
1541
查看次数

标签 统计

guzzle ×5

php ×5

api ×1

curl ×1

exception ×1

mailchimp ×1

slim ×1