Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

curl_exec printing results when I don't want to

Tags:

php

curl

I am using the following code:

$ch = curl_init();

curl_setopt($ch, CURLOPT_URL, $url);

curl_setopt($ch, CURLOPT_TIMEOUT, 12); 

$result = curl_exec($ch);

curl_close ($ch);

However it's printing the results straight away. Is it possible to put the JSON result into a variable so I can print it out when I want to?

like image 244
Oliver Bayes-Shelton Avatar asked Jan 26 '11 11:01

Oliver Bayes-Shelton


2 Answers

Set CURLOPT_RETURNTRANSFER option:

// ...
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);

$result = curl_exec($ch);

Per the docs:

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

like image 170
Kel Avatar answered Nov 04 '22 00:11

Kel


Have you tried?

curl_setopt($ch, CURLOPT_VERBOSE, 0);

This worked for me!

like image 29
Cesar Octavio Bibriesca Avatar answered Nov 04 '22 00:11

Cesar Octavio Bibriesca