21

I'm trying to process result from $data = curl_exec($ch); instead of printing it on the screen. In order to achieve that I set the option CURLOPT_RETURNTRANSFER to true like this:

curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);

On my local server this works as expected but when I put the same file online on my server it doesn't work.

When I set CURLOPT_RETURNTRANSFER to false it works.

What am I doing wrong?

YakovL
  • 7,557
  • 12
  • 62
  • 102
GentSVK
  • 324
  • 1
  • 3
  • 12
  • 1
    Maybe you just ran out of (allowed) memory on the hosting server. When CURLOPT_RETURNTRANSFER is set to true the whole fetched content is held in memory when set to false you only have the unechoed buffer in memory. To verify just try to fetch something very small. – Henning Mar 28 '21 at 11:43

2 Answers2

26

If you set CURLOPT_RETURNTRANSFER to true or 1 then the return value from curl_exec will be the actual result from the successful operation. In other words it will not return TRUE on success. Although it will return FALSE on failure.

As described in the Return Values section of curl-exec PHP manual page: http://php.net/manual/function.curl-exec.php

You should enable the CURLOPT_FOLLOWLOCATION option for redirects but this would be a problem if your server is in safe_mode and/or open_basedir is in effect which can cause issues with curl as well.

Anthony Hatzopoulos
  • 10,437
  • 2
  • 40
  • 57
  • 2
    maybe I put the question wrongly. I want the result to be returned in the variable. So I set CURLOPT_RETURNTRANSFER to true and it should do that but it doesnt on hosting server but on localhost it works fine. maybe some settings in php ini are not set i dont know. – GentSVK Sep 05 '12 at 06:07
  • @MarcelGentSimonis I edited my answer, check what safe_mode and open_basedir are set. Your local machine probably has them disabled – Anthony Hatzopoulos Oct 03 '12 at 17:42
  • Note that if not set to true the return from curl will be outputted as part of **your** response. toke an hour from me to realize that the strange output at the top of the page is because curl outputted it's response. – Accountant م Dec 18 '17 at 11:15
  • Just to add that "safe_mode" is eliminated since php version 5.4.0 If your server php version is higher you dont have to worry about this – Mbotet Aug 07 '19 at 07:02
1

If it works fine on your local environment, probably your remote server's IP is being blocked by the server at the target URL you've set for cURL to use. You need to verify that your remote server is allowed to access the URL you've set for CURLOPT_URL.

rayalois22
  • 161
  • 3
  • 7