fsockopen will not send post data
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&xm开发者_C百科l=".$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);
            }
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.
Does this help?
Change
fputs($socket, "Content-type: text/xml\r\n");
to
fputs($socket, "Content-type: application/x-www-form-urlencoded\r\n");
 
         加载中,请稍侯......
 加载中,请稍侯......
      
精彩评论