fsockopen 不会发送 post 数据

发布于 2024-11-15 08:22:22 字数 2758 浏览 4 评论 0原文

已解决:问题出在内容类型中。应该是

Content-Type: application/x-www-form-urlencoded

帮助链接: http://www.bradino.com/php/empty- post-array/

我似乎无法获取要发送的 POST 数据。我已经对这个方法进行了一段时间的梳理,每次运行测试时,$_POST 数组都是空的。

    $output = new SimpleXMLElement($xml);
    $params = "method=updateOrder&xml=".$output;
    $response = Rest::Post("example.com", 80, "/resource/path.php", $params);

上面是我静态调用的另一个方法,它使用所需的方法创建 HttpRequest 。下面是被子调用并传递相同数据但包括方法名称的方法。即:POST

private static function httpRequest($host, $port, $method, $path, $params)
            {
                //Check method  
                if(empty($method))
                    $method = "GET";
                $method = strtoupper($method);

                //Port
                if(empty($port))
                    $port = 80;

                //Build Querystring
                $data = "";
                if(!empty($params) && $method == "GET")
                    foreach($params as $name => $value)
                        {
                            $data .= $name . "=" . urlencode($value) . "&";
                        }
                if($method == "GET" && !empty($data))
                    $path .= "?" . $data;


                //connection
                $socket = fsockopen($host, $port);
                if(!$socket)
                    die("Socket failed, no connection.");

                //Write Data and headers to stream
                fputs($socket, $method ." ". $path . " HTTP/1.1\r\n");
                fputs($socket, "Host: " . $host . "\r\n");
                if($method === "POST")
                    {
                        fputs($socket, "Content-type: text/xml\r\n");
                        fputs($socket, "Content-length: " . strlen($params) . "\r\n");
                    }

                fputs($socket, "Connection: close\r\n\r\n");

                //Write body
                if($method === "POST")
                    fputs($socket, $params);

                //Gets headers
                $responseHeader = "";
                do
                {
                    $responseHeader .= fgets($socket, 1024);
                }
                while(strpos($responseHeader, "\r\n\r\n") === false);

                //Gets body
                $responseBody = "";
                while(!feof($socket))
                    $responseBody .= fgets($socket, 1024);

                //Done & return
                fclose($socket);
                return array(0=>$responseHeader, 1=>$responseBody);
            }

SOLVED: Issue was in Content-type. Should have been

Content-Type: application/x-www-form-urlencoded

Helping link: http://www.bradino.com/php/empty-post-array/

I cannot seem to get POST data to send. I've combed over this method for a while now and each time I run a test, the $_POST array is empty.

    $output = new SimpleXMLElement($xml);
    $params = "method=updateOrder&xml=".$output;
    $response = Rest::Post("example.com", 80, "/resource/path.php", $params);

Above is another method I call statically which creates the HttpRequest with the desired method. Below is the method that gets sub-called and passed the same data but including the method name. IE: POST.

private static function httpRequest($host, $port, $method, $path, $params)
            {
                //Check method  
                if(empty($method))
                    $method = "GET";
                $method = strtoupper($method);

                //Port
                if(empty($port))
                    $port = 80;

                //Build Querystring
                $data = "";
                if(!empty($params) && $method == "GET")
                    foreach($params as $name => $value)
                        {
                            $data .= $name . "=" . urlencode($value) . "&";
                        }
                if($method == "GET" && !empty($data))
                    $path .= "?" . $data;


                //connection
                $socket = fsockopen($host, $port);
                if(!$socket)
                    die("Socket failed, no connection.");

                //Write Data and headers to stream
                fputs($socket, $method ." ". $path . " HTTP/1.1\r\n");
                fputs($socket, "Host: " . $host . "\r\n");
                if($method === "POST")
                    {
                        fputs($socket, "Content-type: text/xml\r\n");
                        fputs($socket, "Content-length: " . strlen($params) . "\r\n");
                    }

                fputs($socket, "Connection: close\r\n\r\n");

                //Write body
                if($method === "POST")
                    fputs($socket, $params);

                //Gets headers
                $responseHeader = "";
                do
                {
                    $responseHeader .= fgets($socket, 1024);
                }
                while(strpos($responseHeader, "\r\n\r\n") === false);

                //Gets body
                $responseBody = "";
                while(!feof($socket))
                    $responseBody .= fgets($socket, 1024);

                //Done & return
                fclose($socket);
                return array(0=>$responseHeader, 1=>$responseBody);
            }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

感性 2024-11-22 08:22:23

您应该考虑使用内置 cURL 库

function httpRequest($host, $port = 80, $method = 'GET', $path = '/', $params = null)
{
  $ch = curl_init();
  curl_setopt($ch, CURLOPT_HEADER, TRUE);
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);

  if ( ! empty($port) )
    curl_setopt($ch, CURLOPT_PORT, $port);
  
  if ( ! empty($method) && $method == 'POST' )
  {
    curl_setopt($ch, CURLOPT_URL, $host . ( ! empty($path) ? $path : '/' ));
    curl_setopt($ch, CURLOPT_POST, TRUE);

    if ( ! empty($params) )
      curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
  }
  else
    curl_setopt($ch, CURLOPT_URL, $host . ( ! empty($path) ? $path : '/' ) . ( ! empty($params) ? '?' . $params : null ));

  $result = curl_exec($ch);
  
  curl_close($ch);
  
  return explode("\r\n\r\n", $result, 2);
}

上面的函数将返回一个包含标题和正文的数组。

You should consider using the built-in cURL library.

function httpRequest($host, $port = 80, $method = 'GET', $path = '/', $params = null)
{
  $ch = curl_init();
  curl_setopt($ch, CURLOPT_HEADER, TRUE);
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);

  if ( ! empty($port) )
    curl_setopt($ch, CURLOPT_PORT, $port);
  
  if ( ! empty($method) && $method == 'POST' )
  {
    curl_setopt($ch, CURLOPT_URL, $host . ( ! empty($path) ? $path : '/' ));
    curl_setopt($ch, CURLOPT_POST, TRUE);

    if ( ! empty($params) )
      curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
  }
  else
    curl_setopt($ch, CURLOPT_URL, $host . ( ! empty($path) ? $path : '/' ) . ( ! empty($params) ? '?' . $params : null ));

  $result = curl_exec($ch);
  
  curl_close($ch);
  
  return explode("\r\n\r\n", $result, 2);
}

The function above will return an array containing the headers and the body.

甜宝宝 2024-11-22 08:22:23

这有帮助吗?

更改

fputs($socket, "Content-type: text/xml\r\n");

fputs($socket, "Content-type: application/x-www-form-urlencoded\r\n");

Does this help?

Change

fputs($socket, "Content-type: text/xml\r\n");

to

fputs($socket, "Content-type: application/x-www-form-urlencoded\r\n");
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文