$url = $params['url']; //www.111.com/index.php/ww/11/tt/22
$data = $params['post']; //$_POST
$url = parse_url($url);
if (!$url) return "couldn’t parse url";
if (!isset($url['port'])) { $url['port'] = ""; }
if (!isset($url['query'])) { $url['query'] = ""; } $encoded = "";
$encoded = http_build_query($data); $fp = fsockopen($url['host'], $url['port'] ? $url['port'] : 80);
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, "Connection: close\n\n"); fputs($fp, "$encoded\n"); $line = fgets($fp,1024); $results = ""; $inheader = 1;
while(!feof($fp)) {
$line = fgets($fp,1024);
if ($inheader && ($line == "\n" || $line == "\r\n")) {
$inheader = 0;
}
elseif (!$inheader) {
$results .= $line;
}
}
fclose($fp);
$arr_json = json_decode($results, true);
$result = $arr_json;这段代码是什么功能 请给我解释下 谢谢了
$data = $params['post']; //$_POST
$url = parse_url($url);
if (!$url) return "couldn’t parse url";
if (!isset($url['port'])) { $url['port'] = ""; }
if (!isset($url['query'])) { $url['query'] = ""; } $encoded = "";
$encoded = http_build_query($data); $fp = fsockopen($url['host'], $url['port'] ? $url['port'] : 80);
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, "Connection: close\n\n"); fputs($fp, "$encoded\n"); $line = fgets($fp,1024); $results = ""; $inheader = 1;
while(!feof($fp)) {
$line = fgets($fp,1024);
if ($inheader && ($line == "\n" || $line == "\r\n")) {
$inheader = 0;
}
elseif (!$inheader) {
$results .= $line;
}
}
fclose($fp);
$arr_json = json_decode($results, true);
$result = $arr_json;这段代码是什么功能 请给我解释下 谢谢了
这里是用来做代理,可以解决ajax跨域的问题。
这个用curl来做会简单许多,这哥们看来可能是个c程序员,不熟悉php的curl库,所以执意要用socket。http,ftp,https请求用curl就可以了。除非是其它协议,才有一定要用socket的必要。