workerman/http-client 如何友好的拿到 错误码和错误信息

huangzj
/**
 * onUnexpectClose.
 */
public function onUnexpectClose()
{
    $this->emitError(new \Exception('Connection closed'));
}

$http = new Workerman\Http\Client();
        switch ( strtolower($method) ) {
            case 'get':
                $http->get($dataUrl, function ( $response ) use ( $remark, $start, $startMic ) {
//                    var_dump($response->getStatusCode());
//                    echo $response->getBody();
                    $ms      = (microtime(TRUE) - $startMic) * 1000;
                    $runTime = $ms . 'ms';
                    set_log(sprintf("%s %s : %s : %s\n", $start, $runTime, $remark, $response->getBody()));
                }, function ( $exception,$errorCode = 0,$msg = '' ) use ($remark,$dataUrl,$params) {
                    set_log(sprintf("code:%s; msg: %s\n",$errorCode,$msg),'ERROR');
                    set_log(sprintf("%s %s %s\n",$remark,$dataUrl,$params),'ERROR');
                });
                break;
            case 'post':
                $http->post($dataUrl, $params, function ( $response ) use ( $remark, $start, $startMic ) {
//                    var_dump($response->getStatusCode());
                    $ms      = (microtime(TRUE) - $startMic) * 1000;
                    $runTime = $ms . 'ms';
                    set_log(sprintf("%s %s : %s : %s\n", $start, $runTime, $remark, $response->getBody()));
                }, function ( $exception,$code = 0, $msg = ''  ) use ($remark,$dataUrl,$params) {
                    set_log($exception,'ERROR');
                    var_dump($code);
                    var_dump($msg);
                    set_log(sprintf("%s %s %s\n",$remark,$dataUrl,$params),'ERROR');
                });
                break;
            default:
                echo '请求类型错误';
                return FALSE;
        }
1887 0 0
0个回答

年代过于久远,无法发表回答
×
🔝