Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issues when setting CURLOPT_RETURNTRANSFER explicitly as true #19

Closed
nigelterry opened this issue May 11, 2015 · 1 comment
Closed

Issues when setting CURLOPT_RETURNTRANSFER explicitly as true #19

nigelterry opened this issue May 11, 2015 · 1 comment
Labels

Comments

@nigelterry
Copy link

Your latest changes broke my code. I have some idea why.

I was explicitly setting CURLOPT_RETURNTRANSFER to true:

$response = $curl ->setOption(CURLOPT_RETURNTRANSFER, true) ->get($url)

This was working but now it is not. Now $response comes back as true, rather than as the data. However when I remove the setOption line, I get the data.
What appears to be happening is that cURL has issues with the order of setting CURLOPT_RETURNTRANSFER and CURLOPT_WRITEFUNCTION.

Your current code sets CURLOPT_WRITEFUNCTION into the options array in _httpRequest() then adds the default options after in getOptions(). With no explicit setting of CURLOPT_RETURNTRANSFER, this means that CURLOPT_WRITEFUNCTION is set ahead of CURLOPT_RETURNTRANSFER in the array and when calling curl_setopt_array(). What happens now, is that the CURLOPT_WRITEFUNCTION callback anonymous function does not get called. The data is returned correctly in $body.

If CURLOPT_RETURNTRANSFER is explicitly set as in my code, then the order in the array changes, CURLOPT_RETURNTRANSFER is set first and hence set first with curl_setopt_array(). Now the callback function is called and messes up the data.

Why cURL exhibits this behavior I don't know and I haven't tried digging through the code. There is an interesting comment here an=bout using them together: http://php.net/manual/en/function.curl-setopt.php just search.

I don't understand the need for the callback function. I can't work out in what case it might be needed and it now seems to mess with your latest changes. I would suggest that it be completely removed unless I am missing something. That certainly makes my code work.

Of course the other way to "fix" this is to not set CURLOPT_RETURNTRANSFER explicitly.

linslin added a commit that referenced this issue Jun 11, 2015
@linslin
Copy link
Owner

linslin commented Jun 11, 2015

Thanks for your request and all the details. I tried to reproduce this behavior and also tried to fix that. So in that way, the order of "defaultOptions" is not changed anymore. The state you got should not come back anymore.

-> https://github.com/linslin/Yii2-Curl/tree/develop

Please test it with the current development build (head). Version 1.0.5. is comming soon. Any feedback would be appreciated.

    $curl = new curl\Curl();

    $response = $curl
        ->setOption(CURLOPT_CONNECTTIMEOUT, 5)
        ->setOption(CURLOPT_RETURNTRANSFER, true)
        ->get('http://www.example.com:');

    var_dump($response);
    var_dump($curl->responseCode);

Cheers and thanks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants