php post raw data - follow post URL - php

i need to post raw data (XML) to an external site, the XML data is good to go, and is working with the below example...
I am using PHP / cURL
normally i do this by cURL, but in this instance, i need to redirect the client to the URL with the post data...
It works using the below code, but it won't redirect the user to the $URL, this method just prints the data while still being in the script on the same server.
eg:
Post from site1.com to site2.com, the below method keeps the client on site1, but needs to redirect the user to site2.com with the raw post data...
$ch = curl_init($URL);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_xml);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_AUTOREFERER, 1);
curl_setopt($ch, CURLOPT_HEADER, 0);
$val = curl_exec($ch);
curl_close($ch);
i have searched all-over but cannot find a method where it functions as a "regular" form submit where the user is redirected to the URL which the data is posted to... but with RAW post data.

Related

Instagram API call to retrieve authorization code with CURL returns empty

I am trying to set up an Instagram API feed with CURL in PHP, however the string returns empty when trying to retrieve the auth code, before later making a successful call to retrieve a user's content:
$uri = 'https://api.instagram.com/oauth/authorize/';
$data = [
'client_id' => $client_id,
'redirect_uri' => $redirect_uri,
'response_type' => 'code'
];
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $uri);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_NOBODY, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
$result = curl_exec($ch);
curl_close($ch);
var_dump($result);
However, when I access the URL directly in the browser, it does return to the redirect URI with the code that I need:
https://api.instagram.com/oauth/authorize/?client_id=CLIENT_ID_HERE&redirect_uri=REDIRECT_HERE&response_type=code
i.e. the browser redirects successfully (including the "code" GET data) to http://REDIRECT_HERE/?code=XXXXXXXXXXXX
I'd basically like to store the auth code in a variable for later use rather than using the manual way of getting the code via a browser redirect.
your browser sends the data in the url RFC-1738 style with a GET request, while your php curl code sends the data in a POST request encoded with multipart/form-data, no wonder the server gets confused. to send the data in the url with php just like your browser does, get rid of curl_setopt($ch, CURLOPT_POST, true); (because it tells curl to do a POST request instead of the default GET request, your browser does a GET request) and curl_setopt($ch, CURLOPT_POSTFIELDS, $data); (because it tells curl to send it in the request body, while your browser send it in the url), and change your CURLOPT_URL to
curl_setopt($ch, CURLOPT_URL, $uri."?".http_build_query($data));
then your php curl request should roughly match your browser request, and get the same response. (there will still be minor differences, like a different User-Agent header, Accept header, and Accept-Encoding header, but that usually doesn't matter)

When using cURL, can the target page detect the exact url?

Lets say we have our own script here:
https://example.com/random_name/script.php
// Code inside the script.php
$url = 'https://foo.com';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 30);
curl_setopt($ch, CURLOPT_TIMEOUT, 60);
$data = curl_exec($ch);
As you can see, the link above gets content of this domain:
https://foo.com
I know foo.com can see example.com and it's IP Address!
but Questions is, can foo.com (the page we're getting content from) also by any methods detect this exact part:
/random_name/script.php is making the request? does it depend on using TLS?
The page (server/application) can have all the information in the request. If the request (in your example - script.php) will not send the extra data (/random_name/script.php) the page the receive the request will not have it.
If you want to receiving end (foo.com) to know about it you can use the referer header:
curl_setopt($ch, CURLOPT_REFERER, "https://example.com/random_name/script.php");
And this way - foo.com can view that information in the referer header.

php curl post redirect

Im trying to integrate a bank api for redirect payment.
Im using curl post method but i want to be able to follow the redirect after the post.
Here is my code
curl_setopt($ch, CURLOPT_URL, 'https://paycenter.piraeusbank.gr/redirection/pay.aspx');
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, array(
'data' => $data
));
$content = curl_exec($ch);
After the post it supposed that i have to be redirected in a url like
https://paycenter.piraeusbank.gr/redirection/4234sgsfdgfsg/pay.aspx
Instead of that im redirecting to:
http://mytestdomain.org/redirection/4234sgsfdgfsg/pay.aspx
which obviously ends up in 404 error
You need to manipulate your response and then write hard code to redirect this script to particular page as curl execute that url on your server.
Try to add the following code:
curl_setopt($ch, CURLOPT_AUTOREFERER, TRUE);
This will automatically set the Referer, example: redirection.
EDIT:Check out this answer Here, Maybe it will help you out a little.

POST data to a different domain form without leaving current domain

I am currently working on a project which requires me to POST data to an form belonging to a different domain. This does work but I don't want the user to see the data posted to the different domain as it just shows a success / error message (on page B).
What I want to do is have the data POST but the page not change. I have read that AJAX can be used to do this but only if the pages belong to the same domain. windows.location.replaced() looked promising but this only worked if I had access to the external form.
Current implementation:
User enters data on page A > POST data to page B, page B loads
Want I would like to implement:
User enters data on Page A > POST data to page B, page A remains displayed
Make use of cURL to do this.
Page1.php
$url='http://www.domain.com/page2.php';
$params='username=jim&pass=jim321';
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
echo $result = curl_exec($ch); // This outputs the response in your same page i.e. Page1.php
curl_close($ch);
try using curl something like this
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,"http://www.otherdoamin.com/page.php");
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS,"post1=val1&post2=val2&post3=val3");
// get server response ...
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$server_output = curl_exec ($ch);
curl_close ($ch);

Complicated: 4 consecutive curls with XML in between

I've a tricky question on how to deal with consecutive curls in PHP. I have this incredibile data flow:
example.com/one.php post data via curl to another.com/two.php
another.com/two.php post data via curl to another.com/three.php
another.com/three.php responds me with XML (or JSON) back to another.com/two.php
another.php/two.php transforms the XML into a php array and then into a query string that i post via curl back to the origin example.com/one.php
It works. If you are wondering why i have this insane data flow it's due to the fact that another.com/three.php is an obfuscated file with Ioncube. I can't edit it but i have to add some checks before i can send data to him. Don't waste time trying to figure out how i can make it in an alternative way because there isn't one (trust me).
On example.com/one.php there's a form where users fill in data. When they press "Submit" they remain on this page while "silently" i make 1->2->3->4 to get their response (the $_POST of step 4) then can save it into example.com/log.txt. Again it works.
Now my question is: how can i display the $_POST response (which is the same i get in log.txt file) in example.com/one.php? I mean this is what i have in step 4.
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, example.com/one.php);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 30);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $query_string);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
$xml = curl_exec($ch);
$_POST arrives on example.com/one.php but of course users and this curl are on two different level. I tryed playing with file_get_contents(), sleep() and CURLOPT_RETURNTRANSFER with no success. What's the answer this this question? I would go for sessions or ob_start() but i'm not sure of it.

Categories