doubangzhang6992 2014-09-04 11:38
浏览 47
已采纳

返回后非空字符串为空

I'm writing a php script that deals with page processing via cURL, so I have a function to get and return pages by URL

function get_url($Url){
    if (!function_exists('curl_init')){
        die('Sorry cURL is not installed!');
    }
    set_time_limit (20);
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $Url);
    curl_setopt($ch, CURLOPT_HTTPHEADER, array("Cookie: age_gate_birthday=19901101"));
    curl_setopt($ch, CURLOPT_REFERER, "http://www.facebook.com");
    curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows NT 6.3; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/37.0.2049.0 Safari/537.36");
    curl_setopt($ch, CURLOPT_HEADER, 0);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    $output = curl_exec($ch);
    return $output;
}

echoing $output in this function always returns a string of HTML, however if I call on this function in another function

function get_vid ($sql, $url) {
    $data = get_url($url);
...

the returned value is an empty string, despite the fact that $output had value when get_url() was doing its thing.

Weirdly enough, the error only exists with specific URLs, but works fine with others.

Thank you for trying to help!

UPDATE: It seems CURL returns FALSE randomly on specific links, which seems to be a culprit of this issue, however curl_error is empty, so I'm unable to identify the cause of this.

  • 写回答

1条回答 默认 最新

  • dougan1465 2014-09-04 12:06
    关注

    I think it's because you get a http redirect.

    Try to check http code like this :

      if (curl_getinfo($ch,CURLINFO_HTTP_CODE) == 302) {
        // Manage http redirect here
      }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 关于#MATLAB#的问题,如何解决?(相关搜索:信噪比,系统容量)
  • ¥500 52810做蓝牙接受端
  • ¥15 基于PLC的三轴机械手程序
  • ¥15 多址通信方式的抗噪声性能和系统容量对比
  • ¥15 winform的chart曲线生成时有凸起
  • ¥15 msix packaging tool打包问题
  • ¥15 finalshell节点的搭建代码和那个端口代码教程
  • ¥15 Centos / PETSc / PETGEM
  • ¥15 centos7.9 IPv6端口telnet和端口监控问题
  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作