fsockopen 不会发送 post 数据
已解决:问题出在内容类型中。应该是
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您应该考虑使用内置 cURL 库。
上面的函数将返回一个包含标题和正文的数组。
You should consider using the built-in cURL library.
The function above will return an array containing the headers and the body.
这有帮助吗?
更改
为
Does this help?
Change
to