Emily
Emily

Reputation: 915

Curl automatically display the result?

I'm using php 5.3.2 and when i execute a curl it display the result directly without adding a print or echo function.

Here is my code:

<?php
$pvars = array('query' => 'ice age', 'orderby' => 'popularity');
$timeout = 30;
$myurl = "http://www.website.com";
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $myurl);
curl_setopt($curl, CURLOPT_TIMEOUT, $timeout);
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, $pvars);
$xml = curl_exec($curl);
curl_close ($curl);
?>

What's wrong with my code and why it displays the result?

Upvotes: 22

Views: 24376

Answers (2)

user3443146
user3443146

Reputation: 71

after php 5.1 curl always displays results as you can view on the documention. to avoid that simply do:

echo "< span style='display:none'>";
$pvars = array('query' => 'ice age', 'orderby' => 'popularity');
$timeout = 30;
$myurl = "http://www.website.com";

$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $myurl);
curl_setopt($curl, CURLOPT_TIMEOUT, $timeout);
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, $pvars);
$xml = curl_exec($curl);
curl_close ($curl);

echo"< /span>";

Upvotes: -1

Brian McKenna
Brian McKenna

Reputation: 46228

By default, the curl extension prints out the result.

You need to enable the CURLOPT_RETURNTRANSFER option, like so:

curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);

After that option is enabled, curl_exec will return the result, instead.

Upvotes: 46

Related Questions