如何使用PHP发布外部表单?

时间:2012-02-14 18:04:37

标签: php forms curl

外部表格

<form action="MyForm.action" method="post">  

托管在不同的服务器上。

我希望我的PHP脚本发布此表单并显示结果

我该怎么做?

1 个答案:

答案 0 :(得分:2)

要从PHP脚本发布表单,您需要使用cURL。这是一个例子

<?php
  $url = 'http://domain.com/get-post.php';
  $fields = array(
    'lname'=>urlencode($last_name),
    'fname'=>urlencode($first_name),
  );

  //url-ify the data for the POST
  foreach($fields as $key=>$value) { $fields_string .= $key.'='.$value.'&'; }
  rtrim($fields_string,'&');

  // Initialize curl
  $ch = curl_init();

  //set the url, number of POST vars, POST data
  curl_setopt($ch,CURLOPT_URL,$url);
  curl_setopt($ch,CURLOPT_POST,count($fields));
  curl_setopt($ch,CURLOPT_POSTFIELDS,$fields_string);

  //execute post
  $result = curl_exec($ch);

  // Results of post in $result
?>
相关问题