没事的时候自己封装了一个socket类
功能很简单和curl功能是一样的
随着电子商务模式更加多样化,企业和个人的迫切需求,PHPShops多用户商城系统正可以为其提供专业的电子商务解决方案。社区化电子商务,主要面向行业类和地方门户类站点。 PHPShops多用户商城系统(简称PHPShops)是基于电子商务的一套平台交易系统,它采用目前最流行网站建设工具PHP+MYSQL,实现模版分离技术,通过HTML交互式网页技术来实行客户端与服务器端的交流。无论在
0
class socketClass{
private $host;
private $url;
private $error_no = 0;
public $error_str = '';
private $port = 80;
private $timeout = 5;
private $method;
private $param;
private $fp;
private $content = '';
private $header = array();
public function __construct($host, $port, $url, $param, $method='GET', $timeout=5) {
$this->host = $host;
$this->port = $port;
$this->url = $url;
$this->timeout = $timeout;
$this->method = $method;
if(is_array($param)){
$this->param = http_build_query($param);
}else{
$this->param = $param;
}
}
private function connect(){
$this->fp = fsockopen($this->host, $this->port, $this->error_no, $this->error_str, $this->timeout);
if(!$this->fp){
$this->error_str = 'socket connect failed';
return false;
}
return true;
}
private function send(){
$this->fp = '';
if(!$this->connect()){
$this->error_str = 'connect socket failed';
return false;
}
if($this->method == 'POST'){
$header = "POST $this->url HTTP/1.1\r\n";
}else{
if(!empty($this->param)){
$header = "GET $this->url?$this->param HTTP/1.1\r\n";
}else{
$header = "GET $this->url HTTP/1.1\r\n";
}
}
$header .= "Content-Length:" . strlen($this->param) . "\r\n";
$header .= "Host:$this->host:$this->port\r\n";
$header .= "Content-Type:application/x-www-form-urlencode\r\n";
$header .= "Connection:close\r\n\r\n";
if($this->method == 'POST'){
$header .= "$this->param\r\n\r\n";
}
if(!empty($this->header)){
foreach ($this->header as $key=>$val){
$header .= "$key:$val\r\n";
}
}
fwrite($this->fp, $header);
while(!feof($this->fp)){
$this->content .= fread($this->fp, 1024);
}
fclose($this->fp);
return true;
}
public function getContent(){
if(!$this->send()){
return false;
}
$pos = strpos($this->content, "\r\n\r\n");
$this->content = substr($this->content, $pos);
return $this->content;
}
public function setHeader($key, $val){
$this->header[$key] = $val;
}
public function getErrorStr(){
return $this->error_str;
}
}
$host = 'localhost';
$port = 81;
$url = 'http://localhost/phpdemo/php/socket_accept.php';
$param = array('username'=>'beggar', 'passwd'=>'20060400');
$method = 'GET';
$timeout = 10;
$http = new socketClass($host, $port, $url, $param, $method, $timeout);
$http->setHeader('referer', $url);
$http->setHeader('Accept', '*/*');
$header = $http->getContent();
echo $header;以上就介绍了PHP socket类,包括了方面的内容,希望对PHP教程有兴趣的朋友有所帮助。
PHP怎么学习?PHP怎么入门?PHP在哪学?PHP怎么学才快?不用担心,这里为大家提供了PHP速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号