Parse DOM in php with CURL - php

When i use Firebug or chrome inspector on this page http://www.facebook.com/GaryFromCooper?sk=wall (right click inspect element) i could see an hidden input named "link_data".
But when i curl it with php and read the HTML file there is no hidden input...
So i guess this might be in the DOM.
But i couldn't found any way to read the DOM after my curl request, i tried DOM Php function but this doesn't work...
Can someone help me ?
I just want to retrieve the "link_data" value from the http://www.facebook.com/GaryFromCooper?sk=wall page...using curl
Thanks for your help

It's probably inserted with JavaScript. cURL is just a tool for transfering data, not executing JavaScript :P
Considering this involves Facebook, there's probably a really good reason why you can't just 'scrape' that value.

Your better of using the Facebook API to get the data that you need, if anything changes from Facebooks part you wont be affected.
http://developers.facebook.com/

It might be a DOM node inserted by JS. See this curl FAQ. Curl doesn't support JavaScript.

Related

Get response from ajax and parse the response with html on webpage

I need to run a search using Ajax, such that the response I get from Ajax (should not contain HTML, it should only contain data) is fetched on webpage and then parse that response with HTML on the page and display.
I want to know can it be done, if yes then how to do it. Also is it going to make process run faster or consume less resources on server?
Since you have made no attempt to try to code this, I will give you a couple pointers.
1.) It is very possible, I do it on login forms.
2.) Post data to a external page, then encode the response on that page to an array in JSON. echo out the JSON on the external page.
3.) After your ajax post is finished, you can carry out a function similar to this:
function(data){alert(data.given_name_on_external_page)}
or something similar. Once you google around for Ajax form examples you should be able to grasp a little better.
4.) Now for displaying this on a web page, it is fairly easy.
HTML
<div id='response'></div>
Javascript
function(data){document.GetElementById('response').html=data.data};
That should be enough for you to understand what needs to be done, I will leave the rest to you and your ability to use google :).
use escape and load() functionality in JQUERY

How to access a variable in PHP

I am a real newbie at PHP and have, I hope, a simple question.
I have found a variable with Firebug in the DOM section and would like to access it to determine if it is true or false. The variable is called isResponsive and it is found under the DOM section of Firebug. I would post a screen capture but the forum won't let me.
In firebug it says:
window > responsiveDesign and then below it lists all of the variables. One of them is call isResponsive and in the column to the right it says either true or false.
Any help in how to access this variable is very much appreciated!!
Thanks in advance,
Doug
You can't, at least not directly.
PHP runs on the server and generates some (mostly) text which it sends to the browser.
The browser parses that text as HTML, JavaScript, CSS, etc and constructs a DOM from it.
The DOM variables exist in the browser, not on the server where PHP runs.
For PHP to get access to them, you would have to use JavaScript (or something else that runs client side and can access the DOM) to serialise the data to strings, and then send them to the server in an HTTP request.
You can't Access DOM vars in php - they are only available over JavaScript. If you want this value the only way is to pass the var via JavaScript to a php script

How to output DOM after all javascript loaded?

As title, my question is how to output (lets say save as a text file on server computer or pass the result to some other php function using ajax) all DOM content on a page?
I did some homework, I tried curl can just output all DOM content using "curl http://google.ca > dom.txt"
However, the this approach will not save contents that Javascript generated, in other words, the javascript code will not run.
Another approach is to embed some javascript code into a page and let the page load the website we want to output, then use the javascript code to save all DOM file after everything is loaded.
I am not sure if phantom.js can do such job, if yes, then how to?
Any body can give a detailed answer on how to achieve this?
I am open to any solutions, this program will run on my server to provide service.
Thank you in advance.
Why not:
jQuery(document).ready(function($) {
$.post(
'/your_filename.php',
'html='+$("html").html(),
function(response){
alert(response);
}
);
});
You can get the contents of the HTML element (including both head and body) using document.documentElement.innerHTML. If you need everything, you can concatenate document.doctype with document.documentElement.outerHTML.
Note that outerHTML isn't quite cross-browser (it works in IE and Chrome, but not Firefox) - for a way to simulate outerHTML for Firefox, see this question: How do I do OuterHTML in firefox?
Javascript is a client side language, so running it on a Server is going to require specialized technology. PHP actually has the ability to work with DOM stuff, as it can build and modify dom elements before transmitting to the client, read more about that here.
I'm not really sure what you are trying to accomplish by doing this, but it sounds like you are trying too hard: you are sending code to the client so that the client can turn around and send code back to the server so that the server can save it as a file? Although if that is what you need to do, follow Brilliand's and iambriansreed's advice to scoop up dom elements with Javascript/jQuery.

How can I manipulate DOM using PHP?

I saw an article here: http://code.lancepollard.com/automatically-publish-posts-to-stumbleupon-with-ruby
I don't know Ruby, but the following lines are pretty self explanatory:
page = agent.get("http://www.stumbleupon.com/submit?url=#{url}&title=#{title}")
form = page.forms.first
form.radiobuttons_with(:name => "sfw").first.check
page = agent.submit(form)
I'm guessing Ruby can fetch the webpage, check a checkbox, then submit the form. Is that possible using PHP?
The Ruby code you referenced actually uses a third party library called Mechanize.
Something similar for PHP is The SimpleTest Scriptable Browser. It's not as feature rich as Mechanize but can get the job done and it can be used independently of the SimpleTest framework.
You probably would want something like:
http://simplehtmldom.sourceforge.net/
PHP's internal support is sufficient, but would be more cumbersome to use than a third party library.
Not out of the box. Possibly there is a third-party library that can do it for you. One that might help is PHPQuery to loop over a fetched page and select the form and its values. The submit then would have to be done using Curl or the like...!
More info:
fetching a page: http://php.net/manual/en/function.file-get-contents.php
JQuery for PHP: http://code.google.com/p/phpquery/wiki/Basics for a basic intro
Submitting a form with Curl: http://davidwalsh.name/execute-http-post-php-curl

php and server side javascript

I am dealing with a problem where I need to do few thing at the SERVER SIDE using JAVASCRIPT (I am using php + apache combination )-
read source of url using curl
run it through some server side JavaScript and get DOM out of it
traverse and parse the DOM using pre-existing java script code.This code works fine in a browser.
I goggled and found http://pecl.php.net/package/spidermonkey , which allows us to run java script at server.is there any better way to achieve this? can we use Mozilla engine to get DOM out of HTML source code and process it using java script ?
Thanks in advance
You can check Jaxer.org, where you tell your javascript where to run.
alt text http://jaxer.org/images//Picture+4_0.png
hope it helps, Sinan.
PHP contains a DOM parser - I would recommend using this to achieve the same results, rather than using server-side Javascript.
You might want to use something else than Javascript, but if you really need this, you can run firefox under Xvfb and remote connect to it from php. It's not exactly trivial to set up, but it's possible.
You might want to try with something like SimpleBrowser instead.
You might want to try installing GromJS. But the success depends on complexity of your JS code. As far as I see, GromJS does not have DOM :(
A lot more complex project, Narwhal does have DOM and a lot more.
For more information, refer to Mozilla hub about ServerJS.

Categories