PHP Socket mock HTTP request ____php

Source: Internet
Author: User
Tags set socket urlencode

function Http_request ($url, $type = "Get", $post _data = NULL)
{
$type = Strtoupper ($type);

$http _info = Array ();
$url 2 = Parse_url ($url);
if ($socket = Socket_create (Af_inet, Sock_stream, sol_tcp)) < 0)
{
return false;
}
Set Socket timeout
Socket_set_option ($socket, Sol_socket, So_sndtimeo, Array ("SEC" =>1, "USEC" =>0));
$url 2["Path" = ($url 2["path"] = = ""? /": $url 2[" path "]); $url 2["Port" = ($url 2["port"] = = ""? : $url 2["Port"]);
$host _ip = http_get_host_ip ($url 2["host");
if ($result = Socket_connect ($socket, $host _ip, $url 2["Port")) < 0)
{
Socket_close ($socket);
return false;
}


$request = $url 2["path"]. ($url 2["query"]!= ""? ". $url 2[" Query ":" "). ($url 2["fragment"]!= ""? " #" . $url 2["Fragment"]: "");
if ($type = = "Get")
{//get method
$in = "Get". $request. "Http/1.1\r\n";
$in. = "Accept: */*\r\n";
$in. = "user-agent:lowell-agent\r\n";
$in. = "Host:". $url 2["Host"]. "\ r \ n";
$in. = "connection:close\r\n\r\n";
if (!socket_write ($socket, $in, strlen ($in))
{
Socket_close ($socket);
return false;
}
Unset ($in);
}
else if ($type = = "POST")
{//post method
Build Post Data
$needChar = false;
foreach ($post _data as $key => $val)
{
$post _data2. = ($needChar? "&": ""). UrlEncode ($key). "=" . UrlEncode ($val);
$needChar = true;
}
$in = "POST". $request. "Http/1.1\r\n";
$in. = "Accept: */*\r\n";
$in. = "Host:". $url 2["Host"]. "\ r \ n";
$in. = "user-agent:lowell-agent\r\n";
$in. = "content-type:application/x-www-form-urlencoded\r\n";
$in. = "Content-length:". strlen ($post _data2). "\ r \ n";
$in. = "connection:close\r\n\r\n";
$in. = $post _data2. "\r\n\r\n";
unset ($post _data2);
if (! @socket_write ($socket, $in, strlen ($in))
{
Socket_close ($socket);
return false;
}
Unset ($in);
}
Else
{//unknowd method
Trigger_error ("Unknowd method", E_user_error);
Exit
}
Process response
$out = "";
while ($buff = @socket_read ($socket, 2048))
{
$out. = $buff;
}
Finish socket
Socket_close ($socket);
$pos = Strpos ($out, "\r\n\r\n");
$head = substr ($out, 0, $pos); HTTP Head
$status = substr ($head, 0, Strpos ($head, "\ r \ n")); HTTP status Line
$body = substr ($out, $pos + 4, strlen ($out)-($pos + 4)); Page body
if (Preg_match ("/^http\/\d\.\d\s" ([\d]+) \s.*$/, $status, $matches))
{
if (Intval ($matches [1])/100 = 2)
{
return $body;
}else{
return false;
}
}else{
return false;
}
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.