Beispiel #1
0
 static function socket($url, $data)
 {
     //解析url
     if (!is_array($url)) {
         $url = parse_url($url);
     }
     if (!isset($url["port"])) {
         $url['port'] = 80;
     }
     //打开socket
     $fp = fsockopen($url['host'], $url['port'], $error_no, $error_info, 30);
     if (!$fp) {
         return 'error:(' . $error_no . ')' . $error_info;
     }
     //组装发送数据
     if (is_array($data)) {
         $data = fun::arr_str($data);
     }
     $data = trim($data);
     //构造头部信息
     $head = 'POST ' . $url['path'] . " HTTP/1.0\r\n";
     $head .= 'Host: ' . $url['host'] . "\r\n";
     $head .= 'Referer: http://' . $url['host'] . $url['path'] . "\r\n";
     $head .= "Content-type: application/x-www-form-urlencoded\r\n";
     $head .= 'Content-Length: ' . strlen($data) . "\r\n\r\n";
     $head .= $data;
     //接收并返回结果
     $write = fputs($fp, $head);
     while (!feof($fp)) {
         $info = fgets($fp);
     }
     return json_decode($info, true);
 }