【问题标题】:How to send data from my form to webservice?如何将数据从我的表单发送到网络服务?
【发布时间】:2012-02-25 14:45:24
【问题描述】:

我需要将一些数据从我的 html 表单发送到 web 服务。 (为此,我需要做 POST 的操作)

我看到研究表明我可以使用 php cURL 传输信息。但在所有示例中,我不认为将数据发送到 web 服务,只发送到打印 $_POST 变量的文件 php。

我有这个网络服务:http://192.168.1.1/fastfood/event/attendee(示例) 我尝试以数组的形式发送数据。

例如我尝试发送:attendee = array( 'name' => $_POST['name'] , 'lastname' => $_POST['lastname'] , 'address' => $_POST['address'] );

然后,Web 服务取出数组数据。 ¿如何做到这一点?

更新 1:

这是我现在正在做的代码...但是不工作:(

$name = $_POST['name'];
$lastname = $_POST['lastname'];
$address = $_POST['address'];

$attendee = array(
    'name' => "$name",
    'lastname' => "$lastname",
    'address' => "$address"
);

$url_target = 'http://192.168.1.1/fastfood/event/attendee';
//$header = array('Content type: multipart/form-data');
$user = 'root';
$pass = '123';
$userpasswd = "$user:$pass";

$ch = curl_init($url_target);

    curl_setopt($ch, CURLOPT_POST, TRUE);
    curl_setopt($ch, CURLOPT_USERPWD, $userpasswd);
    //curl_setopt($ch, CURLOPT_URL, $url_target);
    //curl_setopt($ch, CURLOPT_HEADER, TRUE);
    //curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $attendee);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
    curl_setopt($ch, CURLOPT_FRESH_CONNECT, TRUE);
    curl_setopt($ch, CURLOPT_FAILONERROR, TRUE);
    curl_setopt($ch, CURLOPT_TIMEOUT, 30);
    $result = curl_exec($ch);

    $getInfo = curl_getinfo($ch);

curl_close($ch);

变量 $result 返回 FALSE,变量 $getInfo 返回 http_code = 500,Content-Type = Null。

当我发送数组之类的数据时阅读 cURL 的文档,内容类型应该是“multipart/form-data”,但也不适用于我。

【问题讨论】:

  • 您可以将您的 from 指向一个 php 文件,该 php 文件可以使用 cURL 通过“POST”操作向 Web 服务发送请求。
  • @Shaheer Uuhhmm 就是这样。但是,如何做到这一点?...现在我正在编写我的 php 脚本。但是,我的网络服务没有收到数据:( ..当我的网络服务收到这些数据时,会自动显示在表格中。但不要显示...
  • 首先,确保该网络服务支持它(它们可以轻松阻止外部请求)。其次,阅读此php.net/manual/en/book.curl.php。祝你好运
  • @OfirBaruch 可以给我一个例子吗?.. 我做了一个代码,但我没有发送数据:S 我不知道我做错了。我已经更新了我的帖子。
  • 如果您收到 FALSEhttp_code=500,则该 URL 可能不允许发送外部请求或错误的登录详细信息。 (500 http 代码表示禁止访问)

标签: php web-services curl


【解决方案1】:
// Here is the data we will be sending to the service
  $data = array(
    'name'     => $_POST['name'],
    'lastname' => $_POST['last_name'],
    'address'  => $_POST['address']
  );  


  $curl = curl_init('http://192.168.1.1/fastfood/event/attendee');


  curl_setopt($curl, CURLOPT_POST, 1); //Choosing the POST method
  curl_setopt($curl, CURLOPT_URL, 'http://localhost/helloservice.php');  // Set the url path we want to call
  curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);  // Make it so the data coming back is put into a string
  curl_setopt($curl, CURLOPT_POSTFIELDS, $some_data);  // Insert the data

  // Send the request
  $result = curl_exec($curl);

  // Free up the resources $curl is using
  curl_close($curl);

  echo $result;

由 Chad Lung 撰写,GiantFlyingSaucer

【讨论】:

  • 谢谢!..这段代码和我一直在做的非常相似..这个例子对我帮助很大
  • 只有一个问题,为什么 curl_init(服务的 URL)的参数与 CUROPT_URL(一个 php 脚本)的参数不同?有什么不同?
  • 所以.. 我需要重复我的网络服务的 url 吗??
  • 如果我没记错的话 - 是的。一个简单的检查方法是尝试一下。玩代码:)
  • UUhhmmm 不适合我 :( ...我正在用我的代码更新我的帖子。我不知道我做错了什么。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-03-18
  • 2013-10-27
  • 2011-11-11
  • 1970-01-01
相关资源
最近更新 更多