curl_exec printing results when I don't want to - php

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?

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.

Have you tried?
curl_setopt($ch, CURLOPT_VERBOSE, 0);
This worked for me!

after php 5.1 curl will display always result you can view in documentation. for avoid it simply use
echo "< span style='display:none'>";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_TIMEOUT, 12);
$result = curl_exec($ch);
curl_close ($ch);
echo"< /span>";

Related

cUrl - getting the html response body

I'm sure this is fairly simple. I'm using the function below to retrieve sites raw html
in order to parse it. during my testing, I decided to run my code on stackoverflow.com
Instead of getting the html response the Chrome is printing out the actual site rather then assigning the html to its veritable. What am I missing?
function get_site_html($site_url)
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_COOKIESESSION, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_MAXREDIRS, 4);
curl_setopt($ch, CURLOPT_FORBID_REUSE, true);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 30);
curl_setopt($ch, CURLOPT_URL, $site_url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
global $base_url;
$base_url = curl_getinfo($ch, CURLINFO_EFFECTIVE_URL);
$http_response_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close ($ch);
return $response;
}
The site raw html should be assigned to $response, and then return it.
Your code works. Try echo htmlentities($response); You'll get the raw html for the site you're curling.

Manipulating curl-obtained data before outputting

I'm using the below code to pull the html text from a site to publish on my own.
How can I manipulate the curl handle before echo?
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_VERBOSE, 1);
curl_setopt($ch, CURLOPT_POST, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
$returned = curl_exec($ch);
curl_close ($ch);
echo $returned;
You can manipulate your returned data:
$returned = curl_exec($ch);
curl_close ($ch);
$returned is the response so you can manipulate it
Did you mean something like this

php google weather api query

I'm having trouble retrieving data from the google api. When I run the code, it only returns a blank page and not a printout of the xml array. Here is the code:
$url="http://www.google.com/ig/api";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, "?weather=london,england");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$data = curl_exec($ch);
curl_close($ch);
echo "<pre>";
print_r($data);
I think that the problem is that you use POST method, and not the GET
Try like this
$url="http://www.google.com/ig/api?weather=london,england";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$data = curl_exec($ch);
curl_close($ch);
Hope it helps :)
EDIT: And yes, you have to do some extra parsing to get the data from the XML string

Not getting HTML using curl, why?

Why am I not getting the HTML code when I use PHP curl? This is my code:
// $content = file_get_contents('http://www.datadiary.com/Company/311734/dimsinstituteofhotelmanagement');
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'http://www.datadiary.com/Company/311734/dimsinstituteofhotelmanagement');
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_COOKIESESSION, false);
$content = curl_exec($ch);
curl_close($ch);
echo $content;
By default curl_exec sends the response to the output (usually the browser). Set the CURLOPT_RETURNTRANSFER option if you want curl_exec to return the result instead:
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
Relevant manual entries:
http://php.net/curl-exec
http://php.net/curl-setopt
You need to add the option to return the transfer as a string:
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

cURL code in PHP dumps output to the page

How do I hide the output from curl in PHP?
My code as it stands is the following:
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_USERPWD, $pass);
$result = curl_exec($ch);
curl_close ($ch);
The problem is that is spews out the entire page to the user. Instead I want to capture the output and simply show a "success" or "failed" message?
Use this option to curl_setopt():
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
This will make curl_exec return the data instead of outputting it.
To see if it was successful you can then check $result and also curl_error().
Also make sure to turn off this option:
curl_setopt($ch, CURLOPT_VERBOSE, 0);
Or else it will still print everything to screen.

Categories