【发布时间】:2016-03-16 09:44:06
【问题描述】:
我正在用 php 开发一个简单的客户端和服务器并想出了这个
Client.php
<?php
// Create map with request parameters
$params = array ('name' => 'ASD', 'lastname' => 'SDF');
// Build Http query using params
$query = http_build_query ($params);
// Create Http context details
$contextData = array (
'method' => 'POST',
'header' => "Connection: close\r\n".
"Content-Length: ".strlen($query)."\r\n",
'content'=> $query );
// Create context resource for our request
$context = stream_context_create (array ( 'http' => $contextData ));
// Read page rendered as result of your POST request
$result = file_get_contents (
'http://localhost/phpserver/server.php', // page url
false,
$context);
// Server response is now stored in $result variable so you can process it
服务器文件是:
Server.php
<?php
if( $_POST["name"] || $_POST["lastname"] )
{
echo "Welcome: ". $_POST['sirname']. "<br />";
echo "Your Email is: ". $_POST["lastname"]. "<br />";
}
?>
我说错了
注意:file_get_contents():假设内容类型未指定 应用程序/x-www-form-urlencoded 在 C:\wamp\www\phpServer\client.php 在第 22 行
我对 php 的了解不多。只想在简单的服务器和客户端之间传递数据 (更好的无卷曲解决方案)
【问题讨论】: