【问题标题】:How to make a post request without curl?如何在没有 curl 的情况下发出 post 请求?
【发布时间】:2020-06-01 10:43:56
【问题描述】:

考虑到我不想使用 curl,它是发出发布请求的有效替代方法?也许Zend_http_client

我只需要基本的东西(我需要一个只有一个帖子参数的网址)

【问题讨论】:

  • 您宁愿部署框架也不愿使用 cURL?
  • @webarto:部署? Zend 框架只需要一个包含路径。有了它,您就可以访问这么多值得的功能

标签: php http


【解决方案1】:

您可以使用file_get_contents()。

PHP 手册有一个很好的example here。这只是从手册中复制过去:

$postdata = http_build_query(
    array(
        'var1' => 'some content',
        'var2' => 'doh'
    )
);
$opts = array('http' =>
    array(
        'method'  => 'POST',
        'header'  => 'Content-type: application/x-www-form-urlencoded',
        'content' => $postdata
    )
);
$context  = stream_context_create($opts);
$result = file_get_contents('http://example.com/submit.php', false, $context);

【讨论】:

    【解决方案2】:

    您可以使用stream_context_createfile_get_contents

    <?php
    $context_options = array (
            'http' => array (
                'method' => 'POST',
                'header'=> "Content-type: application/x-www-form-urlencoded\r\n"
                    . "Content-Length: " . strlen($data) . "\r\n",
                'content' => $data
                )
            );
    ?>
    
    
    $context = stream_context_create($context_options); 
    $result = file_get_contents('http://www.php.net', false, $context);
    

    【讨论】:

      【解决方案3】:

      您可以通过套接字自己实现它:

          $url = parse_url(''); // url
          $requestArray = array('var' => 'value');
          $sock = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
          socket_connect($sock, $url['host'], ((isset($url['port'])) ? $url['port'] : 80));
          if (!$sock) {
              throw new Exception('Connection could not be established');
          }
      
          $request = '';
          if (!empty($requestArray)) {
              foreach ($requestArray as $k => $v) {
                  if (is_array($v)) {
                      foreach($v as $v2) {
                          $request .= urlencode($k).'[]='.urlencode($v2).'&';
                      }
                  }
                  else {
                      $request .= urlencode($k).'='.urlencode($v).'&';
                  }
              }
              $request = substr($request,0,-1);
          }
          $data = "POST ".$url['path'].((!empty($url['query'])) ? '?'.$url['query'] : '')." HTTP/1.0\r\n"
          ."Host: ".$url['host']."\r\n"
          ."Content-type: application/x-www-form-urlencoded\r\n"
          ."User-Agent: PHP\r\n"
          ."Content-length: ".strlen($request)."\r\n"
          ."Connection: close\r\n\r\n"
          .$request."\r\n\r\n";
          socket_send($sock, $data, strlen($data), 0);
      
          $result = '';
          do {
              $piece = socket_read($sock, 1024);
              $result .= $piece;
          }
          while($piece != '');
          socket_close($sock);
          // TODO: Add Header Validation for 404, 403, 401, 500 etc.
          echo $result;
      

      当然,您必须更改它以满足您的需求或将其包装到一个函数中。

      【讨论】:

        【解决方案4】:

        如果您使用 pecl_http 配置 PHP,最简单的方法是:

        $response = http_post_data($url, $post_params_string);

        该函数记录在 php.net 上:

        PECL 还提供了一种有据可查的方法来在 POST 之前处理 Cookie、重定向、身份验证等:

        http://php.net/manual/en/http.request.options.php

        【讨论】:

          【解决方案5】:

          如果你已经在使用 Zend 框架,你应该试试你提到的Zend_Http_Client

          $client = new Zend_Http_Client($host, array(
                      'maxredirects' => 3,
                      'timeout'      => 30));
          $client->setMethod(Zend_Http_Client::POST);
          // You might need to set some headers here
          $client->setParameterPost('key', 'value');
          $response = $client->request();
          

          【讨论】:

          • 不,我没有使用 zend 框架,但考虑到这一点,我应该现在开始:D
          • 可能想检查一下,但 Zend 可能只是在包装 curl
          • 无论如何,如果我只想在我的框架中使用 zend_http_client,是否有安装教程?还是要使用 zend_http_client 我必须使用所有框架?
          • 好的,很简单,设置包含路径就可以了
          【解决方案6】:

          RESTclient 是一个不错的小应用程序:http://code.google.com/p/rest-client/

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2017-09-15
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2016-06-15
            • 1970-01-01
            • 2019-07-12
            • 1970-01-01
            相关资源
            最近更新 更多