I'm using the phpQuery library (http://code.google.com/p/phpquery/) to parse web pages but have stumbled across a problem getting sites that use Ajax to display all the content.
I have worked out that I can get all the content if I load it in to an iframe (the code below works):
$temp = phpQuery::newDocumentHTML('<iframe src="" id="test">a</iframe>')->find('iframe[id=test]')->attr('src', 'http://www.example.com/');
echo $temp;
BUT, my question is, how can I get my PHP script to wait until the iframe has loaded before proceeding?
Below is the jQuery equivalent but I was wondering if anybody knows how to do the equivalent using phpQuery?
$(iFrame).attr('src', 'http://www.example.com');
$(iFrame).load(function(){
alert("Loaded");
});
Thanks in advance.
BUT, my question is, how can I get my PHP script to wait until the iframe has loaded before proceeding?
This is not how PHP-side HTML parsing works. phpQuery just parses the HTML code, it doesn't do anything with it - like load and/or render iframes, or run JavaScript events.
There is probably a way to do what you want to do - if you tell us what that is!
Related
I'm using headless-chromium-php, but getHtml function seems to get the html source code.
https://github.com/chrome-php/chrome#get-the-page-html
Instead, I want to get the DOM displayed in the chrome browser.
so, How can i do it
I want to get the html source after browser rendering.
As you surmise, you need to wait for the page to finish loading, including any javascript rendering; have a look at the example earlier on in that documentation
[https://github.com/chrome-php/chrome#evaluate-script-on-the-page] to get the inner html.
require('simple_html_dom.php');
// Create DOM from URL or file
$html = file_get_html('https://www7.fmovies.se/film/hometown-hero.m2r28/6xpjrp');
foreach($html->find('div[id=player]') as $div)
{
foreach($div->find('iframe') as $iframe)
{
echo $iframe->src;
}
}
This is my code and as you see I'm trying to get the src of the iframe under the player div using PHP Simple HTML DOM Parser, can you explain to me why I'm getting a blank page as a result?
Thanks!
UPDATE: After using a javascript switcher addon and disabling javascript, I noticed that the iframe I'm looking for is not loaded. What should I do to get the iframe src?
There are two possible solutions;
Try to figure out how the javascript works, and mock this behavior in your PHP script.
Let the page load in, for example, selenium and then grab the SRC from the iframe using selenium. (https://www.seleniumhq.org/)
Hope this helps
I'm create and loading multiple iframes with basic src. But, I have noticed that for each now iframes I create and the src being added with new url for each, the browers locks a bit. So I thought, can I add the page though Ajax instead, using php proxy on the server. I have managed to do this in some extent, but all data does not load. For example, in one specific case a flexslider does not load.
I use
PHP Using phpQuery:
$html = file_get_contents('http://' . $myitem['Web_address']);
$doc = phpQuery::newDocumentHTML($html);
$doc->find('head')->prepend('<base href="'.'http://' . $myitem['Web_address'].'">');
JQUERY: (Note, the iframe is already prepaired with a loader.html document)
$('iframe').contents().find('html').html(data.comp[0]['html']);
A lot does load, but some images do not, can I load ALL content, and how can I acheive this.
Thanks!
So my school has this very annoying way to view my rooster.
you have to bypass 5 links to get to my rooster.
this is the link for my class (it updates weekly without changing the link)
https://webuntis.a12.nl/WebUntis/?school=roc%20a12#Timetable?type=1&departmentId=0&id=2147
i want to display the content from that page on my website but with my
own stylesheet.
i don't mean this:
<?php
$homepage = file_get_contents('http://www.example.com/');
echo $homepage;
?>
or an iframe....
I think this can be better done using jquery and ajax. You can get jquery to load the target page, use selectors to strip out what you need, then attach it to your document tree. You should then be able to style it anyway you like.
I would recommend you to use the cURL library: http://www.php.net/manual/en/curl.examples.php
But you have to extract part of the page you want to display, because you will get the whole HTML document.
You'd probably read the whole page into a string variable (using file_get_contents like you mentioned for example) and parse the content, here you have some possibilities:
Regular expressions
Walking the DOM tree (eg. using PHPs DOMDocument classes)
After that, you'd most likely replace all the style="..." or class="..." information with your own.
I've been working on this for about four hours and have been all over the internet trying to understand it, so please be gentle.
I'd like to display a div from an external source on my php page. I've tried usingfile_get_dom, simplexml_load_file, file_get_contents with preg_match_all, then printed them on my page, but they don't work. cURLing is over my head from what I have seen and can't understand any of it, but I've been told it is the best way to do it. They all result in various errors when all I want is to grab the contents of an external div. What should I do?
An example would be scraping the div id='hmenus' on this page, then displaying it on my local page.
Thanks!
If cURL is over your head then perhaps try Simple HTML DOM
$html = file_get_html($url);
echo $html->find('div[id=hmenus]', 0);