linslin / Yii2-Curl

:curly_loop: Yii2 Curl extension based on PHP cURL.
178 stars 92 forks source link

Problem with get #58

Closed konorlevich closed 7 years ago

konorlevich commented 7 years ago

I have upgraded linslin/yii2-curl from 1.0.3 to 1.1.1

My request

        $curl = new Curl();
        $curl->setOption(CURLOPT_HEADER, 'Accept:application/json');
        $response = $curl->get($url);

in function _httpRequest we have

        //extract header / body data if CURLOPT_HEADER are set to true
        if (isset($curlOptions[CURLOPT_HEADER]) && $curlOptions[CURLOPT_HEADER]) {
            $this->response = $this->_extractCurlBody($response);
            $this->responseHeaders = $this->_extractCurlHeaders($response);
        } else {
            $this->response = $response;
        }

and

protected function _extractCurlBody ($response)
    {
        return substr($response, $this->getInfo(CURLINFO_HEADER_SIZE));
    }

in my response I have

'getInfo(CURLOPT_HEADER)' => false
'getInfo(CURLINFO_HEADER_SIZE)' => 503

Then cURL returns my response without half of it But without

$curl->setOption(CURLOPT_HEADER, 'Accept:application/json');

I have full response

What am i doing wrong?

linslin commented 7 years ago

HI @konorlevich, thanks for you request. Just for refactoring: since v1.1.1 you can use the following syntax to setup your cURL request. But the old syntax should work aswell.

$curl = new curl\Curl();
$response = $curl->setHeaders([
        'accept' => 'application/json'
     ])
     ->get($url);

It seems like the header size provided by CURLINFO_HEADER_SIZE does not match to the real response header size in your response. Could you add your response + request headers & body to make it reproduceable? I'll try to create a new functionalTest for it.

konorlevich commented 7 years ago

Yes, of course

[
    'file_get_contents($url)' =>  '[{\"id\":28,\"asset_id\":53,\"parent_id\":27,\"lft\":68,\"rgt\":69,\"level\":2,\"path\":\"peoplehavaet/usaboxoffice\",\"title\":\"Бокс-офис США\",\"alias\":\"usaboxoffice\",\"new_path\":\"box-office/usa\",\"note\":\"\",\"description\":\"\",\"extension\":\"com_content\",\"published\":1,\"checked_out\":0,\"checked_out_time\":null,\"access\":1,\"params\":\"{}\",\"metadesc\":\"\",\"metakey\":\"\",\"metadata\":\"\",\"created_user_id\":0,\"created_time\":\"2012-01-02 14:26:35\",\"modified_user_id\":0,\"modified_time\":\"2016-07-25 17:41:19\",\"hits\":0,\"language\":\"*\",\"is_active\":1,\"catsort\":null},{\"id\":31,\"asset_id\":56,\"parent_id\":27,\"lft\":74,\"rgt\":75,\"level\":2,\"path\":\"peoplehavaet/ruboxoffice\",\"title\":\"Бокс-офис России\",\"alias\":\"ruboxoffice\",\"new_path\":\"box-office/russia\",\"note\":\"\",\"description\":\"\",\"extension\":\"com_content\",\"published\":1,\"checked_out\":0,\"checked_out_time\":null,\"access\":1,\"params\":\"{}\",\"metadesc\":\"\",\"metakey\":\"\",\"metadata\":\"\",\"created_user_id\":0,\"created_time\":\"2012-01-02 14:26:35\",\"modified_user_id\":0,\"modified_time\":\"2016-07-25 17:41:18\",\"hits\":0,\"language\":\"*\",\"is_active\":1,\"catsort\":null}]'
    '$curl->getInfo(CURLOPT_HEADER)' => false
    '$curl->getInfo(CURLINFO_HEADER_SIZE)' => 502
    '$curl->responseHeaders' => [
        'http_code' => ''
    ]
    '$curl->responseCode' => 200
    '$curl->get($url)' => 'is_active\":1,\"catsort\":null},{\"id\":31,\"asset_id\":56,\"parent_id\":27,\"lft\":74,\"rgt\":75,\"level\":2,\"path\":\"peoplehavaet/ruboxoffice\",\"title\":\"Бокс-офис России\",\"alias\":\"ruboxoffice\",\"new_path\":\"box-office/russia\",\"note\":\"\",\"description\":\"\",\"extension\":\"com_content\",\"published\":1,\"checked_out\":0,\"checked_out_time\":null,\"access\":1,\"params\":\"{}\",\"metadesc\":\"\",\"metakey\":\"\",\"metadata\":\"\",\"created_user_id\":0,\"created_time\":\"2012-01-02 14:26:35\",\"modified_user_id\":0,\"modified_time\":\"2016-07-25 17:41:18\",\"hits\":0,\"language\":\"*\",\"is_active\":1,\"catsort\":null}]'
]

Do you need any other information?

linslin commented 7 years ago

Could you give me the full output of $curl->getInfo() ?

konorlevich commented 7 years ago
'$curl->getInfo()' => [
        'url' => 'http://backend.local/v1/categories?per-page=2'
        'content_type' => 'application/json; charset=UTF-8'
        'http_code' => 200
        'header_size' => 502
        'request_size' => 114
        'filetime' => -1
        'ssl_verify_result' => 0
        'redirect_count' => 0
        'total_time' => 0.013927
        'namelookup_time' => 0.004129
        'connect_time' => 0.004187
        'pretransfer_time' => 0.004218
        'size_upload' => 0
        'size_download' => 1068
        'speed_download' => 76685
        'speed_upload' => 0
        'download_content_length' => 1068
        'upload_content_length' => -1
        'starttransfer_time' => 0.013912
        'redirect_time' => 0
        'redirect_url' => ''
        'primary_ip' => '127.0.1.1'
        'certinfo' => []
        'primary_port' => 80
        'local_ip' => '127.0.0.1'
        'local_port' => 40332
    ]
linslin commented 7 years ago

Thx! I need to add a param to get the raw response for debugging. Well, could you add var_dump($response);die(); for debugging output in line 519 in linslin\yii2-curl\Curl.php and add the full raw output here?

konorlevich commented 7 years ago

Oh, sorry, wrong request.

Now it's right.

'HTTP/1.1 200 OK
Date: Mon, 20 Mar 2017 14:27:55 GMT
Server: Apache/2.4.18 (Ubuntu)
X-Pagination-Total-Count: 13
X-Pagination-Page-Count: 7
X-Pagination-Current-Page: 1
X-Pagination-Per-Page: 2
Link: <http://backend.rulezoom.local/v1/categories?per-page=2&page=1>; rel=self, <http://backend.rulezoom.local/v1/categories?per-page=2&page=2>; rel=next, <http://backend.rulezoom.local/v1/categories?per-page=2&page=7>; rel=last
Content-Length: 1068
Content-Type: application/json; charset=UTF-8

[{\"id\":28,\"asset_id\":53,\"parent_id\":27,\"lft\":68,\"rgt\":69,\"level\":2,\"path\":\"peoplehavaet/usaboxoffice\",\"title\":\"Бокс-офис США\",\"alias\":\"usaboxoffice\",\"new_path\":\"box-office/usa\",\"note\":\"\",\"description\":\"\",\"extension\":\"com_content\",\"published\":1,\"checked_out\":0,\"checked_out_time\":null,\"access\":1,\"params\":\"{}\",\"metadesc\":\"\",\"metakey\":\"\",\"metadata\":\"\",\"created_user_id\":0,\"created_time\":\"2012-01-02 14:26:35\",\"modified_user_id\":0,\"modified_time\":\"2016-07-25 17:41:19\",\"hits\":0,\"language\":\"*\",\"is_active\":1,\"catsort\":null},{\"id\":31,\"asset_id\":56,\"parent_id\":27,\"lft\":74,\"rgt\":75,\"level\":2,\"path\":\"peoplehavaet/ruboxoffice\",\"title\":\"Бокс-офис России\",\"alias\":\"ruboxoffice\",\"new_path\":\"box-office/russia\",\"note\":\"\",\"description\":\"\",\"extension\":\"com_content\",\"published\":1,\"checked_out\":0,\"checked_out_time\":null,\"access\":1,\"params\":\"{}\",\"metadesc\":\"\",\"metakey\":\"\",\"metadata\":\"\",\"created_user_id\":0,\"created_time\":\"2012-01-02 14:26:35\",\"modified_user_id\":0,\"modified_time\":\"2016-07-25 17:41:18\",\"hits\":0,\"language\":\"*\",\"is_active\":1,\"catsort\":null}]
linslin commented 7 years ago

Yea, crazy. The header size does not match with the characters in the header. I will check this. Which PHP version are you using?

konorlevich commented 7 years ago

PHP Version 7.0.15-0ubuntu0.16.10.4

linslin commented 7 years ago

Allright, thx. It will take some time to create a test for this case. Please hang on. It will maybe take some days for me.

konorlevich commented 7 years ago

Thank you.

linslin commented 7 years ago

Sorry, still unable to reproduce it. We looking straight forward to fix this in v1.1.4.

linslin commented 7 years ago

@konorlevich could you provide an URL/API/ENDPOINT where this happends? I'm still unable to reproduce this problem with my endpoints.

linslin commented 7 years ago

I going to close this issue. Once you bring some feedback I will reopen it.

konorlevich commented 7 years ago

Sorry, but I don't have access to that code anymore Thank you