File gets content - php

I work for a voluntary fire brigade and years ago I created an operational status code using file_get_contents. Now it has no function anymore and I have no idea how to fix it.
<?php
$word = array ('T1','T2','T3','B1','B2','B3','B4','S1','S2','S3');
$word2 = array ('keine Einsätze');
$quelltext = file_get_contents("https://www.feuerwehr-huettendorf.at/einsatz.html");
foreach ($word as $einsatz) {
$pos = strpos($quelltext, $einsatz);
if($pos !== FALSE)
{
echo '<img title="Feuerwehr im Einsatz" alt="Feuerwehr im Einsatz" width="160" height="50" src="/images/content/icons/einsatz.gif" />';}
}
foreach ($word2 as $bereit) {
$pos2 = strpos($quelltext, $bereit);
if($pos2 !== FALSE)
{
echo '<img title="Einsatzbereit" alt="Einsatzbereit" width="160" height="50" src="/images/content/icons/einsatzbereit.gif" />';}
}
?>

The source code of that URL you've mentioned is (at the time of writing) simply this:
<iframe frameborder="0" style="height: 300px; width: 100%; frameborder: 0px" width="100%" height="680" src="https://www.feuerwehr-krems.at/Dokumente/Bezirk/Die%20Feuerwehren/Die%20Feuerwehren/FFInfo_Allgemein.asp?EldisID=222201&Select=1" scrolling="no"></iframe>
It doesn't contain any of the content your code is looking for. I wonder if someone changed the way the server-side application is set up.
You need to directly get the source code of the URL mentioned in that iframe instead, e.g.
$quelltext = file_get_contents("https://www.feuerwehr-krems.at/Dokumente/Bezirk/Die%20Feuerwehren/Die%20Feuerwehren/FFInfo_Allgemein.asp?EldisID=222201&Select=1");
This contains the real contents of the page.

Related

PHP 1 is displayed instead of an image

public function main($content, array $conf) {
$this->conf = $conf;
$this->pi_setPiVarDefaults();
$this->pi_loadLL();
$content = '';
$background_image = $this->cObj->parentRecord['data']['media'];
// Wenn ein Bild vorhanden ist
if ($background_image != '') {
$content .= '<img src="uploads/media/'.$background_image.'" alt="" title="" width="100%" />';
}
return $this->pi_wrapInBaseClass($content);
}
When expecting html I'm getting path like this:
<img src="uploads/media/1" alt="" title="" width="100%">
This is not written by me so that's why it's to understand why it behaves the way it does. Appreciate any tips.
['data']['media'] is a field used by FAL. The field itself only contains the number of references.
You need to resolve these reference with the FAL API, you may take a look here

PHP: Check if file exists at url

I have a small problem of needed to check which of 3 possible file paths relates to the correct image. I've always used get_headers for this in the past - but there are up to 100 images to load on any given pagination - so thats a potential of 300 get_headers in a worst case scenario and its painfully slow on my university server space.
However for any given steam_id there is the potential for images to exist at 2 of the possible file paths. Which means if I just include all three of urls and use Jquery to hide the on errors, for around half the results I end up with 2 images (which is in all fairness faster then what I'm doing currently, but at least the get_headers method looks fine.
Here's what I'm using so far:
//Use Header Response to determine which img to use
$url1 = 'http://cdn4.steampowered.com/v/gfx/apps/' . $row -> steam_id_clean . 'header_292x136.jpg';
$url2 = 'http://cdn4.steampowered.com/v/gfx/apps/' . $row -> steam_id_clean . 'header.jpg';
$header_response = get_headers($url1, 1);
if (strpos($header_response[0], "404") !== false) {
// File does not exist at url1 - recurse.
$header_response = get_headers($url2, 1);
if (strpos($header_response[0], "404") !== false) {
//File does not exist at URL - Therefore must be at URL3
echo '<img src = "http://cdn4.steampowered.com/v/gfx/subs/' . $row -> steam_id_clean . 'header.jpg" style="width: 200px; height: auto;" >';
} else {
//File exists at URL2
echo '<img src = "http://cdn4.steampowered.com/v/gfx/apps/' . $row -> steam_id_clean . 'header.jpg" style="width: 200px; height: auto;" >';
}
} else {
// File exists at URL1
echo '<img src = "http://cdn4.steampowered.com/v/gfx/apps/' . $row -> steam_id_clean . 'header_292x136.jpg" style="width: 200px; height: auto;" >';
}
This data is too dynamic - so I can't write a scrape to grab the images. Any Suggestions in a better method than get_headers would be appreciated. I don't have access to curl (unfortunately) on my university server either!

change img tag structure for width and height

I have an editor in my site that will save images in this format automatically:
<img alt="image-alt" src="image-path" style="width: Xpx; height: Ypx;" title="image-title" />
this tag will save in static .html file and then will shows in my site with readfile()...
I want to change this structure before saving it in static .html file to this new format:
<img alt="image-alt" src="image-path" width="Xpx" height="Ypx" title="image-title" />
infact, I want to change the way "width" and "height" is writing in static html file.
I'm using PHP and can run any function on the html string before fwrite() it.
thanks.
I started off thinking this'd be quite easy using preg_replace_callback, but it turned into a bit of a monster. I'm sure it could easily be improved with a bit of refactoring:
<?php
// Some HTML test data with two images. For one image I've thrown in some extra styles, just
// to complicate things
$content= '<img alt="image-alt-2" src="image-path" style="width: 20px; height: 15px; border: 1px solid red;" title="image-title" />
<p>Some other tags. These shouldn\'t be changed<br />Etc.</p>
<img alt="image-alt-2" src="image-path-2" style="width: 35px; height: 30px;" title="another-image-title" />
<p>This last image only has a width and no height</p>
<img alt="image-alt-3" src="image-path-3" style="width:35px;" title="another-image-title" />';
$content= preg_replace_callback('/<img ((?:[a-z]+="[^"]*"\s*)+)\/>/i', 'replaceWidthHeight', $content);
var_dump($content);
function replaceWidthHeight($matches) {
// matches[0] will contain all the image attributes, need to split
// those out so we can loop through them
$submatches= array();
$count= preg_match_all('/\s*([a-z]+)="([^"]*)"/i', $matches[1], $submatches, PREG_SET_ORDER);
$result= '<img ';
for($ndx=0;$ndx<sizeof($submatches);$ndx++) {
if ($submatches[$ndx][1]=='style') {
// Found the style tag ...
$width= ''; // Temporary storage for width and height if we find them
$height= '';
$result.= ' style="';
$styles= split(';', $submatches[$ndx][2]);
foreach($styles as $style) {
$style= trim($style); // remove unwanted spaces
if (strlen($style)>6 && substr($style, 0, 6)=='width:') {
$width= trim(substr($style, 6));
}
elseif (strlen($style)>7 && substr($style, 0, 7)=='height:') {
$height= trim(substr($style, 7));
}
else { // Some other style - pass it through
$result.= $style;
}
}
$result.= '"';
if (!empty($width)) $result.= " width=\"$width\"";
if (!empty($height)) $result.= " height=\"$height\"";
}
else {
// Something else, just pass it through
$result.= $submatches[$ndx][0];
}
}
return $result.'/>';
}

correct syntax to echo variable inside iframe

I know i am missing something simple. I just want to display this iframe if $video-code exists. Can anyone see what is wrong with this? working in wordpress. error is on the echo line. i've also tried adding .'$video-code'. into the url.
it is displaying the iframe correctly, but the variable is displaying as text in the url. if i call the variable elsewhere in the page without the If statement, it displays correctly.
THANKS for any help!
<?php
$key = 'video-code';
$themeta = get_post_meta($post->ID, $key, TRUE);
if($themeta != '') {
echo '<iframe id="player" width="560" height="315" frameborder="2" src="http://www.youtube.com/embed/$video-code" ></iframe>';
}?>
You can concatenate your $key, like so:
echo '<iframe id="player" width="560" height="315" frameborder="2"
src="http://www.youtube.com/embed/' . $key . '" ></iframe>';

PHP: Youtube latest video feed php code mechanism

I am currently adapted a code to my website's use but now I would like to change some of its format but it has been a harder task than expected. My code right now is displaying the latest video. But my goal at the moment is to have the code display the videos *thumbnail pic, *video description and *total views. Below is my code, If you think there is a better way to approach this then I am open for suggestions:
<?
error_reporting(E_ALL);
$feedURL = 'http://gdata.youtube.com/feeds/api/users/USERNAME/uploads?max-results=20';
$sxml = simplexml_load_file($feedURL);
$i = 0;
foreach ($sxml->entry as $entry) {
$media = $entry->children('media', true);
$url = (string)$media->group->player->attributes()->url;
$index = strrpos($url, "&");
$url = substr($url, 0, $index);
$index = strrpos($url, "watch");
$url = substr($url, 0, $index) . "v/" . substr($url, $index + 8, strlen($url) - ($index + 8));
echo '<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" width="400" height="250" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,40,0"><param name="allowFullScreen" value="true" /><param name="allowscriptaccess" value="always" /><param name="src" value="' . $url . '" /><param name="allowfullscreen" value="true" /><embed type="application/x-shockwave-flash" width="400" height="250" src="' . $url . '" allowscriptaccess="always" allowfullscreen="true"></embed></object>';
break;
}
?>
To build on Chris Willenbrock's work, in order to simplify the code a little and save yourself some overhead (an extra entries 20-$count across the wire, extra explode on 20-$count entries that won't be displayed anyway):
//SETTINGS
$channel_name = 'mychannelname';//Be sure to change this to your channel
$count = 8;//# of videos you want to show (MAX = 20)
$em_width = 420;//width of embedded player
$em_height = 315;//height of embedded player
$wrap_class = 'video';//class name for the div wrapper
//The output...
$sxml = simplexml_load_file("http://gdata.youtube.com/feeds/api/users/$channel_name/uploads?max-results=$count");
foreach ($sxml->entry as $entry) {
$vidKey = substr(strrchr($entry->id,'/'),1);
echo "
<div class=\"$wrap_class\">
<iframe width=\"$em_width\" height=\"$em_height\" src=\"http://www.youtube.com/embed/$vidKey\" frameborder=\"0\" allowfullscreen></iframe>
</div>
";
}
I don't enjoy working with XML and avoid it where I can, so here is another option that uses JSON. Also, note that by switching to v2 of the API here we get much cleaner access to the video key, as well as the other meta-data that the original poster was asking for:
//The output...
$api_v2 = "http://gdata.youtube.com/feeds/api/users/$channel_name/uploads?max-results=$count&v=2";
foreach (json_decode(file_get_contents("$api_v2&alt=json"))->feed->entry as $entry) {
// meta information
$title = $entry->title->{'$t'};
$description = $entry->{'media$group'}->{'media$description'}->{'$t'};
$views = $entry->{'yt$statistics'}->viewCount;
$thumbnails = $entry->{'media$group'}->{'media$thumbnail'};
// few different thumbnail image choice here:
// 0 => default image, low res - "default"
// 1 => default image, medium res - "mqdefault"
// 2 => default image, higher res - "hqdefault"
// 3 => first frame of vid, low res - "start"
// 4 => middle frame, low res - "middle"
// 5 => last frame, low res - "end"
$thumb_img = $thumbnails[1]; // I'll go with default, medium res
echo "
<!-- meta information output - format to taste -->
<div>
<img src='$thumb_img->url' style='float: left; margin-right: 10px;' width='$thumb_img->width' height='$thumb_img->height' alt='{$thumb_img->{'yt$name'}}'>
<b>Title:</b> $title<br><br>
<b>Description:</b> $description<br><br>
<b>Views:</b> $views
<br style='clear: left;'>
</div>
<div class=\"$wrap_class\">
<iframe width=\"$em_width\" height=\"$em_height\" src=\"{$entry->content->src}\" frameborder=\"0\" allowfullscreen></iframe>
</div>
";
}
Add
var_dump($entry);exit;
at the first line inside the foreach code, then take a look at the output and search for your thumbnail images. Then you have to follow the path like it was done with the URL ($entry->children(..) and $media->path->to->thumbnail)
Youtube has updated their embed methods and api output, so I took the opportunity to update the script. You can probably just move all of the settings into the core part of the script, but I figured I'd pull it out to make it easier to follow. Hope this helps:
//SETTINGS
$channel_name = 'mychannelname';//Be sure to change this to your channel
$count = 8;//# of videos you want to show (MAX = 20)
$em_width = 420;//width of embeded player
$em_height = 315;//height of embeded player
$wrap_class = 'video';//class name for the div wrapper
//The output...
error_reporting(E_ALL);
$feedURL = 'http://gdata.youtube.com/feeds/api/users/'.$channel_name.'/uploads?max-results=20';
$sxml = simplexml_load_file($feedURL);
$i = 1;
foreach ($sxml->entry as $entry) {
$vidUrl = explode("/", $entry->id);
$vidKey = $vidUrl[6];
if ($i <= $count ) :
echo '
<div class="'.$wrap_class.'">
<iframe width="'.$em_width.'" height="'.$em_height.'" src="http://www.youtube.com/embed/'.$vidKey.'" frameborder="0" allowfullscreen></iframe>
</div>
';
endif;
$i++;
}

Categories