ixudra / curl

Custom PHP curl library for the Laravel 5 framework - developed by Ixudra
MIT License
561 stars 128 forks source link

suggest to return Http status code #16

Closed swansun95 closed 8 years ago

swansun95 commented 8 years ago

it will be useful that there is a function to return HTTP status for a request.

elimentz commented 8 years ago

I assume you want the HTTP status combined with the HTML/JSON response and not instead of? Could you give me a use case of how you would like to use it? That would help me in making it functionally correct as well as intuitive to use.

swansun95 commented 8 years ago

since the return value of the get() function is HTML/JSON content, i'm not sure if it's easy to add a HTTP status in it?

i would like to have such codes $response = Curl::to($url)->get(); if(statusCode==200){ // success, then get the content }else{ // do something else }

elimentz commented 8 years ago

Well, there are two options, really:

The first is the easiest

$responseStatus = Curl::to($url)->getStatus();

But if you also need the data, you'd have to do two requests, which would double the delay on your page. This might be ok in some situations, but most often this is not a good thing.

The second option is to encapsulate the response into a separate object:

$response = Curl::to($url)->get();
$data = $responseObject->data;
$status = $responseObject->status;

More flexible, but it would reduce the simplicity of the package

swansun95 commented 8 years ago

the second option is better, i think.

i guess the $responseObject is the $response?

but if the the return value of get() is an Object, what about the old scripts that using $response as HTML? would it be affected? then it would be a compatible problem?

kat3su commented 8 years ago

It's currently return as an json object.

So you can make it return as CurlResponse which has all the data of the json object + 1 extra method getStatus() or status().

By this way, it will keep the simplicity of this package and won't affect any existing script.

elimentz commented 8 years ago

Fixed in version 6.3.0:

        $response = \Curl::to('http://someurl.com')
            ->withData( $data )
            ->returnResponseObject()
            ->get();

    $status = $response->status;
    $content = $response->content
yansen-tan commented 8 years ago

Hi, thank you for the fix. But the content returns null if the status code returns 422. How do I get the response data if I got the validation error from the API?

elimentz commented 8 years ago

@yansen-tan Can you give an example of what you get back and what kind of data you would like to see?

yansen-tan commented 8 years ago

Hi, thank you for replying. I managed to get the response using withOption('FAILONERROR', false).

Thank you again. Problem solved

On Tuesday, 20 September 2016, Jan Oris notifications@github.com wrote:

@yansen-tan https://github.com/yansen-tan Can you give an example of what you get back and what kind of data you would like to see?

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/ixudra/curl/issues/16#issuecomment-248143490, or mute the thread https://github.com/notifications/unsubscribe-auth/ALYqKUG2LC-B4-9wsh_KqUaPtOmBmQ3iks5qrwiwgaJpZM4Im8Bt .