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.
Related
I use curl in php to get data from a website and add to my mysql database.
But the string I get is formatted strange. i've tried some method but didn't help me. who used to deal with it please give me your solution.
My curl method here:
function grab_page($site){
$ch = curl_init();
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
curl_setopt($ch, CURLOPT_TIMEOUT, 40);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_COOKIEFILE, "cookie.txt");
curl_setopt($ch, CURLOPT_URL, $site);
ob_start();
return curl_exec($ch);
ob_end_clean();
curl_close ($ch);
}
After get it, i echo the html, my method here:
function getDetailPage(){
$detailData = grab_page("https://www.deliverynow.vn/ho-chi-minh/hanuri-quan-an-han-quoc-xo-viet-nghe-tinh");
echo htmlspecialchars($detailData);
}
The html string i got is change to weird character like: Món khác . But it should be like this: Món khác
Use html_entity_decode to restore characters.
I'm trying to load information from Wikipedia with cURL and PHP, but it's not working as intended.
See my code :
$url = "http://en.wikipedia.org/w/api.php?action=opensearch&format=xml&limit=1&search=stackoverflow";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_USERAGENT, 'USERAGENT');
$result = curl_exec($ch);
curl_close($ch);
//return $result;
var_dump($result);
It doesn't return anything. However, if you load the address in your browser, it works!
So I'm wondering what should I change in order to execute this cURL request well.
Thank you folks !
The request is redirecting to https. So either reference the https uri instead or set CURLOPT_FOLLOWLOCATION to true:
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
I am trying to Get from my Api,Im using curl in php to log into on one page and
then get another page passing all cookies from the first page along with you but I keep getting Access denied!!
$ch = curl_init();
curl_setopt($ch, CURLOPT_COOKIEJAR, "/tmp/cookieFileName");
curl_setopt($ch, CURLOPT_URL,"http://www.supersaas.com/api/users");
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, "?account=myname&password=mypassword");
ob_start(); // prevent any output
curl_exec ($ch); // execute the curl command
ob_end_clean(); // stop preventing output
curl_close ($ch);
unset($ch);
$ch = curl_init();
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch, CURLOPT_COOKIEFILE, "/tmp/cookieFileName");
curl_setopt($ch, CURLOPT_URL,"http://www.supersaas.com/api/users?id=12");
$buf2 = curl_exec ($ch);
curl_close ($ch);
echo "<PRE>".htmlentities($buf2);
What's wrong with this code????
Thanks
Your login didn't successful, hence you had invalid cookie in the file and got your second curl request denied from the server.
Remove the ? mark from the ?account. So it will be
curl_setopt($ch, CURLOPT_POSTFIELDS, "account=myname&password=mypassword");
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);
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>";