Viewed   119 times

How can I send POST data to a URL in PHP (without a form)?

I'm going to use it for sending a variable to complete and submit a form.



If you're looking to post data to a URL from PHP code itself (without using an html form) it can be done with curl. It will look like this:

$url = '';
$myvars = 'myvar1=' . $myvar1 . '&myvar2=' . $myvar2;

$ch = curl_init( $url );
curl_setopt( $ch, CURLOPT_POST, 1);
curl_setopt( $ch, CURLOPT_POSTFIELDS, $myvars);
curl_setopt( $ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt( $ch, CURLOPT_HEADER, 0);
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, 1);

$response = curl_exec( $ch );

This will send the post variables to the specified url, and what the page returns will be in $response.

Saturday, August 6, 2022

you would need 2 forms on your page, one form that posts back to index.php and a second form that posts to paypal

after the first form posts back to index.php, echo javascript in the body tag to submit the paypal form when it loads


        do database stuff

        $LOAD = 'document.paypal.submit();';
    <body onload="<?php echo $LOAD ?>">
    <form name="paypal" action="">
    paypal fields
    <form name="myform" action="index.php">
    your form stuff
    submit button
Sunday, September 18, 2022

Your post data is JSON so use json_decode to turn it into an array containing anobject and access the object_id property

$rawPostData = file_get_contents('php://input');
$json = json_decode($rawPostData);
$json = $json[0];
$all = date("F j, Y, g:i a") . " " . $json->object_id. "rn";
file_put_contents("Activity.log", $all, FILE_APPEND);
Monday, October 3, 2022

I just found the solution and yea it was easier than I thought :)

so here is the solution:

string URI = "";
string myParameters = "param1=value1&param2=value2&param3=value3";

using (WebClient wc = new WebClient())
    wc.Headers[HttpRequestHeader.ContentType] = "application/x-www-form-urlencoded";
    string HtmlResult = wc.UploadString(URI, myParameters);

it works like charm :)

Friday, December 23, 2022

I used:

QByteArray postData;

So & instead of newline after each parameter.

Sunday, October 30, 2022
Only authorized users can answer the search term. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :