【问题标题】:PHP cURL not working on other sitesPHP cURL 在其他网站上不起作用
【发布时间】:2016-07-20 20:07:16
【问题描述】:

我是 PHP 新手。上周我读到了关于 cURL 的文章,它吸引了我的注意力去研究它。首先,我复制并粘贴发布在不同博客上的代码,它运行良好,就像我下面的代码一样。

<?php
$handle=curl_init('http://www.google.co.kr/');
curl_setopt($handle, CURLOPT_VERBOSE, true);
curl_setopt($handle, CURLOPT_RETURNTRANSFER, true);
curl_setopt($handle, CURLOPT_SSL_VERIFYPEER, false);
$content = curl_exec($handle);
echo $content;
?>

但为什么我不能卷曲网站

http://www.todayhumor.co.kr/

从那以后,我在输出上面使用相同的代码

寻找您的积极回应伙计们。先感谢您。

【问题讨论】:

  • 感谢 Sandeep 编辑我的语法..
  • 检查您服务器的错误日志,正如消息已告诉您的那样

标签: php curl web xampp


【解决方案1】:

调用curl_exec($handle) 后,您应该使用curl_close($handle) 关闭会话。也许您尝试了很多次,但现在它不再起作用了,因为您的本地服务器上有很多打开的会话。我会将该行添加到您的代码中,重新启动 xampp 并重试。

编辑: 服务器拒绝没有有效用户代理的请求。在您的请求中添加用户代理:curl_setopt($handle, CURLOPT_USERAGENT, 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/49.0.2623.110 Safari/537.36'); 这对我有用

【讨论】:

  • 我重新启动了两次 Xampp,但输出仍然相同。
  • 我也尝试了 HTML DOM PARSEL,但仍然无法在这个网站上运行.. todayhumor.co.kr/');回声 $html; ?>
  • 尝试输出有关错误的附加信息。它说什么? echo curl_errno($handle);echo curl_error($handle);var_dump(curl_getinfo($handle));
  • 回声 curl_errno($handle); & echo curl_error($handle);输出为 0 和 var_dump(curl_getinfo($handle));有一个输出
  • 00array(26) { ["url"]=> string(28) "todayhumor.co.kr" ["content_type"]=> string(29) "text/html; charset=iso-8859 -1" ["http_code"]=> int(500) ["header_size"]=> int(398) ["request_size"]=> int(59) ["filetime"]=> int(-1) [" ssl_verify_result"]=> int(0) ["redirect_count"]=> int(0) ["total_time"]=> float(0.625) ["namelookup_time"]=> float(0) ["connect_time"]=> float (0.234) ["pretransfer_time"]=> float(0.234) ["size_upload"]=> float(0) ["size_download"]=> float(1226) [ . . . . . . . . .等等
猜你喜欢
  • 2015-03-30
  • 2018-12-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-03-31
  • 2011-04-12
  • 2016-02-26
相关资源
最近更新 更多