国产av日韩一区二区三区精品,成人性爱视频在线观看,国产,欧美,日韩,一区,www.成色av久久成人,2222eeee成人天堂

Home 類庫下載 PHP類庫 How to use socket to get remote pictures in php

How to use socket to get remote pictures in php

Oct 14, 2016 am 10:46 AM

Steps:

1, match the host name and file part in the URL

2, create socket and connect to the target server

3, construct HTTP request and send

4, read HTTP response and parse

5, save Content to file and close the socket connection


The code is implemented as follows:

<?php
/*
 * 使用socket獲取遠(yuǎn)程資源(網(wǎng)頁,圖片等)
 * url 資源URL
 * savepath 資源的保存路徑
 * return true/false
 */
function get_remote_picture($url,$savepath="./"){
    set_time_limit(0);
    $pattern = &#39;/(http:\/\/)?([^\/]+)(.+)/&#39;;
    $res = preg_match($pattern, $url, $matches);
    if($res == 0){
        return false;
    }
    $host = "";//主機(jī)名
    $file = "";//請(qǐng)求的文件
    if(count($matches) == 3){
        $host = $matches[1];
        $file = $matches[2];
    }else if(count($matches) == 4){
        $host = $matches[2];
        $file = $matches[3];
    }else{
        return false;
    }
    $socket = socket_create(AF_INET,SOCK_STREAM,SOL_TCP);
    $res = socket_connect($socket,gethostbyname($host),80);
    if(!$res){
        //echo socket_strerror(socket_last_error($socket));
        socket_close($socket);
        return false;
    }
    $request = "";
    $request .= "GET $file HTTP/1.1\r\n";
    $request .= "Host: $host\r\n";
    $request .= "Connection: close\r\n\r\n";
    $len = socket_write($socket,$request);
 
    $response = "";
    while($buf=socket_read($socket,512)){
        if(strlen($buf) == 0){
            break;
        }
        $response .= $buf;
    }
    if(strpos($response,"\r\n\r\n")){
        $arr = explode("\r\n\r\n",$response);
        if(!file_exists($savepath)){
            @mkdir($savepath);
        }
        $savepath = rtrim($savepath,&#39;/&#39;).&#39;/&#39;;
        file_put_contents($savepath.basename($file),$arr[1]);
    }else{
        socket_close($socket);
        return false;
    }
    socket_close($socket);
    return true;
}
 
/* 獲取百度logo */
$url = "http://su.bdimg.com/static/superplus/img/logo_white.png";
$result = get_remote_picture($url);
if($result){
    echo &#39;get remote picture success&#39;;
}else{
    echo &#39;get remote picture failed&#39;;
}


Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undress AI Tool

Undress AI Tool

Undress images for free

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

PHP Tutorial
1500
276