选择语言 :

 Module_HttpClient_Driver_Fsock::_create

创建一个CURL对象

fsockopen() Module_HttpClient_Driver_Fsock::_create( string $url , int $timeout )

参数列表

参数 类型 描述 默认值
$url string URL地址
$timeout int 超时时间
返回值
  • fsockopen()
File: ./modules/httpclient/driver/fsock.class.php
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
protected function _create($url,$timeout)
{
    if (false===strpos($url, '://'))
    {
        preg_match('#^(http(?:s)?\://[^/]+/)#', $_SERVER["SCRIPT_URI"] , $m);
        $the_url = $m[1] . ltrim($url,'/');
    }
    else
    {
        $the_url = $url;
    }
 
    preg_match('#^(http(?:s)?)\://([^/]+)(/.*)$#', $the_url , $m);
    $hostname = $m[2];
    $uri      = $m[3];
 
    list($host, $port) = explode(':', $hostname, 2);
 
    if ($this->ip)
    {
        $host = $this->ip;
    }
 
    if ($m[1]=='https')
    {
        $host = 'tls://' . $host;
    }
 
    if (!$port)
    {
        if ($m[1]=='https')
        {
            $port = 443;
        }
        else
        {
            $port = 80;
        }
    }
 
    $ch = fsockopen($host, $port, $errno, $errstr, $timeout);
 
    $header = array
    (
        'Host'       => $hostname ,
        'Accept'     => 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
        'Connection' => 'close',
    );
 
    if ($this->cookies)
    {
        $header['Cookie'] = is_array($this->cookies)?http_build_query($this->cookies, '', ';'):$this->cookies;
    }
 
    if ($this->referer)
    {
        $header['Referer'] = $this->referer;
    }
 
    if ($this->agent)
    {
        $header['User-Agent'] = $this->agent;
    }
    elseif (array_key_exists('HTTP_USER_AGENT', $_SERVER))
    {
        $header['User-Agent'] = $_SERVER['HTTP_USER_AGENT'];
    }
 
    if ($this->header)
    {
        $header = array();
        foreach ($this->header as $item)
        {
            # 防止有重复的header
            if (preg_match('#(^[^:]*):(.*)$#', $item,$m))
            {
                $header[trim($m[1])] = trim($m[2]);
            }
        }
    }
 
    # 设置POST数据
    if ($this->_post_data)
    {
        $vars = (string)$this->_post_data[$the_url];
        $header['Content-Length'] = strlen($vars);
        $header['Content-Type']   = 'application/x-www-form-urlencoded';
    }
 
    $str = $this->method . ' ' . $uri . ' HTTP/1.1'."\r\n";
    foreach ($header as $k=>$v)
    {
        $str .= $k .' :' . str_replace(array("\r","\n"),'',$v) . "\r\n";
    }
    $str .= "\r\n";
 
    if ($this->_post_data)
    {
        // 追加POST数据
        $str .= $vars;
    }
 
    fwrite($ch,$str);
 
    return $ch;
}