[英]cURL PHP not working if CURLOPT_RETURNTRANSFER set true
I try to extract the HTML of a site via cURL for PHP. 我尝试通过cURL for PHP提取网站的HTML。 Normally it works fine, but there are some website the response is empty. 通常它工作正常,但有一些网站的响应是空的。 For example if I execute the following script for the URL alditalk.de: 例如,如果我为URL alditalk.de执行以下脚本:
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'http://www.alditalk.de/');
curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.1) Gecko/20061204 Firefox/4");
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 30);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, false);
$data = curl_exec($ch);
curl_close($ch);
In this case the variable $data
is empty. 在这种情况下,变量$data
为空。 The strange thing about that is if I change curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
奇怪的是,如果我更改curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
to curl_setopt($ch, CURLOPT_RETURNTRANSFER, false);
to curl_setopt($ch, CURLOPT_RETURNTRANSFER, false);
the website will be printed on the screen. 该网站将打印在屏幕上。 The problem is, I need the content in a variable for further operations. 问题是,我需要变量中的内容以进行进一步的操作。
I tried it locally on WAMPP as well on my Hoster. 我在WAMPP本地尝试了我的Hoster。 I also tried to set some header information without success. 我也尝试设置一些标题信息但没有成功。 There are also no errors. 也没有错误。 Are there any solutions? 有什么解决方案吗?
Its working for me. 它为我工作。 i think you are not enable the curl extension. 我认为你没有启用卷曲扩展。
Since you're using XAMPP, uncomment the line 由于您正在使用XAMPP,请取消注释该行
;extension=php_curl.dll
in xampp\\apache\\bin\\php.ini
, and then restart the Apache service. 在xampp\\apache\\bin\\php.ini
,然后重新启动Apache服务。
NB: In newer XAMPP versions, PHP has moved to root xampp folder xampp\\php\\php.ini
. 注意:在较新的XAMPP版本中,PHP已移至root xampp文件夹xampp\\php\\php.ini
。
Steps in WAMP SERVER
WAMP SERVER
步骤
The steps are as follows : 步骤如下:
WAMP\\bin\\php\\(your version of php)\\
导航到WAMP\\bin\\php\\(your version of php)\\
php.ini
编辑php.ini
extension=php_curl.dll
搜索curl,取消注释extension=php_curl.dll
WAMP\\bin\\Apache\\(your version of apache)\\bin\\
导航到WAMP\\bin\\Apache\\(your version of apache)\\bin\\
php.ini
编辑php.ini
extension=php_curl.dll
搜索curl,取消注释extension=php_curl.dll
Its working fine for me ..I think the curl extension is not enabled well ..Are you using xampp ?? 它的工作正常对我..我认为卷曲扩展没有很好启用..你使用xampp ??
Go to http://www.anindya.com/php-5-4-3-and-php-5-3-13-x64-64-bit-for-windows/
and download the curl version that is same as your php version.. 并下载与您的php版本相同的curl版本..
This doesn't directly answer your question, but it offers a bit of a workaround: Could you consider using ob_start() and associated functions to capture the output that you are able to get? 这并不直接回答你的问题,但它提供了一个位的一种解决方法:你可以考虑使用ob_start()和相关功能,以捕获你能得到的输出?
The idea above is a hacky way to solve it, of course. 当然,上面的想法是解决它的一种黑客方式。 Before falling back on ob_start(), I would suggest double-checking all variable names, ini settings, etc., since it's always possible that you're doing something like accidentally trying to access the contents of $daya rather than $data -- which is precisely the kind of typo I just spent 45 minutes on in my own script. 在回到ob_start()之前,我建议仔细检查所有变量名,ini设置等,因为你总是可能会做一些事情,比如不小心试图访问$ daya而不是$ data的内容 - 这正是我在自己的剧本中花了45分钟的那种拼写错误。 Fixing my variable name fixed the the misbehavior that had originally led me to this question. 修复我的变量名称修复了最初引发我这个问题的错误行为。
Hoping that helps somebody. 希望有助于某人。 Cheers! 干杯!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.