douhuanqiao5290 2016-06-12 23:32
浏览 446
已采纳

PHP curl_exec()返回不完整的数据

$url='http://api.cruiseline.com/cruises/7-night-western-caribbean-ft-lauderdale-roundtrip-35052/detail';
$ch = curl_init();
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_URL,$url);
$result=curl_exec($ch);
curl_close($ch);
$result=json_decode($result);
var_dump($result);

Here is the response of curl_getinfo()

array(26) { ["url"]=> string(96) "http://api.cruiseline.com/cruises/7-night-western-caribbean-ft-lauderdale-roundtrip-35052/detail" ["content_type"]=> string(16) "application/json" ["http_code"]=> int(200) ["header_size"]=> int(511) ["request_size"]=> int(127) ["filetime"]=> int(-1) ["ssl_verify_result"]=> int(0) ["redirect_count"]=> int(0) ["total_time"]=> float(5.11) ["namelookup_time"]=> float(0.203) ["connect_time"]=> float(0.532) ["pretransfer_time"]=> float(0.532) ["size_upload"]=> float(0) ["size_download"]=> float(650744) ["speed_download"]=> float(127347) ["speed_upload"]=> float(0) ["download_content_length"]=> float(-1) ["upload_content_length"]=> float(0) ["starttransfer_time"]=> float(1.125) ["redirect_time"]=> float(0) ["certinfo"]=> array(0) { } ["primary_ip"]=> string(14) "75.101.141.196" ["primary_port"]=> int(80) ["local_ip"]=> string(13) "192.168.10.15" ["local_port"]=> int(59916) ["redirect_url"]=> string(0) "" } 

Above code sometimes sends complete json data and sometimes sends partial data to $result. When it sends incomplete data, json_decode returns null.

There is no issue on server side. When we open http://api.cruiseline.com/cruises/7-night-western-caribbean-ft-lauderdale-roundtrip-35052/detail in browser, it sends complete response all the time.

Please help and let me know what is the problem and how I can fix that.

  • 写回答

1条回答 默认 最新

  • dongmu5920 2016-06-13 00:42
    关注

    Add this:

    curl_setopt($ch, CURLOPT_ENCODING, '');
    

    to your cURL setup. This made your code workable for me.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
编辑
预览

报告相同问题?

悬赏问题

  • ¥15 WSL上下载的joern在windows怎么用?
  • ¥17 MC9S12XS128单片机开发板实验
  • ¥15 jetson nano4GB
  • ¥15 电脑回复出厂设置,重装过程报错提示,求解决方案Windows 无法分析或处理无人参与应答文件 [C:\WINDOWS\Panther\unattend.xml,如何解决?
  • ¥15 进入lighttools中的UDOP编辑器的方法
  • ¥15 求Gen6d训练数据集
  • ¥20 liunx中winscp中可以登入ftp,但是不能登入sftp,如何解决
  • ¥15 lighttools的光学属性自定义的用法流程
  • ¥15 uni-app动态修改推荐页标题时报错
  • ¥15 图片是GET请求,后面是图片的id,Glide如何缓存?
手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部