dt614037527
dt614037527
2017-11-06 07:28

直接在浏览器中使用时收到的JSON数据,但不包含cURL

$ch = curl_init();
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_URL, 'https://www.unocoin.com/trade?all');
$result = curl_exec($ch);
curl_close($ch);
$data = json_decode($result);
return $data;

JSON data is received when directly used in browser, but not with cURL in PHP.

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

1条回答

  • druybew06513 druybew06513 4年前

    Try like this,

    $curl = curl_init();
    
    curl_setopt_array($curl, array(
      CURLOPT_URL => "https://www.unocoin.com/trade?all=",
      CURLOPT_RETURNTRANSFER => true,
      CURLOPT_ENCODING => "",
      CURLOPT_MAXREDIRS => 10,
      CURLOPT_TIMEOUT => 30,
      CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
      CURLOPT_CUSTOMREQUEST => "GET"
    ));
    
    $response = curl_exec($curl);
    $err = curl_error($curl);
    
    curl_close($curl);
    
    if ($err) {
      echo "cURL Error #:" . $err;
    } else {
      echo $response;
    }
    

    Response:

    {
        "buy": 505189,
        "sell": 487508,
        "avg": 496348
    }
    
    点赞 评论 复制链接分享