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 )

7
  • Look into cURL. Commented May 5, 2012 at 9:42
  • can you explain it little bit more? Commented May 5, 2012 at 9: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. Commented May 5, 2012 at 9: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 Commented May 5, 2012 at 9: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. Commented May 5, 2012 at 10:06

5 Answers 5

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.

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

6 Comments

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
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?
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.
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?
If you need an easy way to observer the requests that jquery is doing in the background, use the chrome network panel, or the firebug network panel
|
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

Comments

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/

Comments

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.

2 Comments

i don't have to do it only for ebay.. but for other sites like subito.it, kijii ..
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).
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.

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.