简体   繁体   中英

Curl CURLOPT_RETURNTRANSFER not working as expected?

I am trying to load a page with Curl. The page loads properly, just like I expected. But the problem is Curl prints the whole content out instead of saving it into a variable as I want to.

The code looks like this:

//set the url, number of POST vars, POST data
curl_setopt($ch,CURLOPT_URL,$url);
curl_setopt($ch,CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch,CURLOPT_POST,count($fields));
curl_setopt($ch,CURLOPT_POSTFIELDS,$fields_string);

//execute post
$result = curl_exec($ch);

//close connection
curl_close($ch);

Except for the initializations, this is the whole content of the php file. It can't get much simpler than this but yet it prints out the content and does not save it into the variable $result.

I tried:

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

also I tried a bunch of other addresses, same result. Did I miss something?

Maybe I have to set another Curl option or I have to change some settings of my test server?

In my case, I was returning rather than echoing the return data. When I echoed the return value all was well.

EDIT: It was for an AJAX command using jQuery.

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