How to update once a day - php

I want example_homepage.html update once a day , how ?
<?php
$ch = curl_init("http://rss.news.yahoo.com/rss/oddlyenough");
$fp = fopen("example_homepage.html", "w");
curl_setopt($ch, CURLOPT_FILE, $fp);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_exec($ch);
curl_close($ch);
fclose($fp);
$xml = simplexml_load_file('example_homepage.html');
print "<ul>\n";
foreach ($xml->channel->item as $item){
print "<li>$item->title</li>\n";
}
print "</ul>";
?>
Do you know what i mean? This page is very slow , so I want example_homepage.html update once a day. But I dont know how to do ?

you can use cron tab for this in Linux environment
for more info
https://help.ubuntu.com/community/CronHowto
or you can use third party cron services
like
http://cronless.com/

Related

PHP parse array data using file_get_contents($url)

I have the following data I pulled from an API using PHP's file_get_contents($url), I would like to parse this data and put each comma separated value into a variable whilst looping to the next set (there are thousands of row, I simply extracted the first two), I have tried parsing methods however most have an element consisting of the datalabel:data, any assistance would be gladly appreciated.
[[1610223840000,"3.63410000","3.65100000","3.62900000","3.64150000","14194.01000000",1610223899999,"51684.84892800",195,"7619.89000000","27756.15839400","0"],[1610223900000,"3.64610000","3.65090000","3.63410000","3.65000000","2219.73000000",1610223959999,"8090.68646600",46,"1176.75000000","4290.44934900","0"]]
Ok figured this out today, for anyone who needs it
$ch = curl_init();
$url = "https://api.blahblahblah";
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_URL, $url);
$result = curl_exec($ch);
curl_close($ch);
$obj = json_decode($result, true);
foreach ($obj as $key => $value) {
echo $value[0] . "<br>";
echo $value[1] . "<br>";
}

php curl steam-api super slow

I'm making a scoreboard and implementing the steam API to retrieve avatars for users. At first I was using file_get, but it was so slow! So someone suggested I use curl.
Old method
$url = 'http://www.com';
$content = file_get_contents($url);
$json = json_decode($content, true);
I then used a foreach loop to grab the items I wanted from the data.
foreach($output['response']['players'] as $item) {
}
new curl code,
$url = 'www.com';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
echo $output = curl_exec($ch);
curl_close($ch);
$json = json_decode($output, true);
I get pretty much the same result from the json method but it is a little faster. But it is still extremely slow, is there anyway to increase the speed of this? Can I load the table and then load the avatars as they become available?
Scoreboard
http://fyre.site.nfoservers.com/index.php
Consider using for loops since those can speed things up. If you are talking about the load times (time until page loads and displays) being slow, consider using output buffering like this:
Unset arrays or values that you don't need anymore.
Note that the steam API accepts 100 ID's at once, so the friendslist is split into chunks of 100.
It will push out the information once its done, and the site will not wait until it is done. Try it out, I guess.
$totalfriends = count($friends);
$chunkedfriends = array_chunk($friends, 100);
$chunks = ceil($totalfriends / 100);
if(ob_get_length() > 0) {
ob_end_flush();
ob_implicit_flush();}
for($i=0; $i < $chunks; $i++){
$url = "https://api.steampowered.com/ISteamUser/GetPlayerSummaries/v0001/?key=". $steamkey . "&steamids=". implode(',', $chunkedfriends[$i]) . "";
$friendscountchunk = count($chunkedfriends[$i]);
$ch = curl_init();
curl_setopt($ch, CURLOPT_PIPEWAIT, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_URL,$url);
$urlresult=curl_exec($ch);
curl_close($ch);
$json_decoded = json_decode($urlresult);
if(ob_get_length() > 0) {
ob_end_flush();
ob_implicit_flush();}
for($x=0; $x < $friendscountchunk; $x++){
?>
<li class="friendsli"><a href="steamuser.php?id=<?=$json_decoded->response->players->player[$x]->steamid?>">
<img src=' <?=$json_decoded->response->players->player[$x]->avatar?>'/><p class="friendname"> <?=$json_decoded->response->players->player[$x]->personaname?> </p>
</a></li> <?php
}}
unset($friends); unset($player); unset($json_decoded);
I don't think it is the best script or method, but it will help for sure.
You cannot speed up an external API, but you can improve and adapt your code.

curl multi download image

I would like if it's possible to optimize this request with multi curl handler ?
Thanks
$array_img = array(
'https://www.foooooobbbaaarrr.fr/images/1.jpeg',
'https://www.foooooobbbaaarrr.fr/images/2.jpeg',
'https://www.foooooobbbaaarrr.fr/images/3.jpeg',
'https://www.foooooobbbaaarrr.fr/images/4.jpeg');
foreach ($array_img as $k => $v)
{
$ch = curl_init($v);
$name = ($k + 1).'.jpeg';
$fp = fopen($name, 'wb');
curl_setopt($ch, CURLOPT_FILE, $fp);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_exec($ch);
curl_close($ch);
fclose($fp);
}
That's a very open-ended question, but with the PHP/CURL's multi functions you can at least get all those files in parallel instead of getting them in a serial manner.

Php not reading xml data

Hello I am getting xml feed from expedia and trying to parse it onto the screen. It is not returning the data, and I can not figure out why. the only thing that will return is activePropertyCount. Here is the url where I am doing this if you want to take a look: http://travellinginmexico.com/test/index.php and here is the xml that I am trying to parse: http://travellinginmexico.com/test/data.xml
My code:
<?php
$post_string = 'type=xml&cid=55505&minorRev=5&apiKey=4sr8d8bsn75tpcuja6ypx5g3&locale=en_US&currencyCode=USD&customerIpAddress=10.184.2.9&customerUserAgent=Mozilla/5.0+(Windows+NT+6.1)+AppleWebKit/535.11+(KHTML,+like+Gecko)+Chrome/17.0.963.79+Safari/535.11&customerSessionId=&xml=<HotelListRequest><arrivalDate>04/05/2012</arrivalDate><departureDate>04/07/2012</departureDate><RoomGroup><Room><numberOfAdults>2</numberOfAdults></Room><Room><numberOfAdults>2</numberOfAdults><numberOfChildren></numberOfChildren></Room></RoomGroup><city>Cancun</city><countryCode>MX</countryCode><supplierCacheTolerance>MED_ENHANCED</supplierCacheTolerance></HotelListRequest> ';
$path = "http://api.ean.com/ean-services/rs/hotel/v3/list"; //Relative path to the file with $_POST parsing
$ch = curl_init($path);
$fp = fopen('data.xml','w');
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_string); //Send the data to the file
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Accept: application/xml'));
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_FILE, $fp);
$val = curl_exec($ch);
curl_close($ch);//Close curl session
fclose($fp); //Close file overwrite
$data = simplexml_load_file('data.xml');
echo "<ul id=\"data\">\n";
foreach ($data as $info):
$title=$info->HotelList;
$add=$info->address1;
$count=$info['activePropertyCount'];
echo "<li><div class=\"title\">",$title,"</div><div class=\"artist\">by ",$add,"</div><time>",$count,"</time></li>\n";
endforeach;
echo "</ul>";
?>
It appears that you are not trying to retrieve your data from the correct part of the XML object. I have updated your code to read from the HotelList->HotelSummary section, and it will now display the hotel name and address properly.
//Use Curl to get XML here.
$data = simplexml_load_file('data.xml');
//Uncomment this to view the parsed XML on screen.
/*
echo '<pre>';
print_r($data->HotelList);
echo '</pre>';
*/
echo "<ul id=\"data\">\n";
foreach($data->HotelList->HotelSummary as $info):
$title=$info->name;
$add=$info->address1;
$count=$data->HotelList['activePropertyCount'];
echo "<li><div class=\"title\">",$title,"</div><div class=\"artist\">by ",$add,"</div><time>",$count,"</time></li>\n";
endforeach;
echo "</ul>";
I recommend checking out libxml_get_errors. There's a fairly good example on how to use it in the PHP documentation for the function. It should be able to tell you what problems are occurring during simplexml_load_file.
If you think the problem is coming from cURL (or to be a little more thorough) you can use the cURL error functions as well. (Such as curl_error).

How to run cURL once, checking domain availability in a loop? Help fixing code please

What I have in place, is a domain availability check, which connects up to an API and outputs "Available: and Unavailable:" from $tmp. Ths below code will only check the availability ONCE.
I would like to check the availability of the domain, multiple times (possibly on a loop?), without having to run restart cURL connection everytime (as it wastes time).
I just don't know how I can connect to cURL once and run the loop (doing the check through the API).
Current code
<?php
function GetCurlPage ($pageSpec)
{
$ch = curl_init($pageSpec);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
$tmp = curl_exec ($ch);
curl_close ($ch);
$tmp = preg_replace('/(?s)<meta http-equiv="Expires"[^>]*>/i', '', $tmp);
$tmp = explode('<br>', $tmp);
echo $tmp[0];
echo "<br>";
echo $tmp[1];
echo "<br>";
return $tmp;
}
$returnUrl = "http://www.mysite.com.au/check.php";
$url = "https://www.apisite.com.au/availability/check.php?domain=testdomain&suffixes=.com.au";
$output = GetCurlPage("$url");
?>
link textI think you're after the callback options documented. For example --> CURLOPT_SEEKFUNCTION

Categories