I have managed to create a graph using JpGraph, I want to now take it a step further and embed this graph in my template/view php file.
So I put the graph code into a seperate php file to then embed however, when I try to embed it via
<embed src="templates/ctl008/graphshow.php" height=80em; width=160em;> </embed> it leaves me with a requested URL not found error. I have fiddled with the path and tried just: graphshow.php, ctl008/graphshow.php and other such variations. Is it my path that is not correct or is there something else I am missing? I have also tried to embed it with <img src="graphshow.php" /> . A funny quirk is when I turn on ini_set('display_errors', 1); error_reporting(E_ALL); and try to view the graph on its own I am left with an error message: JpGraph Error: HTTP headers have already been sent. Caused by output from file gd_image.inc.php at line 92. but if i turn off the error_reporting it displays the graph as expected. I would appreciate any pointers in the right direction with this :)
Here is my graphshow.php file:
<?php require_once ('../library/include/jpgraph/src/jpgraph.php');
require_once ('../library/include/jpgraph/src/jpgraph_line.php');
require_once ('../library/include/jpgraph/src/jpgraph_bar.php');
ini_set('display_errors', 1); ini_set('display_startup_errors', 1); error_reporting(E_ALL);
$gasArray = array();
foreach($searchArray as $gArray){
$gasArray[] = $gArray['GASVB'];
}
$stromArray = array();
foreach($searchArray as $sArray){
$stromArray[] = $sArray['STROVB'];
}
$olArray = array();
foreach($searchArray as $oArray){
$olArray[] = $oArray['OLVB'];
}
if(!empty($gasArray) || !empty($stromArray) || !empty($olArray)){
ob_end_clean();
$width= 1200;
$height = 400;
$graph = new Graph($width, $height);
$graph->SetScale('intint');
$graph->title->Set('Energie Verbrauch');
$graph->SetMargin(80,25,30,80);
$graph->xaxis->title->Set('Datum');
$graph->yaxis->title->Set('Verbrauch');
// print_r($dateArray);
$lineGas = new LinePlot($gasArray);
$lineGas->SetLegend('Gas');
$lineStrom = new LinePlot($stromArray);
$lineStrom->SetLegend('Strom');
$lineOl = new LinePlot($olArray);
$lineOl->SetLegend('ÖL');
$graph->Add($lineGas);
$graph->Add($lineStrom);
$graph->Add($lineOl);
$graph->Stroke();
}
?>
I'd like to start off with a few helpful links:
JpGraph Error: HTTP headers have already been sent
http://www.digialliance.com/docs/jpgraph/html/2210intro.html
https://coursesweb.net/php-mysql/jpgraph-graph-charts-plots-php
https://jpgraph.net/download/manuals/chunkhtml/ch05s05.html
now that that is out of the way, how I managed to get it working:
Starting with the advice/help in the above StackOverflow link I turned my seperate php file into a function the final line of my graphshow.php function was this: $graph->Stroke("charts/graphEnergy.png"); where charts is the folder I am saving in and graphEnergy.png is the name of the file being saved.
then in my view/template page I require_once('chart/ctl008/graphshow.php'); made my php file accessible, called my function like so generateGraph($gasArray, $stromArray, $olArray); and then in the html section embedded the graph with <img src="./charts/graphEnergy.png" /> What really tripped me up was needing to call the graph.png with the ./ before the charts/graphEnergy.php, and then I was able to display my bootiful graph on my view/template page. I hope this helps someone in the future :)
BoogaBooga
Related
My case is, I want to scrap a website, which is success, and I'm using PHP cURL. The problem start when I want to use the DOM Parser to get the content I want. Here is the warning came out:
the error image is here
And the code I use is here. Before this code, I scrap a website using cURL, it's working, but just this part got error :
include 'simple_html_dom.php';
//Here is where I scraping, no need to show it
$fp = fopen(dirname(__FILE__) . '/airpaz.html', 'w');
//$html contain the page I scrap
fwrite($fp, $html);
fclose($fp);
$html_content = file_get_contents(dirname(__FILE__) . '/airpaz.html');
echo $html_content;
$html2 = new simple_html_dom();
$html2->load_file($html_content);
Hope you guys can help, thanks
It looks like you are trying to read a file 3 times:
$read_file = fread($fr, filesize(dirname(__FILE__) . '/airpaz.html'));
and:
$html_content = file_get_contents($read_file);
and:
$html2->load_file($html_content);
In the last two instances, instead of a file-name you pass html contents to the function so that will not work.
You should read the file only once and use string functions on the contents you receive. Or you open the url directly in $html2->load_file().
try this code
include 'simple_html_dom.php';
$html_content = file_get_html(dirname(__FILE__) . '/airpaz.html');
echo $html_content;
$html2 = new simple_html_dom();
$html2->load_file($html_content);
I´m parsing some itunes links with dom parser in php. With most of the links it works perfectly. Others which are totally the same type it doesn`t?! I need the "img" tag and the "src-swap-high-dpi" attribute. It drives me nuts. That´s a part of my php-code
$url = "https://itunes.apple.com/us/podcast/id278981407";
$htmlContent = str_get_html(file_get_contents($url));
foreach ($htmlContent->find("img") as $element) {
$value = $element->getAttribute("src-swap-high-dpi");
echo $value;
}
So e.g. I can parse the following links:
https://itunes.apple.com/us/podcast/id201671138
https://itunes.apple.com/us/podcast/id523121474
https://itunes.apple.com/us/podcast/id152249110
But this e.g. not:
https://itunes.apple.com/us/podcast/id278981407
I do not get any output.
Edit:
New Code doesnt work as well:
Still not working for me. Very strange. Thats my new complete code now:
<?php
ini_set("display_errors",1); error_reporting(E_ALL);
require_once ('simple_html_dom.php');
$url = "https://itunes.apple.com/us/podcast/id278981407";
$htmlContent = str_get_html(file_get_contents($url));
foreach($htmlContent->find("div.artwork") as $div) {
$value = $div->find("img",0)->getAttribute("src-swap-high-dpi");
echo $value."<br/>";
}
?>
I get the Output:
Fatal error: Call to a member function find() on a non-object in /home/www/whatever/delete.php on line 10
line 10 is the line starting with "foreach". Your code works fine with the links provided above which I declared as working. But as soon as I take one of the designated one which doesnt work I get the error message provided above. ?!
I think this is one of the cases Simple DOM gets a bit confused and you need to provide it with a parent:
$url = "https://itunes.apple.com/us/podcast/id278981407";
$htmlContent = str_get_html(file_get_contents($url));
foreach($htmlContent->find("div.artwork") as $div) {
$value = $div->find("img",0)->getAttribute("src-swap-high-dpi");
echo $value."<br/>";
}
UPDATE
Here are the results using the above fragment:
http://a3.mzstatic.com/us/r30/Podcasts/v4/61/cc/7f/61cc7f25-131f-7616-6549-5553e6444b87/mza_7489225285918350214.150x150-75.jpg
http://a2.mzstatic.com/us/r30/Podcasts6/v4/04/a9/64/04a964d7-7c10-72d6-871b-97619cf89066/mza_1416781107029663068.150x150-75.jpg
http://a5.mzstatic.com/us/r30/Podcasts4/v4/bb/a6/f4/bba6f4b6-eeab-d7d9-8591-adb2bd277ccb/mza_5223368352447971673.150x150-75.jpg
http://a1.mzstatic.com/us/r30/Podcasts5/v4/aa/54/16/aa541600-cc8b-772b-9c0a-824efe8fdc42/mza_6772270613386652594.150x150-75.jpg
http://a2.mzstatic.com/us/r30/Podcasts3/v4/95/3d/2f/953d2f75-c2c2-4815-a752-f30fdcc0b9fb/mza_9037746738018570312.150x150-75.jpg
http://a4.mzstatic.com/us/r30/Podcasts4/v4/a2/1c/f5/a21cf5a4-2d8d-1ed7-983f-1c90f2f4f948/mza_7120473049241631392.340x340-75.jpg
http://a2.mzstatic.com/us/r30/Podcasts4/v4/5d/21/8d/5d218d2a-2980-0ac9-0bc7-9321ea6eb334/mza_6358466742996313573.150x150-75.jpg
http://a1.mzstatic.com/us/r30/Podcasts/b2/bb/bf/ps.ykmejwzs.150x150-75.jpg
http://a4.mzstatic.com/us/r30/Podcasts6/v4/17/ea/31/17ea3187-ef8c-4756-e488-0c65adced988/mza_7931750363714403933.150x150-75.jpg
http://a1.mzstatic.com/us/r30/Podcasts2/v4/0b/3c/7d/0b3c7d2b-19bf-f7a2-7c50-ca15338b8316/mza_2792239161425784587.150x150-75.jpg
Can you verify you're not getting errors at all ? Say, just write some weird characters in your PHP file, does the PHP shows the error? If not, try to add this in your .htaccess file.
<IfModule mod_php5.c>
# do not display errors
php_value display_errors 1
</IfModule>
UPDATE 2
$url = "https://itunes.apple.com/us/podcast/id278981407";
$ch = curl_init();
curl_setopt($ch,CURLOPT_URL,$url);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,FALSE);
$html = curl_exec($ch);
curl_close($ch);
//$htmlContent = str_get_html(file_get_contents($url));
$htmlContent = str_get_html($html);
foreach($htmlContent->find("div.artwork") as $div) {
$value = $div->find("img",0)->getAttribute("src-swap-high-dpi");
echo $value."<br/>";
}
The reason i didn't use file_get_html of Simple Dom is because it simply uses file_get_contents internally.
I have been working with this php code, which should modify Google Calendars layout. But when I put the code to page, it makes everything below it disappear. What's wrong with it?
<?php
$your_google_calendar=" PAGE ";
$url= parse_url($your_google_calendar);
$google_domain = $url['scheme'].'://'.$url['host'].dirname($url['path']).'/';
// Load and parse Google's raw calendar
$dom = new DOMDocument;
$dom->loadHTMLfile($your_google_calendar);
// Change Google's CSS file to use absolute URLs (assumes there's only one element)
$css = $dom->getElementByTagName('link')->item(0);
$css_href = $css->getAttributes('href');
$css->setAttributes('href', $google_domain . $css_href);
// Change Google's JS file to use absolute URLs
$scripts = $dom->getElementByTagName('script')->item(0);
foreach ($scripts as $script) {
$js_src = $script->getAttributes('src');
if ($js_src) { $script->setAttributes('src', $google_domain . $js_src); }
}
// Create a link to a new CSS file called custom_calendar.css
$element = $dom->createElement('link');
$element->setAttribute('type', 'text/css');
$element->setAttribute('rel', 'stylesheet');
$element->setAttribute('href', 'custom_calendar.css');
// Append this link at the end of the element
$head = $dom->getElementByTagName('head')->item(0);
$head->appendChild($element);
// Export the HTML
echo $dom->saveHTML();
?>
When I'm testing your code, I'm getting some errors because of wrong method call:
->getElementByTagName should be ->getElementsByTagName with s on Element
and
->setAttributes and ->getAttributes should be ->setAttribute and ->getAttribute without s at end.
I'm guessing that you don't have any error_reporting on, and because of that don't know anything went wrong?
I am trying to pull area of a page with AJAX.
In JS I have on click I pass href to PHP;
in PHP(located in tools):
<?php defined('C5_EXECUTE') or die("Access Denied.");
$path = ($_POST['path']);
$page = Page::getByPath($path);
$a = new Area('Main');
$ret = $a->display($page);
echo json_encode($ret);
?>
If I make:
echo json_encode($page);
I receive the page so everything working, But when I try to receive an Area I get this error:
concrete\elements\block_area_header_view.php on line 5
In this File I found this
$c = Page::getCurrentPage();
$areaStyle = $c->getAreaCustomStyleRule($a);
So as I understand $c is null that why I have this error how can I fix this??
This line of code:
$ret = $a->display($page);
...does not do what you think it does. The "display" function does not return the content -- instead it outputs it to the browser. So your json_encode($ret) is just encoding and echo'ing an empty variable.
To capture the displayed content and put it into a variable, you can use php's output buffering feature, like so:
ob_start();
$a->display($page);
$ret = ob_end_clean();
Suppose you have a thumbnail generator script that accepts source images in the form of a URL. Is there a way to detect if the source URL is "broken" - whether nonexistent or leads to an non-image file?
Just brute force using getimagesize() or another PHP GD function is not a solution, since spoofed stray URL's that might not be images at all (http://example.com/malicious.exe or the same file, but renamed as http://example.com/malicious.jpg) could be input - such cases could easily be detected by PHP before having to invoke GD. I'm looking for GD pre-sanitizing before having GD try its battalion at parsing the file.
as a first step, the following regular expression checks if the URL is an image extension:
preg_match('#(https?://([-\w\.]+)+(:\d+)?(/([\w/_\.]*(\?\S+)?)?)?)([^\s]+(\.(?i)(jpg|png|gif|bmp))$)#', $txt,$url);
use file_exists function in php, you can check urls with it.
See documentation below, shows how to check img... exactly what you need
FILE EXISTS - http://www.php.net/manual/en/function.file-exists.php#93572
URL EXISTS - http://www.php.net/manual/en/function.file-exists.php#85246
Here is alternative code for checking the url. If you will test in browser replace \n with <br/>
<?php
$urls = array('http://www.google.com/images/logos/ps_logo2.png', 'http://www.google.com/images/logos/ps_logo2_not_exists.png');
foreach($urls as $url){
echo "$url - ";
echo url_exists($url) ? "Exists" : 'Not Exists';
echo "\n\n";
}
function url_exists($url) {
$hdrs = #get_headers($url);
echo #$hdrs[1]."\n";
return is_array($hdrs) ? preg_match('/^HTTP\\/\\d+\\.\\d+\\s+2\\d\\d\\s+.*$/',$hdrs[0]) : false;
}
?>
Output is as follows
http://www.google.com/images/logos/ps_logo2.png - Content-Type: image/png
Exists
http://www.google.com/images/logos/ps_logo2_not_exists.png - Content-Type: text/html; charset=UTF-8
Not Exists
I have used the following to detect attributes for remote images
$src='http://example.com/image.jpg';
list($width, $height, $type, $attr) = #getimagesize($src);
example (checking stackoverflows "Careers 2.0" image)
$src='http://sstatic.net/ads/img/careers2-ad-header-so.png';
list($width, $height, $type, $attr) = #getimagesize($src);
echo '<pre>';
echo $width.'<br>';
echo $height.'<br>';
echo $type.'<br>';
echo $attr.'<br>';
echo '</pre>';
If $height, $width etc is null the image is obvious not an image or the file does not exists. Using cURL is overkill and slower (even with CURLOPT_HEADER)
The only really reliable way is to request the image using file_get_contents(), and finding out its image type using getimagesize().
Only if getimagesize() returns a valid file type, can you rely that it is in fact a valid image.
This is quite resource heavy, though.
You could consider not doing any server-side checks at all, and adding an onerror JavaScript event to the finished image resource:
<img src="..." onerror="this.style.display = 'none'">
try for local files
<?php
if(file_exists($filename))
{
//do what you want
}
else
{
//give error that file does not exists
}
?>
for external domains
$headers = #get_headers($url);
if (preg_match("|200|", $headers[0])) {
// file exists
} else {
// file doesn't exist
}
Also you can use curl request for the same.
Fast Solution for broken or not found images link
i suggest you that don't use getimagesize() because it will 1st download image then it will check images size+if this will not image then it will throw exception so use below code
if(checkRemoteFile($imgurl))
{
//found url, its mean
echo "this is image";
}
function checkRemoteFile($url)
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$url);
// don't download content
curl_setopt($ch, CURLOPT_NOBODY, 1);
curl_setopt($ch, CURLOPT_FAILONERROR, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
if(curl_exec($ch)!==FALSE)
{
return true;
}
else
{
return false;
}
}
Note:
this current code help you to identify broken or not found url image this will not help you to identify image type or headers
You could check the HTTP status code (it should be 200) and the Content-type header (image/png etc.) of the HTTP response before you put the actual image through the generator.
If these two preconditions are ok, after retrieving the image you can call getimagesize() on it and see if it breaks, what MIME type it returns etc.
did you try file_get_contents() method?
http://php.net/manual/en/function.file-get-contents.php
onerror Event
Execute a JavaScript if an error occurs when loading an image:
The onerror event is triggered if an error occurs while loading an external file (e.g. a document or an image).
Example:
<!DOCTYPE html>
<html>
<body>
<img src="image.gif" onerror="myFunction()">
<p>A function is triggered if an error occurs when loading the image. The function shows an alert box with a text.
In this example we refer to an image that does not exist, therefore the onerror event occurs.</p>
<script>
function myFunction() {
alert('The image could not be loaded.');
}
</script>
</body>
</html>
Although not detecting broken links, might be useful for someone else...
onerror=""
<img src="PATH" onerror="this.src='NEW PATH'" />