【问题标题】:Sending Data Using Post Method Without Form使用没有表单的 Post 方法发送数据
【发布时间】:2011-10-09 02:40:10
【问题描述】:

我想向 API 发送数据。数据包括简单的变量:用户名、密码、电子邮件等。 问题是 O 想使用 POST 方法向它发送数据。我在谷歌上搜索了这个问题,每个人都说去 CURL。

什么是 CURL?它是函数、脚本、API 还是什么?

还有其他方法吗?

我想发送这样的东西:

$username, $password to www.abc.com?username=$username&password=$password

最好的问候

【问题讨论】:

    标签: php post curl


    【解决方案1】:

    Php curl manual 是一个很好的起点。

    以下是 Stackoverflow 上 php 中 curl 的使用示例:

    Send json post using php

    【讨论】:

      【解决方案2】:

      通过使用此功能,您可以发送带有可选数量的 post 请求 参数。只需使用键值对填充 postdata 数组。 提供了示例用法。

      <?php
      
      function do_post_request($url, $postdata)
      {
        $content = "";
      
        // Add post data to request.
        foreach($postdata as $key => $value)
        {
          $content .= "{$key}={$value}&";
        }
      
        $params = array('http' => array(
          'method' => 'POST',
          'header' => 'Content-Type: application/x-www-form-urlencoded',
          'content' => $content
        ));
      
        $ctx = stream_context_create($params);
        $fp = fopen($url, 'rb', false, $ctx);
      
        if (!$fp) {
          throw new Exception("Connection problem, {$php_errormsg}");
        }
      
        $response = @stream_get_contents($fp);
        if ($response === false) {
          throw new Exception("Response error, {$php_errormsg}");
        }
      
        return $response;
      }
      
      // Post variables.
      $postdata = array(
        'username' => 'value1',
        'password' => 'value2',
        'param3' => 'value3'
      );
      
      do_post_request("http://www.example.com", $postdata);
      ?>
      

      【讨论】:

      • 更新了一个利用流的示例解决方案。
      【解决方案3】:

      cURL 是一个库,“它允许您使用许多不同类型的协议连接和通信到许多不同类型的服务器”。因此,您可以使用 cURL 发送 HTTP POST 请求。详细信息请阅读PHP手册:http://www.php.net/manual/en/book.curl.php

      但 cURL 并不是唯一的答案。您可以使用PHP Streams,甚至可以使用套接字函数连接到网络服务器,然后生成您的请求。

      我个人经常使用 cURL,因为它非常灵活,而且您可以轻松地使用 cookie。

      【讨论】:

      • 为什么不包括一个使用 cURL 实际发送 Post 数据和答案的示例?
      【解决方案4】:

      xml文件中的数据集:

      $post="<?xml version="1.0\" encoding=\"UTF-8\"?>
      <message xmlns=\"url\"   >
      <username>".$username."</username>
      <password>".$password."</password>
      
      <status date=\"2010-11-05 14:44:10+02\"/>
      <body content-type=\"text/plain\">Noobligatory text</body>
      </message>";
      
      $url = "a-url-to-send-the-data";
      
      $ch = curl_init();
      curl_setopt($ch, CURLOPT_URL,$url);
      curl_setopt($ch, CURLOPT_FAILONERROR, 1);
      curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
      curl_setopt($ch, CURLOPT_TIMEOUT, 6);
      curl_setopt($ch, CURLOPT_POST, 1);
      curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
      curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
      
      $result = curl_exec($ch);
      curl_close($ch);
      

      【讨论】:

        【解决方案5】:

        只需使用:file_get_contents()

        // building array of variables
        $content = http_build_query(array(
                    'username' => 'value',
                    'password' => 'value'
                    ));
        // creating the context change POST to GET if that is relevant 
        $context = stream_context_create(array(
                    'http' => array(
                        'method' => 'POST',
                        'content' => $content, )));
        
        $result = file_get_contents('http://www.example.com', null, $context);
        //dumping the reuslt
        var_dump($result);
        

        【讨论】:

          猜你喜欢
          • 2011-09-17
          • 1970-01-01
          • 1970-01-01
          • 2018-10-13
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2022-11-19
          • 1970-01-01
          相关资源
          最近更新 更多