Ejemplo n.º 1
0
 /**
  * 解析response并判断响应内容是否正确.
  * 如果结果符合restAPI结果格式,则返回结果中包含的response_params内容, 否则依据配置中是不是压制错误产生错误或返回null
  *
  * @param array $response
  *        由httpRequest对象get,post,request等方法返回的结果数组
  * @return mixed|NULL
  */
 public function parse($response)
 {
     extract($response);
     $this->log->log("Parse Response: {$status}, {$statusMsg}, {$content}");
     $result = json_decode($content, true);
     if ($result !== null && array_key_exists('request_id', $result)) {
         if ($status == 200) {
             if (array_key_exists('response_params', $result)) {
                 // 状态200,并且成功解析出json结果. 表示请求成功
                 $this->onError($result['request_id']);
                 return $result['response_params'];
             } else {
                 // json可以解析, 但是格式不可理解.
                 $this->onError($result['request_id'], 4, "http status is ok, but REST returned invalid json struct.");
             }
         } else {
             if (array_key_exists('error_code', $result) && array_key_exists('error_msg', $result)) {
                 // 状态不等于200, 但能解析出结果. 表示请求成功, 但是服务端返回错误
                 $this->onError($result['request_id'], $result['error_code'], $result['error_msg']);
             } else {
                 // json可以解析, 但是格式不可理解.
                 $this->onError($result['request_id'], 4, "http status is ok, but REST returned invalid json struct.");
             }
         }
     } else {
         if ($status == 200) {
             // 无法解析出json, 响应200,
             $this->onError(null, 4, "http status is ok, but REST returned invalid json string.");
         } elseif ($status >= 400) {
             // 无法解析出json, 但是响应一个可理解的http错误
             $this->onError(null, $status, "http response status  : {$statusMsg}");
         } else {
             // 响应200-399 , 即除200外任意http成功状态, 均不能被理解, 响应 (-1) 错误.
             $limitContent = strlen($content);
             if ($limitContent > 0) {
                 $limitContent = substr($content, 200) . "..." . strlen($content);
             } else {
                 $limitContent = 'Empty';
             }
             $this->onError(null, -1, "Can not process the http response. with status ({$status}, {$statusMsg}), content ({$limitContent});");
         }
     }
     return false;
 }
Ejemplo n.º 2
0
if (!defined('PUSH_SDK_HOME')) {
    define('PUSH_SDK_HOME', dirname(dirname(__FILE__)));
}
include_once PUSH_SDK_HOME . '/lib/PushSimpleLog.php';
/**
 * 取得根据http状态码取得对应的默认状态消息
 * @param unknown $code
 */
function send_http_status($code)
{
    static $_status = array(100 => 'Continue', 101 => 'Switching Protocols', 200 => 'OK', 201 => 'Created', 202 => 'Accepted', 203 => 'Non-Authoritative Information', 204 => 'No Content', 205 => 'Reset Content', 206 => 'Partial Content', 300 => 'Multiple Choices', 301 => 'Moved Permanently', 302 => 'Moved Temporarily ', 303 => 'See Other', 304 => 'Not Modified', 305 => 'Use Proxy', 307 => 'Temporary Redirect', 400 => 'Bad Request', 401 => 'Unauthorized', 402 => 'Payment Required', 403 => 'Forbidden', 404 => 'Not Found', 405 => 'Method Not Allowed', 406 => 'Not Acceptable', 407 => 'Proxy Authentication Required', 408 => 'Request Timeout', 409 => 'Conflict', 410 => 'Gone', 411 => 'Length Required', 412 => 'Precondition Failed', 413 => 'Request Entity Too Large', 414 => 'Request-URI Too Long', 415 => 'Unsupported Media Type', 416 => 'Requested Range Not Satisfiable', 417 => 'Expectation Failed', 500 => 'Internal Server Error', 501 => 'Not Implemented', 502 => 'Bad Gateway', 503 => 'Service Unavailable', 504 => 'Gateway Timeout', 505 => 'HTTP Version Not Supported', 509 => 'Bandwidth Limit Exceeded');
    if (array_key_exists($code, $_status)) {
        header('HTTP/1.1 ' . $code . ' ' . $_status[$code]);
    }
}
$log = new PushSimpleLog('stdout', 0);
if (array_key_exists('q', $_REQUEST)) {
    $log->log(print_r($_REQUEST['q'], true));
    switch ($_REQUEST['q']) {
        case "404_1":
            send_http_status(404);
            echo '{"request_id":100, "error_code":10000 , "error_msg":"what is that??"}';
            break;
        case "404_2":
            send_http_status(404);
            echo 'what is this??';
            break;
        case "200_1":
            send_http_status(200);
            echo '{"request_id":100, "response_params":[1,2,3,4]}';
            break;
Ejemplo n.º 3
0
 /**
  * 快速发起post请求
  *
  * @param string $path
  *        请求资源路径
  * @param array $postBody
  *        请求参数
  * @param array $headers
  *        附带header
  * @param array $curlOpts
  *        附加curlOpts
  *        
  * @return array http response信息 , 具有如下结构 array(
  *         httpProtocol:'',
  *         status:'',
  *         statusMsg:'',
  *         header:array(),
  *         content:""
  *         )
  */
 function post($path = null, $postBody = null, $headers = null, $curlOpts = null)
 {
     $payload = $this->encodePostBody($postBody);
     $this->log->debug("\n\n ====== Dump the payload before Http POST send! ====== \n\n{$payload}\n\n====== End Dump! ======\n");
     return $this->request($path, self::HTTP_POST, $payload, $headers, $curlOpts);
 }