download() public method

Download
public download ( $url, $mixed_filename ) : boolean
$url
$mixed_filename
return boolean
Beispiel #1
0
 /**
  * Create user cache.
  *
  * @param string $user Twitter screen name
  * @return bool If successful true
  * @throws CacheException Any errors
  */
 public function create($user)
 {
     try {
         $path = config("cache.path") . "/{$user}";
         $json_file = "{$path}/data.json";
         if (!$this->exists($user)) {
             mkdir($path);
         }
         if (!file_exists($json_file)) {
             touch($json_file);
         }
         $response = $this->twistOAuth->get("users/show", ["id" => $user]);
         $image_url = str_replace("_normal", "", $response->profile_image_url_https);
         $info = new SplFileInfo($image_url);
         $curl = new Curl();
         $curl->setUserAgent(SaveTweet::APP_NAME . "/" . SaveTweet::APP_VERSION . " with PHP/" . PHP_VERSION);
         $curl->download($image_url, "{$path}/icon.{$info->getExtension()}");
         $curl->close();
         $data = ["name" => $response->name, "screen_name" => $response->screen_name, "id" => $response->id_str, "icon" => config("cache.http_path") . "/{$response->id_str}/icon.{$info->getExtension()}", "org_icon" => $image_url, "update_at" => time()];
         $json = json_encode($data, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE);
         (new SplFileObject($json_file, "w"))->fwrite($json);
         return true;
     } catch (RuntimeException $e) {
         $this->_catch($e);
     }
 }
Beispiel #2
0
 /**
  * Get news item, including title, publish time,
  * description, content, images and keywords.
  *
  * @param  string $url
  *
  * @return array
  */
 public function getNews($url)
 {
     $dom = $this->dom($url);
     //标题
     $title = $dom->find('h1', 0)->getPlainText();
     //发布时间
     $time = $dom->find('.post_time_source', 0)->getPlainText();
     $time = strtotime(strstr($time, ' 来源', true));
     //描述
     $desc = $dom->find('.post_desc', 0)->getPlainText();
     $desc = trim($desc);
     //内容
     $body = $dom->find('#endText');
     $content = '';
     foreach ($body as $ele) {
         $content .= $ele->innerHtml();
     }
     //下载图片
     $path = '';
     if ($url = $dom->find('.post_text p img', 0)->getAttr('src')) {
         $path = preg_replace('~http://.*?/sports/~', '', $url);
         $dir = dirname($path);
         if (!is_dir($dir)) {
             mkdir($dir, 0777, true);
         }
         $this->curl->download($url, $path);
     }
     return compact('title', 'time', 'desc', 'content', 'path');
 }
 public function testDownloadCallback()
 {
     // Upload a file.
     $upload_file_path = Helper\get_png();
     $upload_test = new Test();
     $upload_test->server('upload_response', 'POST', array('image' => '@' . $upload_file_path));
     $uploaded_file_path = $upload_test->curl->response->file_path;
     // Download the file.
     $callback_called = false;
     $curl = new Curl();
     $curl->setHeader('X-DEBUG-TEST', 'download_response');
     $curl->download(Test::TEST_URL . '?' . http_build_query(array('file_path' => $uploaded_file_path)), function ($instance, $fh) use(&$callback_called) {
         PHPUnit_Framework_Assert::assertFalse($callback_called);
         PHPUnit_Framework_Assert::assertInstanceOf('Curl\\Curl', $instance);
         PHPUnit_Framework_Assert::assertTrue(is_resource($fh));
         PHPUnit_Framework_Assert::assertEquals('stream', get_resource_type($fh));
         PHPUnit_Framework_Assert::assertGreaterThan(0, strlen(stream_get_contents($fh)));
         PHPUnit_Framework_Assert::assertEquals(0, strlen(stream_get_contents($fh)));
         PHPUnit_Framework_Assert::assertTrue(fclose($fh));
         $callback_called = true;
     });
     $this->assertTrue($callback_called);
     // Remove server file.
     $this->assertEquals('true', $upload_test->server('upload_cleanup', 'POST', array('file_path' => $uploaded_file_path)));
     unlink($upload_file_path);
     $this->assertFalse(file_exists($upload_file_path));
     $this->assertFalse(file_exists($uploaded_file_path));
 }
<?php

require '../src/Curl/Curl.php';
use Curl\Curl;
$curl = new Curl();
$curl->download('https://php.net/images/logos/php-med-trans.png', '/tmp/php-med-trans.png');
Beispiel #5
0
<?php

require __DIR__ . '/vendor/autoload.php';
use Curl\Curl;
$curl = new Curl();
$curl->progress(function ($client, $download_size, $downloaded, $upload_size, $uploaded) {
    if ($download_size === 0) {
        return;
    }
    $percent = floor($downloaded * 100 / $download_size);
    echo ' ' . $percent . '%' . "\r";
});
$curl->download('https://secure.php.net/distributions/manual/php_manual_en.html.gz', '/tmp/php_manual_en.html.gz');
Beispiel #6
0
 public function download($uid, $url, $filename = NULL, $ext = NULL)
 {
     if (empty($url)) {
         return static::DOWNLOAD_ERR_URL;
     }
     ignore_user_abort(TRUE);
     set_time_limit(0);
     $file_path = tempnam(storage_path('utils'), 'download-');
     $curl = new Curl();
     $curl->setOpt(CURLOPT_BINARYTRANSFER, TRUE);
     $curl->setOpt(CURLOPT_SSL_VERIFYPEER, false);
     $curl->setOpt(CURLOPT_SSL_VERIFYHOST, false);
     //$curl->setOpt(CURLOPT_NOSIGNAL, 1); //cURL毫秒就报错的BUG http://www.laruence.com/2014/01/21/2939.html
     $curl->download($url, $file_path);
     if ($curl->error) {
         return static::DOWNLOAD_ERR_FILE;
     }
     $download_filename = $curl->responseHeaders['Content-Disposition'];
     $basename = mb_basename($url);
     //pathinfo($url,PATHINFO_BASENAME);
     if (!empty($download_filename)) {
         preg_match('/filename\\s*=\\s*(\\S*)/i', $download_filename, $tmp);
         !empty($tmp) && ($basename = mb_basename(trim($tmp[1], '\'"')));
         //pathinfo($download_filename,PATHINFO_BASENAME);
     }
     return $this->savefile($uid, $file_path, $basename, $filename, $ext, 'Download from:' . $url);
 }
<?php

require __DIR__ . '/vendor/autoload.php';
use Curl\Curl;
$curl = new Curl();
$curl->download('https://php.net/images/logos/php-med-trans.png', function ($instance, $tmpfile) {
    $save_to_path = '/tmp/' . basename($instance->url);
    $fh = fopen($save_to_path, 'wb');
    stream_copy_to_stream($tmpfile, $fh);
    fclose($fh);
});
<?php

require __DIR__ . '/vendor/autoload.php';
use Curl\Curl;
$start_url = 'https://php.net/images/logos/php-med-trans.png';
$final_url = 'https://secure.php.net/images/logos/php-med-trans.png';
$curl = new Curl();
$curl->setOpt(CURLOPT_FOLLOWLOCATION, true);
$curl->download($start_url, '/tmp/php-med-trans.png');
assert($final_url === $curl->effectiveUrl);
Beispiel #9
0
<?php

/**
 * Created by PhpStorm.
 * User: lee
 * Date: 15-12-18
 * Time: 下午3:37
 */
require __DIR__ . "/../vendor/autoload.php";
use Curl\Curl;
$curl = new Curl();
$url = 'https://ss0.bdstatic.com/70cFvHSh_Q1YnxGkpoWK1HF6hhy/it/u=1070902365,2619384777&fm=116&gp=0.jpg';
$filename = 'example.jpg';
print_r($curl->download($url, $filename));
 /**
  * Imports stations information from EDDB
  */
 public function actionStations()
 {
     $eddbApi = Yii::$app->params['eddb']['archive'] . 'stations.json';
     $time = date('d-m-y_h');
     $file = Yii::getAlias('@app') . '/runtime/stations' . $time . '.json';
     // Download the data from EDDB if the data that we have is out of date
     if (!file_exists($file)) {
         $this->stdOut('Systems data from EDDB is out of date, fetching RAW JSON from EDDB');
         $curl = new Curl();
         $curl = new Curl();
         $curl->setOpt(CURLOPT_ENCODING, 'gzip');
         $curl->download($eddbApi, $file);
     }
     $bench = new \Ubench();
     $result = $bench->run(function ($file, $type) {
         $this->importJsonData($file, $type);
     }, $file, 'stations');
     $this->stdOut("Systems import completed\n");
     $this->stdOut($bench->getTime(false, '%d%s') . "\n");
     $this->stdOut($bench->getMemoryPeak(false, '%.3f%s') . "\n");
     $this->stdOut($bench->getMemoryUsage() . "\n");
 }
 public function testDownloadRange()
 {
     // Create and upload a file.
     $filename = Helper\get_png();
     $uploaded_file_path = Helper\upload_file_to_server($filename);
     $filesize = filesize($filename);
     foreach (array(false, 0, 1, 2, 3, 5, 10, 25, 50, $filesize - 3, $filesize - 2, $filesize - 1, $filesize - 0, $filesize + 1, $filesize + 2, $filesize + 3) as $length) {
         $source = Test::TEST_URL;
         $destination = Helper\get_tmp_file_path();
         // Start with no file.
         if ($length === false) {
             $this->assertFalse(file_exists($destination));
             // Start with $length bytes of file.
         } else {
             // Simulate resuming partially downloaded temporary file.
             $partial_filename = $destination . '.pccdownload';
             if ($length === 0) {
                 $partial_content = '';
             } else {
                 $file = fopen($filename, 'rb');
                 $partial_content = fread($file, $length);
                 fclose($file);
             }
             // Partial content size should be $length bytes large for testing resume download behavior.
             if ($length <= $filesize) {
                 $this->assertEquals($length, strlen($partial_content));
                 // Partial content should not be larger than the original file size.
             } else {
                 $this->assertEquals($filesize, strlen($partial_content));
             }
             file_put_contents($partial_filename, $partial_content);
             $this->assertEquals(strlen($partial_content), strlen(file_get_contents($partial_filename)));
         }
         // Download (the remaining bytes of) the file.
         $curl = new Curl();
         $curl->setHeader('X-DEBUG-TEST', 'download_file_range');
         $curl->download($source . '?' . http_build_query(array('file_path' => $uploaded_file_path)), $destination);
         clearstatcache();
         $expected_bytes_downloaded = $filesize - min($length, $filesize);
         $bytes_downloaded = $curl->responseHeaders['content-length'];
         if ($length === false || $length === 0) {
             $expected_http_status_code = 200;
             // 200 OK
             $this->assertEquals($expected_bytes_downloaded, $bytes_downloaded);
         } elseif ($length >= $filesize) {
             $expected_http_status_code = 416;
             // 416 Requested Range Not Satisfiable
         } else {
             $expected_http_status_code = 206;
             // 206 Partial Content
             $this->assertEquals($expected_bytes_downloaded, $bytes_downloaded);
         }
         $this->assertEquals($expected_http_status_code, $curl->httpStatusCode);
         if (!$curl->error) {
             $this->assertEquals($filesize, filesize($destination));
             unlink($destination);
             $this->assertFalse(file_exists($destination));
         }
     }
     // Remove server file.
     Helper\remove_file_from_server($uploaded_file_path);
     unlink($filename);
     $this->assertFalse(file_exists($filename));
 }
Beispiel #12
0
            $dirPath = "download/" . $obj->articleId . '/';
            $displayFile = $downloadFiles[$key]['displayFile'];
            $linkFile = $downloadFiles[$key]['linkFile'];
            //file_put_contents($myLog,$dirPath.$linkFile);
            if (!file_exists($dirPath . $linkFile)) {
                //file_put_contents($myLog,"file is empty");
                if (!file_exists($dirPath)) {
                    mkdir($dirPath, 0777);
                }
                $downloadPath = $downloadBaseUrl . $obj->articleId . '/' . $linkFile;
                //$downlaodFileName = "download/".$obj->articleId."/".$displayFile ;
                //file_put_contents($myLog,$downloadPath);
                $fileSaveAs = "download/" . $obj->articleId . '/' . $linkFile;
                $agent = new Curl();
                $agent->setOpt(CURLOPT_ENCODING, 'gzip');
                $agent->download($downloadPath, $fileSaveAs);
                $agent->close();
                /*
                		$wget = '/usr/bin/wget';
                		//$downloadPath = "http://163.17.39.135/data/school/board/2912/1452829198_1-2016_01_15.pdf";
                		$cmd = "$wget --directory-prefix=$dirPath $downloadPath ";
                	exec($cmd);
                		//file_put_contents("download/".$obj->articleId."/".$displayFile, fopen("$myLog,$downloadBaseUrl.$linkFile", 'r'));
                */
            }
        }
        //foreach($downloadFiles as $key=>$file)
    }
    //end if
    print $curl->response;
}
Beispiel #13
0
 /**
  *
  */
 public function install()
 {
     // read configuration file
     $this->configuration = json_decode(file_get_contents($this->configurationFilepath), true);
     if (null == $this->configuration) {
         throw new \Exception('Configuration file contains invalid JSON.');
     }
     if (0 < count($this->configuration['version'])) {
         $requirements = array();
         foreach ($this->configuration['version'] as $version => $versionEntry) {
             $requirements = array_merge($requirements, $this->getRequirements($versionEntry['require']));
         }
         // set folder to install requirements
         if (isset($this->configuration['knowledge-directory'])) {
             $folderForRequirements = $this->defaultRootFolder . $this->configuration['knowledge-directory'] . '/';
         } else {
             $folderForRequirements = $this->defaultRootFolder . 'knowledge/';
         }
         // if there are requirements to install, create knowledge directory first
         if (0 < count($requirements) && false == file_exists($folderForRequirements)) {
             $fileObject = new File($folderForRequirements);
             $fileObject->mkdirs();
         }
         $nodeUtils = new NodeUtils();
         $curl = new Curl();
         $curl->setOpt(CURLOPT_ENCODING, 'gzip');
         $curl->setOpt(CURLOPT_FOLLOWLOCATION, true);
         foreach ($requirements as $name => $requirement) {
             // if a valid local file was given
             if (file_exists($requirement['file'])) {
                 $fileObject = new File($requirement['file']);
                 $fileObject->copy($targetPath);
                 // if a valid URL was given
             } elseif ($nodeUtils->simpleCheckURI($requirement['file'])) {
                 // split name to be able to create all folders
                 $name = explode('/', $name);
                 $vendor = $name[0];
                 $project = $name[1];
                 // remove all maybe existing files
                 if (file_exists($folderForRequirements . $vendor . '/' . $project . '.ttl')) {
                     $fileObject = new File($folderForRequirements . $vendor . '/' . $project . '.ttl');
                     $fileObject->delete();
                 }
                 if (false == file_exists($folderForRequirements . $vendor)) {
                     $fileObject = new File($folderForRequirements . $vendor);
                     $fileObject->mkdirs();
                 }
                 echo PHP_EOL . '- Download ' . $vendor . '/' . $project;
                 if (isset($requirement['file-format'])) {
                     $fileFormat = $requirement['file-format'];
                 } else {
                     $fileFormat = $this->getFileFormat($requirement['file']);
                 }
                 if (null !== $fileFormat) {
                     $curl->download($requirement['file'], $folderForRequirements . $vendor . '/' . $project . '.' . $fileFormat);
                     if ('xml' == $fileFormat) {
                         $fileFormatForParsing = 'rdf-xml';
                     } elseif ('ttl' == $fileFormat) {
                         $fileFormatForParsing = 'turtle';
                     } elseif ('n3' == $fileFormat || 'nt' == $fileFormat) {
                         $fileFormatForParsing = 'n-triples';
                     } else {
                         $fileFormatForParsing = $fileFormat;
                     }
                     if (isset($this->configuration['target-file-serialization']) && $this->configuration['target-file-serialization'] != $fileFormatForParsing) {
                         // get parser suiteable for the given file format
                         $parserFactory = new ParserFactory(new NodeFactoryImpl(), new StatementFactoryImpl());
                         $parser = $parserFactory->createParserFor($fileFormatForParsing);
                         if (null == $parser) {
                             echo ' - Unknown file format given: ' . $fileFormatForParsing . '; Leaving file at : ' . $fileFormat;
                             continue;
                         }
                         // parse file content and transform it into a statement
                         $statementIterator = $parser->parseStreamToIterator($folderForRequirements . $vendor . '/' . $project . '.' . $fileFormat);
                         /* go through iterator and output the first few statements
                                                     $i = 0;
                                                     foreach ($statementIterator as $statement) {
                                                         echo (string)$statement->getSubject()
                                                             . ' ' . (string)$statement->getPredicate()
                                                             . ' ' . (string)$statement->getObject()
                                                             . PHP_EOL;
                         
                                                         if ($i++ == 10) { break; }
                                                     }
                         
                                                     continue;*/
                         // get serializer for target file format
                         $serializerFactory = new SerializerFactory(new NodeFactoryImpl(), new StatementFactoryImpl());
                         $targetFormatForSerialization = $this->configuration['target-file-serialization'];
                         if ('rdf-xml' == $targetFormatForSerialization) {
                             $serializedFileFormat = 'xml';
                         } elseif ('turtle' == $targetFormatForSerialization) {
                             $serializedFileFormat = 'ttl';
                         } elseif ('n-triples' == $targetFormatForSerialization) {
                             $serializedFileFormat = 'n3';
                         } else {
                             $serializedFileFormat = $targetFormatForSerialization;
                         }
                         $targetFile = 'file://' . $folderForRequirements . $vendor . '/' . $project . '.' . $serializedFileFormat;
                         $serializer = $serializerFactory->createSerializerFor($targetFormatForSerialization);
                         $serializer->serializeIteratorToStream($statementIterator, fopen($targetFile, 'w'));
                         if (file_exists($targetFile)) {
                             unlink($folderForRequirements . $vendor . '/' . $project . '.' . $fileFormat);
                         }
                     }
                     echo ' - done';
                 } else {
                     echo ' - unknown file format for the ontology reference: ' . $requirement['file'];
                 }
             }
         }
         echo PHP_EOL;
     } else {
         return 'No version information found. Did you added elements to version array?';
     }
 }