【问题标题】:Will Curl in PHP throw out an exception?PHP中的Curl会抛出异常吗?
【发布时间】:2023-03-23 21:41:01
【问题描述】:

这是我的代码

<?php
$url = "http://i.imgur.com/qV39tsL.gif";
$ch = curl_init();
echo $ch;
$timeout = 20;
curl_setopt ($ch, CURLOPT_URL, $url);
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
$content = curl_exec($ch);
curl_close($ch);
echo $content;
?>

在我的环境中,我的上网速度有点慢,所以有时我无法从 imgur 下载一些 gif。

但让我感到困惑的是,我经常收到如下错误:

**ERROR 500: Internal Server Error.**

我在stackoverflow中查了一下,PHP中的Curl不应该让服务器宕机。

那么谁能告诉我为什么会收到 ERROR 500: Internal Server Error?

谢谢~~~

【问题讨论】:

    标签: php apache networking curl network-programming


    【解决方案1】:

    我执行脚本没有问题。 它可能是您的服务器上安装 curl 的东西。

    也检查服务器错误日志以更好地查看错误描述。 也许你需要重新安装 curl。

    【讨论】:

      【解决方案2】:

      试试这个(经过测试),缺少一些指令。

      不包括标题,只会显示.gif 文件的二进制内容,而不是图像本身。

      <?php
      
      $ch = curl_init();
      curl_setopt($ch, CURLOPT_URL, 'http://i.imgur.com/qV39tsL.gif');
      curl_setopt($ch, CURLOPT_HEADER, 0);
      curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
      curl_setopt($ch, CURLOPT_BINARYTRANSFER,1);
      $content = curl_exec($ch);
      curl_close($ch);
      //Display the image in the browser
      header('Content-type: image/gif');
      echo $content;
      
      /* You could also save the file to your server at the same time
      using the following outside this comment
      // save on server option
      $fh = fopen('filename.gif', 'w');
      fwrite($fh, $content);
      fclose($fh);
      */
      
      ?>
      

      【讨论】:

        猜你喜欢
        • 2018-03-25
        • 2011-11-02
        • 1970-01-01
        • 2011-07-12
        • 2012-04-08
        • 2010-09-26
        • 1970-01-01
        • 2011-08-21
        • 2016-04-27
        相关资源
        最近更新 更多