php用file_get_contents、fsockopen发送http请求

$content = stream_context_create(['http' => ['timeout' => 3]]);
$resp_ip = @file_get_contents("http://ip.taobao.com/service/getIpInfo.php?ip={$req_ip}", false, $content);
function SocketPost($data, $target)
{
    $url_info = parse_url($target);
    $httpheader = "POST " . $url_info['path'] . " HTTP/1.0\r\n";
    $httpheader .= "Host:" . $url_info['host'] . "\r\n";
    $httpheader .= "Content-Type:application/x-www-form-urlencoded\r\n";
    $httpheader .= "Content-Length:" . strlen($data) . "\r\n";
    $httpheader .= "Connection:close\r\n\r\n";
    //$httpheader .= "Connection:Keep-Alive\r\n\r\n";
    $httpheader .= $data;

    $fd = fsockopen($url_info['host'], 80);
    fwrite($fd, $httpheader);
    $gets = "";
    while (!feof($fd)) {
        $gets .= fread($fd, 128);
    }
    fclose($fd);
    if ($gets != '') {
        $start = strpos($gets, '<?xml');
        if ($start > 0) {
            $gets = substr($gets, $start);
        }
    }
    return $gets;
}
$post_data = 'aa=3333';
$target = 'http://localhost/lianxi/1xiaoyu/default1.php';
$res = SocketPost($post_data, $target);

var_dump($res);

php异步请求的几种方式:php异步请求的几种方式

猜你喜欢

转载自blog.csdn.net/weixin_38230961/article/details/82967464
今日推荐