OSS\Http\RequestCore::process_response PHP Method

process_response() public method

Take the post-processed cURL data and break it down into useful header/body/info chunks. Uses the data stored in the curl_handle and response properties unless replacement data is passed in via parameters.
public process_response ( resource $curl_handle = null, string $response = null ) : OSS\Http\ResponseCore
$curl_handle resource (Optional) The reference to the already executed cURL request.
$response string (Optional) The actual response content itself that needs to be parsed.
return OSS\Http\ResponseCore A object containing a parsed HTTP response.
    public function process_response($curl_handle = null, $response = null)
    {
        // Accept a custom one if it's passed.
        if ($curl_handle && $response) {
            $this->curl_handle = $curl_handle;
            $this->response = $response;
        }
        // As long as this came back as a valid resource...
        if (is_resource($this->curl_handle)) {
            // Determine what's what.
            $header_size = curl_getinfo($this->curl_handle, CURLINFO_HEADER_SIZE);
            $this->response_headers = substr($this->response, 0, $header_size);
            $this->response_body = substr($this->response, $header_size);
            $this->response_code = curl_getinfo($this->curl_handle, CURLINFO_HTTP_CODE);
            $this->response_info = curl_getinfo($this->curl_handle);
            // Parse out the headers
            $this->response_headers = explode("\r\n\r\n", trim($this->response_headers));
            $this->response_headers = array_pop($this->response_headers);
            $this->response_headers = explode("\r\n", $this->response_headers);
            array_shift($this->response_headers);
            // Loop through and split up the headers.
            $header_assoc = array();
            foreach ($this->response_headers as $header) {
                $kv = explode(': ', $header);
                $header_assoc[strtolower($kv[0])] = isset($kv[1]) ? $kv[1] : '';
            }
            // Reset the headers to the appropriate property.
            $this->response_headers = $header_assoc;
            $this->response_headers['info'] = $this->response_info;
            $this->response_headers['info']['method'] = $this->method;
            if ($curl_handle && $response) {
                //return new $this->response_class($this->response_headers, $this->response_body, $this->response_code, $this->curl_handle);
                return new ResponseCore($this->response_headers, $this->response_body, $this->response_code);
            }
        }
        // Return false
        return false;
    }

Usage Example

コード例 #1
0
 public function testParseResponse()
 {
     $httpCore = new RequestCore("http://www.baidu.com");
     $response = $httpCore->send_request();
     $parsed = $httpCore->process_response(null, $response);
     $this->assertNotNull($parsed);
 }