简体   繁体   中英

cURL PHP not working if CURLOPT_RETURNTRANSFER set true

I try to extract the HTML of a site via cURL for PHP. 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:

$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. The strange thing about that is if I change curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 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. 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

;extension=php_curl.dll

in xampp\\apache\\bin\\php.ini , and then restart the Apache service.

NB: In newer XAMPP versions, PHP has moved to root xampp folder xampp\\php\\php.ini .


Steps in WAMP SERVER

The steps are as follows :

  1. Close WAMP (if running)
  2. Navigate to WAMP\\bin\\php\\(your version of php)\\
  3. Edit php.ini
  4. Search for curl, uncomment extension=php_curl.dll
  5. Navigate to WAMP\\bin\\Apache\\(your version of apache)\\bin\\
  6. Edit php.ini
  7. Search for curl, uncomment extension=php_curl.dll
  8. Save both
  9. Restart WAMP

Its working fine for me ..I think the curl extension is not enabled well ..Are you using 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..

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?

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. Fixing my variable name fixed the the misbehavior that had originally led me to this question.

Hoping that helps somebody. Cheers!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM