Here is my problem
I want to post data to a page using curl, then to be redirected to the destination page without using the header function.
The call must be a post request.
I don't want to retrieve data on the call page.
Any solution ?
Please help
Thank you
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,"http://www.example.com/postdatahere.php");
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS,
"postvar1=value1&postvar2=value2&postvar3=value3");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$server_output = curl_exec ($ch);
curl_close ($ch);
if ($server_output == "OK") {
header("Location: http://example.com/destination.php");
die();
}
You can pickup the variables posted to this page, post them along to postdatahere.php, and still end up redirected to destination.php.
There are multiple way to handle it. One simple solution is to save the $data into session and retrieve the session data from the other file.
Another method is to create a form with those values and post the form using Javascript.
Related
I need to send POST data to the remote form. I have a simple script...
$data['field1'] = '1';
$data['field2'] = '2';
$data['field3'] = '3';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://www.example.com/my-form.php?authToken=poWQyj2q");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
$output = curl_exec($ch);
$info = curl_getinfo($ch);
curl_close($ch);
echo $output;
... It would be work nice, but there is a problem with form URL - How you can see, there is some "authToken" value which changes each time a page is loaded. When it is incorrect, the form is sent, but not processed.
Any ideas how to solve it? Thanks.
Request the page containing the form
Extract the auth token from the DOM
(Presumably) store the cookie value
Make your request but also include the auth token and the cookie
Please look the codes as below :
$url='https://www.test.com/test.php';
$post='?field1=1&field2=2&filed3'; // no need array text as is
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS,$post);
curl_exec ($ch);
curl_close ($ch)
Should be simple code, I used as reference http://curl.haxx.se/libcurl/php/examples/simplepost.html
I modified the code by replacing to variables.
I need to send data to remote server which belong to third party. Other side's server have data base. When I copy manually the www.test.com/test.php?field1=1&field2=2&filed3 into web browser's then the data saved into data base at other server and having respond {"Code":15,"Msg":null"} on browser screen, that's mean data sent properly. When trying to send by PHP script, the data not save in remote data base also not getting respond message.
If the request works when you enter it into a web browser, chances are it is a GET rather than a POST request. A simple example of doing that would be as follows:
$url= 'http://www.test.com/test.php?field1=1&field2=2&filed3';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$result = curl_exec($ch);
curl_close($ch);
if (curl_errno($ch)) {
echo 'Error: ' . curl_error($ch);
}
$http_status = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
var_dump($http_status);
var_dump($result);
I've also set the CURLOPT_RETURNTRANSFER option so you can capture the response in $result.
I'm trying to get copy a website page, but it redirects after enter it on my browser.
For example,
I enter,
http://www.domain.com/cat/121
it redirects,
http://www.domain.com/cat/121/title-of-the-page/
And when I try to php copy function for "www.domain.com/cat/121"
it is not working...
How can I take the redirected new url?
$url='your url';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); // follow the redirects
curl_setopt($ch, CURLOPT_HEADER, false); // no needs to pass the headers to the data stream
curl_setopt($ch, CURLOPT_NOBODY, true); // get the resource without a body
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); // accept any server certificate
curl_exec($ch);
// get the last used URL
$lastUrl = curl_getinfo($ch, CURLINFO_EFFECTIVE_URL);
curl_close($ch);
echo $lasturl;
this will help you to get the redirected url
Use header function for redirecting to specific url.
header('Location: http://www.domain.com/cat/121');
If you are using a CMS you can use appropriate plugins.
For instance, For Wordpress you could use: https://wordpress.org/plugins/redirection/
Please excuse the probable simplicity of the question but I am new to curl and therefore finding my feet. I have the following which is in a php page and when called meant to post dept=xxx to mysite
$ch = curl_init( );
curl_setopt($ch, CURLOPT_URL,"http://mysite/");
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, "dept=".$departmentName);
curl_exec ($ch);
curl_close ($ch);
I have 2 queries:
1) When called the screen goes blank and the user is not redirected.
2) If redirection is correct, am I right in thinking that the following will correctly receive the data posted:
if (isset($_POST["dept"]))
{
$deptName=$_POST["dept"];
}
Include this cURL paramter to your existing set.
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
I need a php page, when loaded, to send a POST request automatically without using a submit button or any user input. I don't need to do anything with the response.
I simply need to POST "8" to www.mydomain.com
$url = 'http://www.mydomain.com/';
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, 8);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
curl_close($ch);
What am I doing wrong?
Try this:
curl_setopt($ch, CURLOPT_POSTFIELDS, array('data' => 8));
On your domain edit script to:
echo $_POST['data'];
Kind of a strange request .. you should consider using a key for the value of 8.
You can, however, get the POST body on http://www.mydomain.com/ by reading 'php://input'.
//on www.mydomain.com/index.php
echo file_get_contents('php://input'); //8
By the way you also have $curl instead of $ch on the third setopt.