douxi8759 2012-06-26 16:36
浏览 41
已采纳

关于PHP的cURL好奇地保持沉默

OK so after struggling for a while trying to get cURL to fetch a password protected XML file, and getting absolutely nothing from it, I tried dumbing down my code to see what would work. It turns out nothing. Here's my code:

$ch = curl_init("www.google.com");

curl_setopt($ch, CURLOPT_HEADER, TRUE);
curl_setopt($ch, CURLOPT_VERBOSE, TRUE);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);

curl_exec($ch);

curl_close($ch);

And cURL is SILENT. I mean nothing prints to the page. I definitely have it enabled, my phpinfo looks like this:

cURL support            enabled
cURL Information            libcurl/7.16.0 OpenSSL/0.9.8a zlib/1.2.3

Running php 5.2 on a Windows NT server. Any suggestions?

  • 写回答

1条回答 默认 最新

  • dpqmu84646 2012-06-26 16:38
    关注

    The key is in this line:

    curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
    

    You're telling cURL to return the result of the transfer instead of echoing it (the default). So either remove that line, or do something like this:

    $result = curl_exec($ch);
    
    if ($result === false) {
        echo "Error: " . curl_error($ch);
    } else {
        echo $result;
    }
    

    If you're getting an SSL error as you point out in your comments, you can set another option on the cURL handle to disable checking the certificate:

    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 神经网络怎么把隐含层变量融合到损失函数中?
  • ¥30 自适应 LMS 算法实现 FIR 最佳维纳滤波器matlab方案
  • ¥15 lingo18勾选global solver求解使用的算法
  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行
  • ¥15 Python3.5 相关代码写作
  • ¥20 测距传感器数据手册i2c
  • ¥15 RPA正常跑,cmd输入cookies跑不出来
  • ¥15 求帮我调试一下freefem代码
  • ¥15 matlab代码解决,怎么运行
  • ¥15 R语言Rstudio突然无法启动