user1438910
user1438910

Reputation:

php CURL not show any response

My code is:

<?php
extract($_POST);
$url = "http://www.domain.com/";

$fields = array(
                    'myparam1' => 'something',
                );

foreach($fields as $key=>$value) { $fields_string .= $key.'='.$value.'&'; }
rtrim($fields_string, '&');


//open connection
$ch = curl_init();

curl_setopt($ch,CURLOPT_URL, $url);
curl_setopt($ch,CURLOPT_POST, count($fields));
curl_setopt($ch,CURLOPT_POSTFIELDS, $fields_string);
//curl_setopt($ch,CURLOPT_RETURNTRANSFER, false);

$result = curl_exec($ch);
print_r("+");
curl_close($ch);

?>

The problem is it show the response in the web browse , but i dont want to. any help would be appreciated.

Upvotes: 0

Views: 3695

Answers (4)

Neel
Neel

Reputation: 19

I think this url will help you.

Get data or Content from a URL using cURL PHP

Upvotes: 0

Jim
Jim

Reputation: 22646

The option CURLOPT_RETURNTRANSFER will return the output rather than printing it.

curl_setopt($ch,CURLOPT_RETURNTRANSFER, true);

Upvotes: 0

Baba
Baba

Reputation: 95103

CURLOPT_POSTFIELDS can take array directly and CURLOPT_RETURNTRANSFER should be true

$url = "http://www.php.net";
$fields = array(
        'myparam1' => 'something'
);
// open connection
$ch = curl_init();

curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, count($fields));
curl_setopt($ch, CURLOPT_POSTFIELDS, $fields);
curl_setopt($ch,CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($ch);
curl_close($ch);

Upvotes: 0

mishu
mishu

Reputation: 5397

You have a comment on this line:

//curl_setopt($ch,CURLOPT_RETURNTRANSFER, false);

You should enable this line and set the value to true

curl_setopt($ch,CURLOPT_RETURNTRANSFER, true);

As you can see on the man page here that option is used this way:

TRUE to return the transfer as a string of the return value of curl_exec() instead of outputting it out directly.

Upvotes: 1

Related Questions