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.

Rakesh Bitling
  • 29
  • 1
  • 10
  • 1
    PHP cannot "click", maybe you want to `header("Location:$url");` to redirect the user to that url. – steven Nov 12 '13 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](http://us3.php.net/curl) instead. – feeela Nov 12 '13 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 – Fallen Nov 12 '13 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. – Rakesh Bitling Nov 12 '13 at 13:19
  • Ok e.g.http://search.yahoo.com/ just query anything and click on search button. After getting DOM click on next button till all pages are crawled. – Rakesh Bitling Nov 12 '13 at 13:23
  • http://stackoverflow.com/questions/819182/how-do-i-get-the-html-code-of-a-web-page-in-php – steven Nov 12 '13 at 13:28
  • I guess what you're looking for is [this.](http://www.simpletest.org/en/browser_documentation.html) – Ben Fortune Nov 12 '13 at 13:34
  • I don't see the problem here. _Please don't tell me to do this_ `file_get_html($html->find("ul li a",0)->href);` means you are retrieving the content of that particular `href` value. That doesn't make anything _clickable_. If it is supposed to be displayed in `html` then why not use the complete `a` tag or build a new one with the value of `href`? – dbf Nov 12 '13 at 13:48
  • how to submit a form using simple html dom ? – Rakesh Bitling Nov 12 '13 at 14:14
  • Did you see this: http://stackoverflow.com/questions/199045/is-there-a-php-equivalent-of-perls-wwwmechanize – Enissay Nov 13 '13 at 16:07

6 Answers6

2

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.

Timothy Groote
  • 8,614
  • 26
  • 52
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

UberNeo
  • 1,298
  • 2
  • 14
  • 16
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");

Veerendra
  • 2,562
  • 2
  • 22
  • 39
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>";
Cassa
  • 1
0

In this API they have a click method

tarek fellah
  • 365
  • 1
  • 10
  • 32
-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