Im new to Php. I kinda need code to open Links in browser when script is loaded.
heres my code below.
<?php
$links = array_open("https://stackoverflow.com/",
"https://outlook.office.com/",
"https://www.protectedtext.com/",
"https://www.adobe.com/",
"https://www.linkedin.com");
echo $links[array_rand($links)];
?>
Hi change array_open to array
<?php
$links = array("https://stackoverflow.com/", "https://outlook.office.com/", "https://www.protectedtext.com/", "https://www.adobe.com/", "https://www.linkedin.com");
echo $links[array_rand($links)];
?>
And if you want to send the browser to the link you would send a location header like so...
<?php
$links = array("https://stackoverflow.com/", "https://outlook.office.com/", "https://www.protectedtext.com/", "https://www.adobe.com/", "https://www.linkedin.com");
header('Location: ' . $links[array_rand($links)]);
exit;
?>
Related
I'm trying to parse the site, but the cat doesn't output anything
<?php
include_once 'simple_html_dom.php';
$html = file_get_html('https://teleprogramma.pro/headlines');
foreach($html->find('.text-part') as $element) {
echo $element->outertext;
}
?>
There are no Elements in the document which match the class .text-part. You can look at the source code when you save the HTML into a file.
<?php
include_once 'simple_html_dom.php';
$html = file_get_html('https://teleprogramma.pro/headlines');
file_put_contents('htmlData.html', $html);
When you try for example to find .block-top-section-posts you'll get a result.
<?php
include_once 'simplehtmldom_1_9_1/simple_html_dom.php';
$html = file_get_html('https://teleprogramma.pro/headlines');
foreach($html->find('.block-top-section-posts') as $element) {
echo $element->outertext;
}
// Outputs
/*
<div class="vue-container block-top-section-posts"> <div id="vuetag-top-section-posts" class="vue-tag news-line" data-url="/rest/term-additional-loading/section/76217/0/0" data-max-width="0" data-min-width="0" data-information=""> </div> </div>
*/
When you lookup the Site in a Browser you will get redirected to another URL. If you want to use that, have a look at php get url of redirect from source url to get the final address.
I've been working on a blogging system and I wish to receive information from external php files for a post's Title and Content, here is my code:
External (Blog Post's Content) File: 03-20-2018-This-Is-A-Test.php
<?php
$Title = 'This is a test!';
$Date = '03-20-2018';
$Content = 'Blog Post's content!';
?>
Client Side Page For Viewing Blog Posts: Post Home.php
<?php
$Title = '';
$Date = '';
$Content = '';
$GetDate = $_GET['d'];
$GetTitle = $_GET['t'];
$postPath = "Posts/$GetDate"."-"."$GetTitle.php";
$postFile = fopen($postPath,"rt");
$postContent = fgets($postFile,filesize($postPath));
?>
<!--Some HTML-->
<h2><? echo $Title; ?></2>
<b><? echo $Date; ?></b>
<p><? echo $Content; ?></p>
<!--Some More HTML-->
<? fclose($postFile); ?>
Client's Page URL: example.com/Post%20Home.php?d=03-20-2018&t=This-Is-A-Test
Reformatted as: example.com/03-20-2018/This-Is-A-Test
As you can see, I am using GET parameters to call the file that I wish to collect the information from.
I have tried using fopen which I wasn't able to get to work. Also include is forbidden with the particular server hosts I am using.
I am open to using file_get_contents if someone is able to help me get it to work.
Note: I have confirmed that my calling URL is correct and I get no errors from my php
So, I am trying to use fopen to collect the data of $Title $Date $Content from the file; 03-20-2018-This-Is-A-Test.php and use that information in the Client Side file; Post Home.php
I was able to find the answer using require().
I have this PHP function that pulls/extracts data from xml elements and displays them on my webpage. However it is only working when used with a local path. not from any external sources. Here is the code.
Index.php
<html>
<body>
<?php
include('render_xml_to_html.php');
// The internal path. DOES work.
render_xml_data('example.xml');
?>
</body>
</html>
Example.xml
<eveapi version="2"><currentTime>2014-05-08 03:34:23</currentTime>
<result>
<serverOpen>True</serverOpen>
<onlinePlayers>24957</onlinePlayers>
</result>
<cachedUntil>2014-05-08 03:35:58</cachedUntil>
</eveapi>
The function - render_xml_to_html.php
<?php
function render_xml_data($path_to_xml_file){
if (!file_exists($path_to_xml_file)){
return;
}else{
$chars_to_replace = array('[\r]','[\n]','[\t]');
$xmlstring = trim(preg_replace($chars_to_replace, '', file_get_contents($path_to_xml_file)));
}
$xml = new SimpleXMLElement($xmlstring);
foreach ($xml->result as $record) {
echo '<div class="record">'."\n";
echo '<h3>'.$record->onlinePlayers.'</h3>'."\n";
echo '</div><!--end record-->'."\n";
}
}
?>
The above code works as is. My issue is when I try to pull this info from the realtime .xml file on the hosts server. That url is:
https://api.eveonline.com/server/ServerStatus.xml.aspx/
When I replace example.xml with the above link it fails to work. So the following doe not work. Where I link to an external path rather than a local.
<?php
include('render_xml_to_html.php');
// The external path DOES NOT WORK
render_xml_data('https://api.eveonline.com/server/ServerStatus.xml.aspx/');
?>
Thanks in advance!
You can just use file_get_contents on this one, it can get the job done. Consider this example:
$url = 'https://api.eveonline.com/server/ServerStatus.xml.aspx/';
// access the url and get that file
$contents = file_get_contents($url);
// convert it to an xml object
$contents = simplexml_load_string($contents);
echo "<div class='record'>Number of Online Players: ".$contents->result->onlinePlayers."</div>";
Sample Output:
Number of Online Players: 23893
I have php reading a text file that contains all the names of images in a directory, it then strips the file extension and displays the file name without the .jpg extension as a link to let the user click on then name, what I am looking for is a easy way to have the link that is clicked be transferred to a variable or find a easier solution so the link once it is clicks opens a page that contains the default header and the image they selected without making hundreds of HTML files for each image in the directory.
my code is below I am a newbie at PHP so forgive my lack of knowledge.
thank you in advance. also I would like a apple device to read this so I want to say away from java script.
<html>
<head>
<title>Pictures</title>
</head>
<body>
<p>
<?php
// create an array to set page-level variables
$page = array();
$page['title'] = ' PHP';
/* once the file is imported, the variables set above will become available to it */
// include the page header
include('header.php');
?>
<center>
<?php
// loads page links
$x="0";
// readfile
// set file to read
$file = '\filelist.txt' or die('Could not open file!');
// read file into array
$data = file($file) or die('Could not read file!');
// loop through array and print each line
foreach ($data as $line) {
$page[$x]=$line;
$x++;
}
$x--;
for ($i = 0; $i <= $x; $i++)
{
$str=strlen($page[$i]);
$str=bcsub($str,6);
$strr=substr($page[$i],0,$str);
$link[$i]= "<a href=".$page[$i]."jpg>".$strr."</a>";
echo "<td>".$link[$i]."<br/";
}
?>
</P></center>
<?php
// include the page footer
include('/footer.php');
?>
</body>
</html>
add the filename to the url that you want to use as a landing page, and catch it using $_GET to build the link.
<a href='landingpage.php?file=<?php echo $filename; ?>'><?php echo $filename; ?></a>
Then for the image link on the landing page
<img src='path/to/file/<?php echo $_GET['file'] ?>.jpg' />
I am trying to link a tumblr feed to a website. I found this code (As you can see, something must be broken with it as it doesnt even format correctly in this post):
<?php
$request_url = “http://thewalkingtree.tumblr.com/api/read?type=post&start=0&num=1”;
$xml = simplexml_load_file($request_url);
$title = $xml->posts->post->{‘regular-title’};
$post = $xml->posts->post->{‘regular-body’};
$link = $xml->posts->post[‘url’];
$small_post = substr($post,0,320);
echo ‘<h1>’.$title.’</h1>’;
echo ‘<p>’.$small_post.’</p>’;
echo “…”;
echo “</br><a target=frame2 href=’”.$link.”’>Read More</a>”;
?>
And i inserted the tumblr link that I will be using. When I try to preview my HTML, i get a bunch of messed up code that reads as follows:
posts->post->{'regular-title'}; $post = $xml->posts->post->{'regular-body'}; $link = $xml->posts->post['url']; $small_post = substr($post,0,320); echo '
'.$title.'
'; echo '
'.$small_post.'
'; echo "…"; echo "Read More"; ?>
Any help would be appreciated. Thank you!
That is PHP, not HTML. You need to process it with a PHP parser before delivering it to a web browser.
… it should also be rewritten so it can cache the remote data, and escape special characters before injecting the data into an HTML document.