API gives a JSON response, but PHP doesn't treat it as JSON on the HTTP request, only if I copy to a local file -


I am using an API that gives a JSON response. If I copy that response to the 'test.txt' file and retrieve data from it - it's okay. However, if I try to @file_get_contents on the HTTPS url directly, I get a non-object.

  function fetchMeasurments ($ url, $ energyCofficient, $ filePrefix) {$ ConnectionSettings = stream_context_create (array ( 'http' = & gt; array ( 'timeout' = & gt; 5))) ; $ JsonData = @file_get_contents ($ url, incorrect, $ connection settings); $ Obj = json_decode ($ jsonData); If (is_null ($ obj)) {echo 'null'; Die (); } Else {echo 'tap'; Die (); }  

If I use Testktxt - do not get the 'I' is empty, but if I use HTTPS url, so I get free I have any idea?

Here is JSON response:

  { "overview": { "lastUpdateTime": "2014-10-27 11:03:15", "lifeTimeData": { "energy": 2kl047042E7, "revenue": 2639.4795}, "lastYearData": { "energy": 2.105334E7}, "lastMonthData": { "energy": 1,388,652.8}, "lastDayData": { "energy": 749.25397} "current power": { "power": 817.0}}}  

I think That you should actually consider using curls instead of file_get_contents () support for curl secure connection and you will not have problems with https.

Morrors suppresion of errors is not a good idea.

  Function Getdta ($ url) {$ f = Krl_init ())  

Krl_setpt ($ f, Krlopt_uarel, $ url); Curl_setopt ($ CH, CURLOPT_RETURNTRANSFER, true); Curl_setopt ($ CH, CURLOPT_SSL_VERIFYPEER, incorrect); $ Response = curl_exec ($ ch); Curl_close ($ ch); $ Refund; }

Use this function instead of file_get_contents ().

Note that this is a very basic function without setting the header, error handling and certificate approval.


Comments