2017-01-30 1 views
0

私はRESTへの呼び出しを実行するためにがつがつ食うを使用しています私はPHP(私は、Javaから来た)絶対初心者と私は例外Guzz ClientExceptionを正しく処理するにはどうしたらいいですか? PHPでJava try catchブロックのようなものに私の呼び出しを入れることはできますか?

を処理する方法に関連する次の問題を抱えています Webサービス、このような何か:

$client = new Client(); //GuzzleHttp\Client 

    $response = $client->get('http://localhost:8080/Extranet/login', 
     [ 
      'auth' => [ 
       $credentials['email'], 
       $credentials['password'] 
      ] 
     ]); 

    $dettagliLogin = json_decode($response->getBody()); 

応じて自分のWebサービスは、私は何の問題もない既存のユーザ情報をreurns場合。

ユーザーが自分のWebサービスの戻り値を存在しない場合は、このような何か:

[2017-01-30 11:24:44] local.INFO: INSERTED USER CREDENTIAL: [email protected] dddd 
[2017-01-30 11:24:44] local.ERROR: exception 'GuzzleHttp\Exception\ClientException' with message 'Client error: `GET http://localhost:8080/Extranet/login` resulted in a `401 Unauthorized` response: 
{"timestamp":1485775484609,"status":401,"error":"Unauthorized","message":"Bad credentials","path":"/Extranet/login"} 

だから、この場合には、クライアントがClientExceptionをスローするように私には思えます。

私の疑いがある:私は置くことができ、このます。$ client-> GET(...)ClientExceptionがcatchedされている場合、私はカスタム応答を作成し、それを扱うことができるようにするJava のtryキャッチブロックのようなものに? TNX

+0

http://docs.guzzlephp.org/en/latest/quickstart.html#exceptionsとhttp://docs.guzzlephp.org/en/latest/request-options.html#http-errors –

+0

http:///php.net/manual/en/language.exceptions.php – Nanne

答えて

2

あなたはのtry catchブロックに類似を使用する場合は

http://docs.guzzlephp.org/en/latest/quickstart.html#exceptions http://docs.guzzlephp.org/en/latest/request-options.html#http-errors

は、私は上記のドキュメントからのコードを摘み取られています:

use GuzzleHttp\Psr7; 
use GuzzleHttp\Exception\RequestException; 

try { 
    $client->request('GET', 'http://localhost:8080/Extranet/login'); 
} catch (RequestException $e) { 
    echo Psr7\str($e->getRequest()); 
    if ($e->hasResponse()) { 
     echo Psr7\str($e->getResponse()); 
    } 
} 

あなたが、それはこちらに記載されているよう

あなたはがつがつ食う例外を使用することができます任意の目的に応じて例外を修正して処理することができます。

関連する問題