Michael
Michael

Reputation: 307

Posting to a URL with PHP and no form

I'd like to use PHP to post to a URL the way a form would. Basically the same function that this form would do

    <form method="POST" id="foo" action="<?php echo $url; ?>">
            <input type="hidden" name="shopId" value="<?php echo $ID; ?>">
            <input type="hidden" name="encodedMessage" value="<?php echo $encodedMessage; ?>">
            <input type="hidden" name="signature" value="<?php echo $signature; ?>">
            <input type="submit" value="Submit" name="buy">
    </form></div>

How can I do this?

Upvotes: 1

Views: 193

Answers (2)

mmalone
mmalone

Reputation: 1132

There's several ways to do this, but the easiest is probably with file_get_contents() and stream_context_create(). See this file_get_contents() comment for an example.

Upvotes: 0

Dan Simon
Dan Simon

Reputation: 13137

you want to use cURL functions to send an http POST request. For example:

$ch = curl_init($url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, "name1=val&name2=val2...");
curl_exec($ch);
curl_close($ch);

Upvotes: 3

Related Questions