使用PHP在指定的URL上收集图像并将其存储到数据库中

Adi*_*PHP 0 php curl screen-scraping image scraper

通常,我希望输入一个URL,然后将该URL的图像导入数据库.

这里有一些代码让我很接近,但欢迎使用替代品.

如果我尝试将$ image作为BLOB存储到数据库中,则会出现错误.

<?php



class wSpider
{
    var $ch; /// going to used to hold our cURL instance
    var $html; /// used to hold resultant html data
    var $binary; /// used for binary transfers
    var $url; /// used to hold the url to be downloaded

    function wSpider()
    {
        $this->html = "http:/";
        $this->binary = 0;
        $this->url = "";
    }


    function fetchPage($url)
    {
        $this->url = $url;
        if (isset($this->url)) {
            $this->ch = curl_init(); /// open a cURL instance
            curl_setopt ($this->ch, CURLOPT_RETURNTRANSFER, 1); // tell cURL to return the data
            curl_setopt ($this->ch, CURLOPT_URL, $this->url); /// set the URL to download
            curl_setopt($this->ch, CURLOPT_FOLLOWLOCATION, true); /// Follow any redirects
            curl_setopt($this->ch, CURLOPT_BINARYTRANSFER, $this->binary); /// tells cURL if the data is binary data or not
            $this->html = curl_exec($this->ch); // pulls the webpage from the internet
            curl_close ($this->ch); /// closes the connection
        }
    }
}

$mySpider = new wSpider(); //// creates a new instance of the wSpider
$mySpider->binary = 1; /// turns on the binary transfer mode
$image = $mySpider->fetchPage("http://static.php.net/www.php.net/images/php.gif");

?>
Run Code Online (Sandbox Code Playgroud)

dyn*_*mic 5

你不需要那些东西.

你可以做: $image = file_get_contents($url);

然后 pdo::prepare("INSERT INTO img (?, ?)");