相关疑难解决方法(0)

从Guzzle中获取异常

我正在尝试从我正在开发的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)

php api functional-testing guzzle

69
推荐指数
7
解决办法
11万
查看次数

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
查看次数

创建函数以将请求发送到API

我有一个API,我正在尝试创建一个用于发送请求的函数,文档位于此处:http : //simportal-api.azurewebsites.net/Help

我考虑过在PHP中创建此函数:

function jola_api_request($url, $vars = array(), $type = 'POST') {
    $username = '***';
    $password = '***';

    $url = 'https://simportal-api.azurewebsites.net/api/v1/'.$url;

    if($type == 'GET') {
        $call_vars = '';
        if(!empty($vars)) {
            foreach($vars as $name => $val) {
                $call_vars.= $name.'='.urlencode($val).'&';
            }
            $url.= '?'.$call_vars;
        }
    }

    $ch = curl_init($url);

    // Specify the username and password using the CURLOPT_USERPWD option.
    curl_setopt($ch, CURLOPT_USERPWD, $username . ":" . $password);  

    if($type == 'POST') {
        curl_setopt($ch, CURLOPT_POSTFIELDS, $vars);
    }

    // Tell cURL to return the output …
Run Code Online (Sandbox Code Playgroud)

php api

4
推荐指数
2
解决办法
245
查看次数

标签 统计

api ×3

php ×3

guzzle ×2

exception ×1

functional-testing ×1

mailchimp ×1