Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to avoid echo in curl_exec()?

Tags:

php

curl

I'm working with cURL, and needing to change my headers after using curl_exec(). But this function displays result automatically. Code:

$ch=curl_init($redir);
$result = curl_exec($ch);
curl_close($ch);

How can I avoid this?

like image 865
Mykhas Kobernyk Avatar asked Jul 29 '26 23:07

Mykhas Kobernyk


2 Answers

By using curl_setopt(), you can set CURLOPT_RETURNTRANSFER to have curl_exec() return the result instead of echoing it:

curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

Direct Output

$ch=curl_init($redir);
curl_exec($ch); // echo ouput
curl_close($ch);

Return Output

$ch=curl_init($redir);
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );
$result = curl_exec($ch); // return ouput
curl_close($ch);
like image 22
Utku Yıldırım Avatar answered Jul 31 '26 13:07

Utku Yıldırım



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!