claudiogc Posted August 23, 2014 Share Posted August 23, 2014 Hello! I'm newbie in PHP(started 3 days ago), and i have two questions about the same topic, i hope you can answer me. 1- Is there methods in php for interaction with webpages as javascript, as they simulate a user interacting with these pages? For example, the method click () or the getElementsByClassName () for you to find something specific on the page. There are methods that do this in php? 2- To interact with a website using a sript (to click on links or save the url of pictures in an array) i start "JavaScript Scratchpad" Firefox with Shift + f4 and then it runs the script there, is just be on the page i want. If i want to do something similar in php, how can i run this script on my browser or is it done another way? Thanks! Quote Link to comment Share on other sites More sharing options...
CroNiX Posted August 23, 2014 Share Posted August 23, 2014 1) No. PHP is run on the server and all it does is output. Once it sends the output it's done. JS runs in the users browser and can interact with the page. 2) not sure what you mean. It can't click on links, but you could scrape anything out of a page on a different server, like images. You'd grab the webpage using CURL or something, and then parse it grabbing the urls for images, and then requesting and saving the images on the server. Quote Link to comment Share on other sites More sharing options...
cyberRobot Posted August 23, 2014 Share Posted August 23, 2014 1- Is there methods in php for interaction with webpages as javascript, as they simulate a user interacting with these pages? For example, the method click () or the getElementsByClassName () for you to find something specific on the page. There are methods that do this in php? For what it's worth, you could look into using a combination of AJAX and PHP. Quote Link to comment Share on other sites More sharing options...
claudiogc Posted August 24, 2014 Author Share Posted August 24, 2014 Thank you for your answers, but i don't understand one thing. I made a javascript that reads a webpage source code and puts in an array all urls of pictures of this page. Then i asked in other forum(javascript forum) how i could download these pictures. They said to me i could do that using PHP because it was better suited to this task, as JavaScript only has limited access to the local file system. And any server-side language (except maybe Classic ASP) is better suited for downloading files. That's why i asked the question 1, because one way to find what i whant in a webpage sorce code is using getElementByClassName(). But now, i don't know who is right, you or them because they said to me to use PHP for do what i want, but you said is not possible. Actually i want to learn PHP for other things, but i need a motivation and make this "program", that downloads all pictures of a webpage is a good motivation for me. Quote Link to comment Share on other sites More sharing options...
CroNiX Posted August 24, 2014 Share Posted August 24, 2014 Well that's a bit different than your original question where you were asking about simulating interacting with the page...clicking, etc. If you just wanted to get the images from some webpage on a different server (not yours), you could do what I mentioned in my #2 answer. 1) Use simple_html_dom to retrieve the webpage 2) Use simple_html_dom to traverse the webpage and extract the image urls 3) Use CURL to request each of the images from the other server and save them Quote Link to comment Share on other sites More sharing options...
mogosselin Posted August 24, 2014 Share Posted August 24, 2014 I found this function that would download an image, individually: http://edmondscommerce.github.io/php/curl/php-save-images-using-curl.html Or this tutorial that seems to download all images of one page. I didn't try it, but here you go: http://snipplr.com/view/69459/ To find it, I simply searched for 'curl php get images in page' Quote Link to comment Share on other sites More sharing options...
KevinM1 Posted August 24, 2014 Share Posted August 24, 2014 It's important to understand the HTTP request/response lifecycle. User sends a request to a server | V Request is delivered to the proper place (PHP script, something else) based on the structure of the request | V The server processes the request based on the info provided. So, your PHP script will fire, maybe work on form data or some other info, interact with a database if necessary, and send a response | V Response sent back to the user. This will generally be in the form of a completed HTML document in our case, since we're interested in creating/modifying/editing web sites | V JavaScript is run in the browser, which is why it can manipulate rendered HTML directly. It's the last step of the process So, as you can see, PHP and JavaScript can't do the same things because they serve different purposes and are used at different times in the HTTP request/response cycle. Now, you can use JavaScript to send a request to a PHP script that resides on your server, and then dynamically apply the results to the screen without a page refresh. That's what AJAX is, and it's how web apps like just about everything Google does works. But, outside of a sophisticated testing framework that can simulate a web browser, PHP can't directly access a rendered page. By the time a page is rendered, PHP is done working. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.