1

I am trying to click on href attribute Here is my sample html

<ul>
  <li>
     <a href='http://www.google.com'>Google</a>
  </li>
</ul>

And Here is my php code

<?php 
  require_once('classes/simple_html_dom.php');
  $html = file_get_html("/var/www/html/dk/PHP_SCRAPPING/google.html");
  echo $html->find("ul li a",0)->href;
?>

Output is

http://www.google.com

I just want to click on this url. How to do that? Please don't tell me to do this

file_get_html($html->find("ul li a",0)->href);

I am looking for method which can click on any href by using simple html dom.

10
  • 1
    PHP cannot "click", maybe you want to header("Location:$url"); to redirect the user to that url. Commented Nov 12, 2013 at 13:13
  • You can't click an URL with PHP on the server side. You may request the containing link. This is what you already have done. Also, the file_get* functions have restrictions on many servers and you maybe should use cURL instead. Commented Nov 12, 2013 at 13:14
  • It can't be done this way. By the way, what's the objective of this script? To click on ads automatically? I don't see any good purpose of this indeed Commented Nov 12, 2013 at 13:17
  • I worked on Ruby also. And they use mechanize class. And that have click method. So I was also expecting this method in simple html dom parser. Commented Nov 12, 2013 at 13:19
  • Ok e.g.search.yahoo.com just query anything and click on search button. After getting DOM click on next button till all pages are crawled. Commented Nov 12, 2013 at 13:23

6 Answers 6

3

You can't do that in PHP since all PHP code is executed server side and not client side. The PHP code doesn't run in a browser at all, so in essence there is no link to click.

If you want to script things client side, you'd need to resort to javascript. Although any sane browser won't let you emulate a click in Javascript for security reasons.

I think you need to read up on what exactly you're doing.

Sign up to request clarification or add additional context in comments.

Comments

1

Have a look at Selenium with Python and Chrome/Firefox/PhantomJS Webdriver , that gives you complete control over clicking any button or link in any webpage .. parse it using BeautifulSoup and may be create your our json from Python

Comments

0

If i am not getting you wrong you can do it with 2 option

  1. PHP -- header("Location:http://www.google.com");

  2. Javascript-- window.location("http://www.google.com");

1 Comment

Ruby Mechanize class having click method. I want to do same thing in php using simple html dom parser or any other parser?
0

Try

echo $html->find("ul li a",0);

If that doesn't works you can do:

echo "<a href='".$html->find("ul li a",0)->href."'>";
echo $html->find("ul li a",0)->plaintext."</a>";

Comments

0

In this API they have a click method

Comments

-1

I would use jQuery, I'm pretty sure you could do something like this.

<head>
<script>      $("#clickable").click();         </script> 
</head>

<body> <?php  echo "<span id='clickable'>" . $html->find("ul li a",0)->href ."</span>";  ?>   </body>

Obviously you would need at add in the jquery library link and (doc.)ready... I haven't tested it but I don't see any reason why jQuery wouldn't click on a server side generated link

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.