PHP Post Request inside a POST Request

2019-02-27 16:42发布

问题:

There is a contact form which current action is http://www.siteA.com/ContactInfo.php, it sends fields and values. In ContactInfo.php, i just catch the values and send and email to y@z.x

BUT, inside ContactInfo.php, I need to send the same data to another destination, in other domain http://www.SiteB.com/Reg.aspx

I have tryed out to create an http POST request to SiteB, but It doesn't work, even with another file in the same site.

I have not the code right now, but it's similar to this structure.

<? php
   //ContactInfo.php
   // CATCTH VALUES and SEND EMAIL

   // CREATE Http POST REquest to another www.siteB.com/Reg.aspx
?>

I have tryed out using... HttpRequest object, cURL, and the other one...i can't remember the name.

回答1:

You can try something like this using cURL (http://www.php.net/manual/en/curl.examples.php) ..

$sub_req_url = "http://www.siteB.com/Reg.aspx";

$ch = curl_init($sub_req_url);
$encoded = '';

// include GET as well as POST variables; your needs may vary.
foreach($_GET as $name => $value) {
  $encoded .= urlencode($name).'='.urlencode($value).'&';
}

foreach($_POST as $name => $value) {
  $encoded .= urlencode($name).'='.urlencode($value).'&';
}

// chop off last ampersand
$encoded = substr($encoded, 0, strlen($encoded)-1);

curl_setopt($ch, CURLOPT_POSTFIELDS,  $encoded);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_POST, 1);
curl_exec($ch);
curl_close($ch);

Shamly



回答2:

I'd encourage you to try the Snoopy Class. It's really rather simple:

<?php

  $vars = array("fname"=>"Jonathan","lname"=>"Sampson");
  $snoopy = new Snoopy();

  $snoopy->httpmethod = "POST";
  $snoopy->submit("http://www.siteB.com/Reg.aspx", $vars);

  # Use the following if you need to view the results
  # print $snoopy->results;

?>


回答3:

Without seeing your code it's hard to say what the problem is.

Questions:

Is site B a domain you also own, or is it third party? If it's third party they may be running bot checks and the like that would make your POST fail.

Have you done anything to confirm that the data is in fact being send to domain B?

If domain B is also under your control, my guess is they are running on the same web server - why not just write logic into the domain A page that does whatever it is that needs to be done without having to submit to domain B?