ixudra / curl

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

Response Data asJson returns null #12

Closed GianNipitella closed 8 years ago

GianNipitella commented 8 years ago

i'm Using Laravel 5.2 with ixudra/curl problem is that when i do

              $api_url="http://www.freecodecamp.com/news/hot";
               $response = Curl::to($api_url)
              ->asJson()
               ->get() ;
                dd($response);//shows null   

But when i do $api_url="http://www.freecodecamp.com/news/hot"; $response = Curl::to($api_url) ->get() ; dd($response); the above shows some html code that still is not what i need what i really need is the information that shows when u acces http://www.freecodecamp.com/news/hot directly from browser how do i get it what am i doing wrong?

elimentz commented 8 years ago

The problem is that your request is being redirected. You must add the appropriate options to your cURL request so this is handled properly:

        $api_url="http://www.freecodecamp.com/news/hot";
        $response = \Curl::to($api_url)
            ->withOption('FOLLOWLOCATION', true)
            ->get() ;
        dd($response); // shows results as expected

Also, I see that the request no longer returns json, so I suppose you should also remove the 'asJson()` option :-)

FYI: first google result on Google: http://stackoverflow.com/questions/21233771/php-curl-function-301-error