1

In a php script I am receiving some data:

$data = $_POST['someData'];

How can I do something like this:

goToThisUrl( "http://someDomain.com/someScript.php?data = ".$data );

or if it is easier how can I do it by POST?

BTW. This is not happening in a browser, the first php script is getting called by a cart when the order is paid for (if it makes any difference)

3
  • Do you really need to call off to another php script like this? If someScript.php is on the same domain, it would be a lot easier to include it using require and simply call the functions directly. Commented Aug 11, 2011 at 0:22
  • @Chris The question title says "to php on another server" Commented Aug 11, 2011 at 0:23
  • You want cURL: php.net/manual/en/book.curl.php Commented Aug 11, 2011 at 0:26

3 Answers 3

1

Replace goToThisUrl with the real function file_get_contents and remember to urlencode($data) and that would work just fine.

If you want to POST the data instead, look at cURL. Typing "[php] curl post" into the search box will get you the code.

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

1 Comment

You can also POST with file_get_contents using stream_context_create, which is far simpler than cURL.
1

If you want to send the user there, then:

header('Location: http://someDomain.com/someScript.php?data='.$data);
exit;

Or if you just want to call the other server, you can do:

$response = file_get_contents('http://someDomain.com/someScript.php?data='.$data);

Both assume data is already a urlencoded string, you might want to use 'data=' . urlencode($data) or just http_build_query($data) otherwise.

Comments

-1
foreach ($_POST as $key => $val) {
  $qs = urlencode($key) . "=" . urlencode($val) . "&";
}
$base_url = "<url here>";
$url = $base_url . "?" . $qs;
header('Location: $url'); exit();

2 Comments

Change that to only urlencode($val) in the loop and not the entire query string and it'd be good. As is, you destroy the query string by encoding the = and & symbols.
@Dan you'd also want urlencode($key) to be safe, or simply $qs = http_build_query($_POST);

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.