1、请使用 socket 相关函数(非 curl )实现如下功 能:构造一个 post 请求,发送到指定 http server 的指定端口的指定请求路径(如 http://www.phpddt.com:8080/test )。请求中包含以下变量:
用户名( username ):温柔一刀
密码( pwd ): &123=321&321=123&
个人简介( intro ): Hello world !
且该 http server 需要以下 cookie 来进行简 单的用户动作跟踪:
cur_query : you&me
last_tm : … (上次请求的 unix 时间戳,定为当前请求时间前 10 分钟)
cur_tm : … (当前请求的 unix 时间戳)
设置超时为 10 秒,发出请求后, 将 http server 的响应内容输出。
传智播客的答案:
网上的答案
Function encode($data, $sep = ‘&’){
while (list($k,$v) = each($data)) {
$encoded .= ($encoded ? "$sep" : "");
$encoded .= rawurlencode($k)."=".rawurlencode($v);
}
Return $encoded;
}
Function post($url, $post, $cookie){
$url = parse_url($url);
$post = encode($data, ‘&’);
$cookie = encode($cookieArray, ‘;’);
$fp = fsockopen($url['host'], $url['port'] ? $url['port'] : 80,$errno,$errstr,10);
if (!$fp) return "Failed to open socket to $url[host]";
fputs($fp, sprintf("POST %s%s%s HTTP/1.0/n", $url['path'], $url['query'] ? "?" : "", $url['query']));
fputs($fp, "Host: $url[host]/n");
fputs($fp, "Content-type: application/x-www-form-urlencoded/n");
fputs($fp, "Content-length: " . strlen($encoded) . "/n");
fputs($fp, "Cookie: $cookie/n/n");
fputs($fp, "Connection: close/n/n");
fputs($fp, "$post /n");
while (!feof($fp