1

i need to know if is possible to load an html page and submit the form inside this page using php. so something like:

<?php
   $html = fopen("http://www.mysite.com","r");
   //get position of form...
   ...
   //submit it 
?>

is possible? can someone help me? thanks!!!

EDIT:

i have to submit this form

https://annunci.ebay.it/pubblica-annuncio

my problem is that in this page there is an image upload and i don't know how to do that using php( scraping it )

Jayyrus
  • 12,961
  • 41
  • 132
  • 214
  • Look into [cURL](http://php.net/manual/en/book.curl.php). – Nadh May 05 '12 at 09:42
  • can you explain it little bit more? – arun May 05 '12 at 09:43
  • Upvoted. I always was curious if such thing can be done. But from what I know that's not possible. It doesn't make sense to submit (or click) a button in an external page. Unless you have something like a browser emulator in your php which does a client side job. – xperator May 05 '12 at 09:52
  • 1
    This is possible because you are getting content of required file and manipulate the one later. You are dealing with T_STRING. But it's strongly recommended not to allow PHP use (require/file_get_contents) outside the root of your server as it opens the door for remote file inclusion – Yang May 05 '12 at 09:55
  • @metal_fan, require or executing code from unknown sources are the common door for remote file inclusion. Submiting a from from another webpage doesn't since you aren't executing any code from them. – Lumbendil May 05 '12 at 10:06
  • @Lumbendil in case if another page is in the same host and owned by root – Yang May 05 '12 at 10:06
  • @metal_fan it can be source of trouble, I can see that. But submiting a form to another webpage (which is, in fact, submiting some POST data to an URL) can't include code on your codebase. You should always check the response from the server and mess with it the less possible, but it's like working with a webservice, the only difference being that the response is in HTML instead of XML or JSON. – Lumbendil May 05 '12 at 10:10

5 Answers5

3

You can also use curl to POST to any URL, for instance the form's action url.

$ch = curl_init('http://example.com/form_action.php');
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, array('your' => 'data', 'goes' => 'here');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

$response = curl_exec($ch);

This will call the URL http://example.com/form_action.php as if it was called from a form with 'your' set to value 'data' and 'goes' set to value 'here'.

To find out the URL you need to POST, you can inspect source code. When doing that, check the "name" atribute on the <input> tags you want to send.

EDIT: If the POST url and the fields can change, you should check @Adan's answer.

Community
  • 1
  • 1
Lumbendil
  • 2,906
  • 1
  • 19
  • 24
  • If you need to submit an image it can get a bit tricky with eBay, better check my other answer, since I believe it'll fit more in what you actually need. If you really believe you need to do it this way, check this answer to see [how to post an image using cURL](http://stackoverflow.com/a/3433581/782323) – Lumbendil May 05 '12 at 10:03
  • my problem is that the upload of image is done using jquery so when user clicks on "upload image", it will not reload page and load the image. how can i do it in php? any ideas? – Jayyrus May 05 '12 at 13:14
  • You mean that the image is uploaded to the server by jQuery before the user has submited your form, and when the user wants to submit your form, you want it to be sent to eBay right? After the jQuery upload you should set a hidden form attribute which gives you an ID of the image, to be able to find it in the next page load. – Lumbendil May 06 '12 at 01:22
  • so you say me that i can set value of the input of file and query will upload it to the server, than i save an hidden value to the next page? – Jayyrus May 06 '12 at 08:08
  • Yes. I'll describe the flow of events so we're on the same page: user selects an image => image is uploaded by jquery => on jquery end, the upload returns an id which can locate the image => jquery sets a hidden field in the form the user is filling with the given id => user submits the form => you use curl to submit to eBay, using the ID to find the image you need to send. – Lumbendil May 06 '12 at 16:19
  • 1
    If you need an easy way to observer the requests that jquery is doing in the background, use [the chrome network panel](https://developers.google.com/chrome-developer-tools/docs/network), or the [firebug network panel](http://getfirebug.com/network) – David May 07 '12 at 13:50
1

Basically this is what you need to do

1- Get the content of the HTML page using file_get_contents() (bearing in mind the security risks)
2- Parse the HTML using DOMDocument
3- Get the form's attributes, most importantly (ACTION, METHOD) using DOMDocument
4- Get the form's fields' names using DOMDocument
5- Then send to the ACTION url using the method METHOD a request with the data you want replacing the fields using cURL

Adi
  • 5,089
  • 6
  • 33
  • 47
1

you can use curl for getting page in php. as mentioned in answer @Lumbendil. For parsing the HTML you can use libraries like

http://simplehtmldom.sourceforge.net/

Or you can use

http://code.google.com/p/phpquery/

Ranjith Siji
  • 1,125
  • 3
  • 12
  • 19
0

As another option, which would be more clean, you could use the eBay API. It provides methods to add new items, and it probably has already built libraries for php, such as the PHP Accelerator toolkit for eBay.

Lumbendil
  • 2,906
  • 1
  • 19
  • 24
  • i don't have to do it only for ebay.. but for other sites like subito.it, kijii .. – Jayyrus May 05 '12 at 11:52
  • Still, I'd try to research first if they have APIs before messing with submiting forms, since the parameters for those can change from time to time and without prior notice. Also, if in any moment you need authentification, you'll be pretty much screwed (it's posible, but tedious). – Lumbendil May 06 '12 at 01:23
0

I am providing a code that I got from net to get the contents of a page. After that you can use jquery(maybe) to force the submit function.

$url = "URL OF YOUR PAGE"; // I have tested page from same server
$lines = file( $url );
foreach( $lines as $line_num => $line ) {
    $line = htmlspecialchars( $line );
    $line = str_replace( "&lt;", '<span>&lt;', $line );
    $line = str_replace( "&gt;", '&gt;</span>', $line );
    $line = str_replace( "&lt;!–", '<em>&lt;!–', $line );
    $line = str_replace( "–&gt;", '–&gt;</em>', $line );
    echo "<span class=\"linenumber\">Line <strong>$line_num </strong></span> : " . $line . "<br/>\n";
}

The above code gave me contents from another page on same server. Now you have to find a way around to check if a form exist and then ; force submit that form.

Roger
  • 1,693
  • 1
  • 18
  • 34