How to load random URL from a file in php - php

I am trying to create news portal.
and I want to load random news link which is in the list.txt file
and created a php script which calls the list.txt and load the link from the list.
checkout my codes.
script.php
<?php
$loadlist = explode("\n", file_get_contents('list.txt'));
$rand = rand(0,count($loadlist)-1);
// Here is our random link URL
$picked = $loadlist[$rand];
?>
<meta http-equiv="refresh" content="2;url=<?php echo $picked; ?>">
list.txt
http://news.com/news1.html
http://news.com/news2.html
http://news.com/news3.html
http://news.com/news4.html
http://news.com/news5.html
Above code is working fine now, thanks

Get a random integer based on item count of your $loadlist array.
$loadlist = explode("\n", file_get_contents('list.txt'));
$rand = rand(0,count($loadlist)-1);
// Here is our random link URL
$picked = $loadlist[$rand];

Related

System hit counter SaaS - Via TXT - Imagecopymerge

I am doing an hit counter (view) system, and now I need to find a way to merge all number images into one to be possible to embed in another pages, could someone help me out?
Here is the code I have:
$number = trim(file_get_contents('visitas.txt'));
$file = 'visitas.txt';
$views = file_get_contents ($file);
$fdata = intval($views)+1;
file_put_contents($file, $fdata);
$array = str_split($number);
if(!empty($array)){
foreach($array as $single){
echo '<img style="height:20px" src="'.$single.'.png">';
}
}else{
echo '<img src="0.png">';
}
$image = imagecreatefromstring(file_get_contents('sample.jpg'));
At the moment everything is working, each refresh is counting and sum the number writed by image, but I need to embed this counter in another pages, something like:
<img src='https://countersite.com.br/img-counter-385503.jpg'>
Here is a image of the counter working:
UPDATED
I need help to merge the image already created with the numbers (0.png, 1.png...) into one image (counter1.png).

Random URL File Get Content PHP

i was trying to rotate url using file get content but there's little error which I am not able to figure out.
<?php
//Rotate
$urls = array();
$divs[] = '/fun.jpg';
$divs[] = '/nuf.jpg';
echo file_get_contents(src="'. $divs[rand(0, count($divs)-1)] .'");
?>
But it's not returning any random div.
Well, if you just show images from same folder, as your PHP script is in, you should be able to do it like this. (did not test it)
<?php
header("Content-Type: image/jpg");
$divs[] = 'fun.jpg';
$divs[] = 'nuf.jpg';
echo file_get_contents($divs[array_rand($divs)]);
?>
You need to include header: Show image using file_get_contents
And for picking random key from array you can use array_rand() function: https://www.php.net/manual/en/function.array-rand.php

Get and return media url (m3u8) using PHP

I have a website that hosts videos from a client. On the website the files load externally via m3u8 link.
The client would now like to have those videos on a Roku channel.
If I simply use the m3u8 link from the site it gives an error because the url generated is sent with a cookie and so a client must click and the link to generate a new code for them.
I would like if possible (and I have not seen this here) is to scrape the html page and just return the link via PHP script on the website from the Roku.
I know how to get titles and such using pure php but am having problems returning the m3u8 link..
I do have code to show I am not looking for handouts and actually am trying.
This is what I have used for getting the title name for example.
Note: I would like to know if it is possible to have one php that autofills the html page per url so I do not have to use a different php for each video with the url pretyped in.
<?php
$html = file_get_contents('http://example.com'); //get the html returned from the following url
$movie_doc = new DOMDocument();
libxml_use_internal_errors(TRUE); //disable libxml errors
if(!empty($html)){ //if any html is actually returned
$movie_doc->loadHTML($html);
libxml_clear_errors(); //remove errors for yucky html
$movie_xpath = new DOMXPath($movie_doc);
//get all the titles
$movie_row = $movie_xpath->query('//title');
if($movie_row->length > 0){
foreach($movie_row as $row){
echo $row->nodeValue . "<br/>";
}
}
}
?>
There is a simple approach for this, which involves using regex.
In this example let's say the video M3u8 file is located at: http://example.com/theVideoPage
You would point the video URL Source in your XML to your PHP file.
http://thisPhpFileLocation.com
<?php
$html = file_get_contents("http://example.com/theVideoPage");
preg_match_all(
'/(http.*m3u8)/',
$html,
$posts, // will contain the article data
PREG_SET_ORDER // formats data into an array of posts
);
foreach ($posts as $post) {
$link = $post[0];
header("Location: $link");
}
?>
Now if you want to use a URL that you can append a URL link at the end it could look something like this and you would use an address as such for a Video Url located at
http://thisPhpFileLocation.com?id=theVideoPage
<?php
$id = $_GET['id'];
$html = file_get_contents("http://example.com".$id);
preg_match_all(
'/(http.*m3u8)/',
$html,
$things, // will contain the article data
PREG_SET_ORDER // formats data into an array of posts
);
foreach ($things as $thing) {
$link = $thing[1];
// clear out the output buffer
while (ob_get_status())
{
ob_end_clean();
}
// no redirect
header("Location: $link");
}
?>

php to display csv file in a paginated format

I am new to PHP and, I have a CSV file which, am displaying in my web page. I want the CSV file to be displayed with the pagination option so that the web page would look nice. This is the code I have so far.
<?php
$names = file('demo.csv');
$page = $_GET['page'];
//constructor takes three parameters
//1. array to be paged
//2. number of results per page (optional parameter. Default is 10)
//3. the current page (optional parameter. Default is 1)
$pagedResults = new Paginated($names, 20, $page);
echo "<ul>";
while($row = $pagedResults->fetchPagedRow()) {
//when $row is false loop terminates
echo "<li>{$row}</li>";
}
echo "</ul>";
//important to set the strategy to be used before a call to fetchPagedNavigation
$pagedResults->setLayout(new DoubleBarLayout());
echo $pagedResults->fetchPagedNavigation();
?>
However, the CSV file gets displayed with the commas in the screen. Let us consider the below example. Let's assume we have 40 records in my csv file. The contents of the CSV file are as below.
Author1,Name1,Name2,email
1,John,Smith,john.smith#gmail.com
2,Jack,Gibbs,Jack.gibbs#gmail.com
3,Mike,Dell,Mike.dell#gmail.com
and so on.
In my web page, I am getting the output in 2 pages (as I have set my pagination option to display 20 records in each page.
$pagedResults = new Paginated($names, 20, $page);
The output however still contains the comma from the original CSV file. I want my output to be like below.
First Page:
Author1 Name1 Name2 Email
1 John Smith john.smith#gmail.com
and so on.
Second Page:
Author1 Name1 Name2 Email
and so on.
This is because you are pulling the line as a row, but not parsing the line and outputting it cleanly.
The easiest solution is to parse it and then output it separated by div's with each row wrapped in a div. Then make the whole thing pretty with CSS.
Like this:
<?php
$names = file('demo.csv');
$page = $_GET['page'];
/*
Constructor takes three parameters:
1. array to be paged
2. number of results per page (optional parameter. Default is 10)
3. the current page (optional parameter. Default is 1)
*/
$pagedResults = new Paginated($names, 20, $page);
echo "<div class='CSVtable'>";
while($row = $pagedResults->fetchPagedRow()) {
$data = str_getcsv($row);
$dataRow = implode("</div><div class='csvCol'>", $data);
echo "<div class='csvRow'><div class='csvCol'>{$dataRow}</div>";
}
echo "</div>";
//important to set the strategy to be used before a call to fetchPagedNavigation
$pagedResults->setLayout(new DoubleBarLayout());
echo $pagedResults->fetchPagedNavigation();

How to name and/or download a dynamic rendered image in PHP to redirect to another page

Ok, I have an API I use that holds a scene file in a dotnet project. I pass it params that adds images to a final image that is rendered out on a webpage. i.e. myserver/GetImage.ashx?param1=value1 which I use link to display an image that is dynamically rendered on my webpage. Value1 = myserver/images/myimage.jpg
My problem is I need to be able to take the result and run it back through the API, so value1 cannot equal myserver/GetImage.ashx?param1=myserver/images/myimage.jpg.
My question is, how can I redirect or store the first result in PHP as FinalRender.jpg to run it back through the API, so it would look like myserver/GetImage.ashx?FinalRender.jpg?
I am totally lost so any help would be very appreciated.
Ok, I figured it out.
I created a session ID:
$a = session_id();
if(empty($a)) session_start();
then I grabbed the API image by the HTML ID as shown below:
$a = session_id();
$api_url = 'imgtest2.php';
$dom = new DOMDocument();
if (#$dom->loadHTMLFile($api_url)) {
$img_tag = $dom->getElementById('render');
$src = $img_tag->getAttribute('src');
$img_content = file_get_contents($src);
file_put_contents('user/' . $a . "/" . $a . '.jpg', $img_content);
}

Categories