Failed to json_decode string from file_get_contents

后端 未结 2 1750
一整个雨季
一整个雨季 2020-12-11 11:43

I recently wanted to fetch and decode API response from a web service. I thought that just just file_get_contents and then json_decode the resultin

2条回答
  •  天命终不由人
    2020-12-11 12:28

    you can use curl instead of file_get_contents and get page content without any encoding

       function get_url($link){
    
          $ch = curl_init();
          curl_setopt($ch, CURLOPT_HEADER, 0);
          curl_setopt($ch, CURLOPT_VERBOSE, 0);
          curl_setopt($ch,CURLOPT_ENCODING, '');
          curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
          curl_setopt($ch, CURLOPT_URL, ($link));
          $response = curl_exec($ch);
          curl_close($ch);
          return ($response); 
    
    
        }
    

提交回复
热议问题