将数据发送到第三方服务器,然后取回数据


Sending data to third-party server then retrieving data back

这在PHP:中可能吗

  1. 用户在我的网站上填写表格
  2. 表单将表单中的数据提交给网络上其他地方的第三方服务器,本质上是以某种方式将数据交给第三方服务
  3. 所述第三方服务器对数据进行处理,然后生成一个数值发送回我的PHP脚本
  4. 我的服务器/PHP脚本获取该数值/数据,以便再次在脚本中使用

它在PHP中可行吗?PHP是否具有执行上述任务的内置功能?这样的事情需要大量的高级代码吗?还是相对容易做到?

提前感谢您对此事的任何帮助

您可以将cURL用于

$urltopost = "http://somewebsite.com/script.php";
$datatopost = $_POST; //This will be posted to the website, copy what has been posted to your website
$ch = curl_init ($urltopost);
curl_setopt ($ch, CURLOPT_POST, true);
curl_setopt ($ch, CURLOPT_POSTFIELDS, $datatopost);
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, true);
$returndata = curl_exec ($ch); //This is the output the server sends back

是的,当您发送表单时,使用POST方法将其发送到您想要的服务器。看起来像:

<form action="www.siteURL/pageToParseCode.php" method="post">
  First name: <input type="text" name="fname" /><br />
  Last name: <input type="text" name="lname" /><br />
  <input type="submit" value="Submit" />
</form>

在服务器上,它发送给你会想做一些类似的事情:

$field1 = $_POST['field1name'];

在将处理数据的服务器上,您可以使用curl之类的东西将其发布回您的服务器,如果您不完全理解curl,请查看那里的链接,或者您可以使用php标头,并使用get方法将您想要发送回的数据设置为url,因此用户使用get方法接收的url将如下所示:

www.yoursite.com/index.php?variable1=值1&variable2=value2等等,然后这样解释:

if (isset($_GET['variable1'])) {
$var = $_GET['variable1'];
}
相关文章: