how to get html source without any error? - php

i want get the html source of a page but give a error!!
i use this code for give:
<?php
header('Content-Type: text/html; charset=utf-8');
$html = file_get_html("http://www.google.com/");
echo $html;
when I want to get the source from here I don't correct response and I get something like these characters
����i�[S$%ٲ�9������

Use the function file_get_contents instead:
http://www.php.net/manual/en/function.file-get-contents.php

you should do like this,note: Download simple_html_dom class here
<?php
include_once('simple_html_dom.php');
$html = file_get_html('http://www.google.co.in');
echo $html;
?>

Related

simple_html_dom trying to parse the site, but the cat doesn't output anything

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.

Warning file_get_contents in DOM Parser

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);

how to echo php file without pars?

how to read a php file and echo it in a html file?
i try to use readfile() , file() file_get_content() to read a php file.
but when i echo file_string its parsed and then show.
how i can prevent to pars stirng var that included php codes.
here my code:
<?php
$path = '..../ex.php';
$source = fopen($path , "r");
echo fread($source,filesize($path ));
fclose($source);
?>
how to echo $source without compiled or parsed.
With this function
<?php
echo htmlspecialchars($text);
?>
php.net/htmlspecialchars
fread should works fine. Remember that when you use echo it prints <?php opening tag and in rendered page it can be not visible.
To test it, just try with var_dump:
$content = fread($source,filesize($path));
var_dump($content);
I'll go out on a limb and guess that the code is not showing up completely in your HTML page, because the browser is trying to interpret <?php as HTML tags. The solution is to HTML encode any text which may contain characters with a special meaning in HTML:
echo htmlspecialchars(file_get_contents('..../ex.php'));
See The Great Escapism (Or: What You Need To Know To Work With Text Within Text).
use this
$path = 'ex.php';
$source = fopen($path , "r");
echo "<textarea style='border:0px; overflow: hidden; width:100%; height:100% '>";
echo fread($source,filesize($path ));
echo "</textarea>";
fclose($source);

How to get page title in php?

I have this function to get title of a website:
function getTitle($Url){
$str = file_get_contents($Url);
if(strlen($str)>0){
preg_match("/\<title\>(.*)\<\/title\>/",$str,$title);
return $title[1];
}
}
However, this function make my page took too much time to response. Someone tell me to get title by request header of the website only, which won't read the whole file, but I don't know how. Can anyone please tell me which code and function i should use to do this? Thank you very much.
Using regex is not a good idea for HTML, use the DOM Parser instead
$html = new simple_html_dom();
$html->load_file('****'); //put url or filename
$title = $html->find('title');
echo $title->plaintext;
or
// Create DOM from URL or file
$html = file_get_html('*****');
// Find all images
foreach($html->find('title') as $element)
echo $element->src . '<br>';
Good read
RegEx match open tags except XHTML self-contained tags
Use jQuery Instead to get Title of your page
$(document).ready(function() {
alert($("title").text());
});​
Demo : http://jsfiddle.net/WQNT8/1/
try this will work surely
include_once 'simple_html_dom.php';
$oHtml = str_get_html($url);
$Title = array_shift($oHtml->find('title'))->innertext;
$Description = array_shift($oHtml->find("meta[name='description']"))->content;
$keywords = array_shift($oHtml->find("meta[name='keywords']"))->content;
echo $title;
echo $Description;
echo $keywords;

php request youtube video is not working

what is wrong to this code?
header('Content-type: application/x-shockwave-flash');
$video_id = $_REQUEST['id'];
$content = readfile("http://www.youtube.com/watch?v=$video_id");
echo $content;
Why this code is not working? How should look the code?
i've edited like this:
header('Content-type: application/x-shockwave-flash');
$video_id = $_REQUEST['id'];
$content = readfile("http://www.youtube.com/v/$video_id");
echo $content;
and if i get http://localhost/media.php?id=pkyRRD9f0ts in browser it works but if i add it in jwplayer it doesn't work :(
That's because you're pulling the entire webpage as you would view it in a browser, not just the video file (youtube specifically doesn't allow that).
Try echoing an embed html tag instead.
echo <embed src="http://www.youtube.com/watch?v=$video_id">;

Categories