$hotel_query = "select hotel_id,hotel_name,trip_url,automatic_status from hotels where automatic_status='0'";
$hotel_result = mysql_query($hotel_query) or die(mysql_error());
while($hotel_row = mysql_fetch_object($hotel_result))
{
$url=$hotel_row->trip_url;
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($ch);
curl_close($ch);
echo curl_error($ch);
echo $result;
}
The above code is running via cron job in php.There are 5 trip_url in hotels table. It means curl should execute 5 times and return result from server 5 times but
but when I run this only one result is printed and stop executing.
Give this a try:
<?php
function curl_download($Url){
// is cURL installed yet?
if (!function_exists('curl_init')){
die('Sorry cURL is not installed!');
}
// OK cool - then let's create a new cURL resource handle
$ch = curl_init();
// Now set some options (most are optional)
// Set URL to download
curl_setopt($ch, CURLOPT_URL, $Url);
// Set a referer
curl_setopt($ch, CURLOPT_REFERER, "http://www.example.org/yay.htm");
// User agent
curl_setopt($ch, CURLOPT_USERAGENT, "MozillaXYZ/1.0");
// Include header in result? (0 = yes, 1 = no)
curl_setopt($ch, CURLOPT_HEADER, 0);
// Should cURL return or print out the data? (true = return, false = print)
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
// Timeout in seconds
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
// Download the given URL, and return output
$output = curl_exec($ch);
// Close the cURL resource, and free system resources
curl_close($ch);
return $output;
}
$loop_counter = 1;
$hotel_query = "select hotel_id,hotel_name,trip_url,automatic_status from hotels where automatic_status='0'";
$hotel_result = mysql_query($hotel_query) or die(mysql_error());
while($hotel_row = mysql_fetch_object($hotel_result)){
$url=$hotel_row->trip_url;
echo "Loop no.".$loop_counter."<br />";
echo curl_download($url);
$loop_counter++;
}
?>
The CURL code is from here
Related
I've written some simple code which should enable the retrieval of a given webpage, in this case Google.
<?php
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://www.google.com/");
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_exec($ch);
curl_close($ch);
?>
Although it works, I've noticed when I click some of the hyperlinks, for instance the 'Privacy' hyperlink, I get redirected to http://mywebsite.com/intl/en/policies/privacy/ which obviously doesn't exist. Why does this happen? And is it possible to get redirected to the correct link?
<?php
function cURL() {
// Create a new cURL resource
$curl = curl_init();
if (!$curl) {
die("Couldn't initialize a cURL handle");
}
// Set the file URL to fetch through cURL
curl_setopt($curl, CURLOPT_URL, "http://ctrlq.org/");
// Set a different user agent string (Googlebot)
curl_setopt($curl, CURLOPT_USERAGENT, 'Googlebot/2.1 (+http://www.google.com/bot.html)');
// Follow redirects, if any
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
// Fail the cURL request if response code = 400 (like 404 errors)
curl_setopt($curl, CURLOPT_FAILONERROR, true);
// Return the actual result of the curl result instead of success code
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
// Wait for 10 seconds to connect, set 0 to wait indefinitely
curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 10);
// Execute the cURL request for a maximum of 50 seconds
curl_setopt($curl, CURLOPT_TIMEOUT, 50);
// Do not check the SSL certificates
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
// Fetch the URL and save the content in $html variable
$html = curl_exec($curl);
// Check if any error has occurred
if (curl_errno($curl))
{
echo 'cURL error: ' . curl_error($curl);
}
else
{
// cURL executed successfully
print_r(curl_getinfo($curl));
}
// close cURL resource to free up system resources
curl_close($curl);
}
?>
I am trying to get the contents of this webpage to be shown.
https://sofifa.com/players?aeh=22&ptl=84&pth=99&pn=27&pn=25&pn=23?
But unfortunately, it is not working.
Here is the code
<?php
function curl_download($Url){// is cURL installed yet?
if (!function_exists('curl_init')){
die('Sorry cURL is not installed!');
}
// OK cool - then let's create a new cURL resource handle
$ch = curl_init("https://sofifa.com/players?aeh=22&ptl=84&pth=99&pn=27&pn=25&pn=23");
// Now set some options (most are optional)
// Set URL to download
curl_setopt($ch, CURLOPT_URL, $ch);
// User agent
curl_setopt($ch, CURLOPT_USERAGENT, "MozillaXYZ/1.0");
// Include header in result? (0 = yes, 1 = no)
curl_setopt($ch, CURLOPT_HEADER, 0);
// Should cURL return or print out the data? (true = return, false = print)
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
// Timeout in seconds
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
// Download the given URL, and return output
$output = curl_exec($ch);
// Close the cURL resource, and free system resources
curl_close($ch);
return $output;
}
?>
<?php
include_once('AFIFAtest.php');#this is the file name of the program
curl_download($url);
?>
This Code is Working Well
<?php
// create curl resource
$ch = curl_init();
// set url
curl_setopt($ch, CURLOPT_URL, "http://ipdev.in/");
//return the transfer as a string
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
// $output contains the output string
$output = curl_exec($ch);
echo $output;
// close curl resource to free up system resources
curl_close($ch);
?>
But below two bunch of codes are not working. I am not getting an error but in these cases browser loading bar is just revolving and revolving and never stops. The page shows loading and loading for a long time but nothing loads from URL. Where is the problem ?
<?php
// create curl resource
$ch = curl_init();
// set url
curl_setopt($ch, CURLOPT_URL, "https://iiitd.ac.in/");
//return the transfer as a string
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
// $output contains the output string
$output = curl_exec($ch);
echo $output;
// close curl resource to free up system resources
curl_close($ch);
?>
<?php
// create curl resource
$ch = curl_init();
// set url
curl_setopt($ch, CURLOPT_URL, "http://iiitd.ac.in");
//return the transfer as a string
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
// $output contains the output string
$output = curl_exec($ch);
echo $output;
// close curl resource to free up system resources
curl_close($ch);
?>
the link https://iiitd.ac.in/ is redirecting to https://www.iiitd.ac.in/ so you need to modify your curl code. You need to set CURLOPT_FOLLOWLOCATION as true.
Have a look on below solution:
<?php
// create curl resource
$ch = curl_init();
// set url
curl_setopt($ch, CURLOPT_URL, "https://iiitd.ac.in/");
//return the transfer as a string
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
// added follow location
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
// $output contains the output string
$output = curl_exec($ch);
echo $output;
// close curl resource to free up system resources
curl_close($ch);
I have an url http://www.example.com/all.php?f=1. When I open the url, it redirect to http://www.example.com/all.php with value of f (where f = 1). I try to grab the redirected URL using curl but it still fails. I try to search anything in this website but almost all of the question is about how to get the url, not how to grab it.
I try with this function :
function get_url_content($url,$timeout) {
$ch = curl_init($url); // initialize curl with given url
curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER["HTTP_USER_AGENT"]); // set useragent
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE); // write the response to a variable
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE); // follow redirects if any
curl_setopt($ch, CURLOPT_MAXREDIRS, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout); // max. seconds to execute
curl_setopt($ch, CURLOPT_FAILONERROR, 1); // stop when it encounters an error
$output = curl_exec($ch);
curl_close($ch);
if (!$output) {
return -1;
}
return $output;
}
But when I run it :
$url = 'http://www.example.com/all.php?f=1';
$me = get_url_content($url);
echo $me; //it should grab the page http://www.example.com/all.php
It returns -1, which mean there are no output. I confused about it. Any solution?
Change it to
$url = 'http://www.example.com/';
$me = file_get_content($url);
echo $me;
I use this piece of code below to send data to another server via a url and it work successfully. I want to capture the response from the server and process it but I can't seem to capture it.
CODE
$url="http://www.example.com/com_spc/api.php?username=".urlencode($uname)."&password=".urlencode($pwd);
$ch = curl_init(); // create cURL handle (ch)
if (!$ch) {
die("Couldn't initialize a cURL handle");
}
// set some cURL options
$ret = curl_setopt($ch, CURLOPT_URL, $url);
$ret = curl_setopt($ch, CURLOPT_HEADER, 0);
$ret = curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 0);
$ret = curl_setopt($ch, CURLOPT_RETURNTRANSFER, 0);
$ret = curl_setopt($ch, CURLOPT_TIMEOUT, 30);
// execute
$ret = curl_exec($ch);
if (empty($ret)) {
// some kind of an error happened
die(curl_error($ch));
curl_close($ch); // close cURL handler
} else {
$info = curl_getinfo($ch);
curl_close($ch); // close cURL handler
if (empty($info['http_code'])) {
die("No HTTP code was returned");
} else {
}
}
Make sure to set CURLOPT_RETURNTRANSFER to 1. Otherwise curl_exec will not return anything