我试图通过curl从网站上获取信息。使用class API
{
function get_curl_content($url) {
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_HEADER, false);
$result = curl_exec($curl);
curl_close($curl);
return $result;
}
const CACHE_KEY = 'rate';
public static function getRate() {
if (Cache::has(self::CACHE_KEY)) {
return Cache::get(self::CACHE_KEY);
}
$oneHourTimestamp = \Carbon\Carbon::now()->addHours(1);
$tenMinutesTimestamp = \Carbon\Carbon::now()->addMinutes(10);
$result = $this->get_curl_content('https://example.com/');
$rates = json_decode($result, TRUE);
if (!$rates) {
Cache::put(self::CACHE_KEY, '-', $tenMinutesTimestamp);
return '-';
}
$Rate = @$rates['1']['2'];
if (!$Rate) {
Cache::put(self::CACHE_KEY, '-', $tenMinutesTimestamp);
return '-';
}
Cache::put(self::CACHE_KEY, $Rate, $oneHourTimestamp);
return $Rate;
}
}
不是一个选项,因为在服务器上已禁用。所以目前的来源就是这个
roduction.ERROR: Symfony\Component\Debug\Exception\FatalErrorException: Uncaught TypeError: Argument 1 passed to Illuminate\Exception\PlainDisplayer::display() must be an instance of Exception, instance of Error given, called in /var/www/vendor/laravel/framework/src/Illuminate/Exception/Handler.php on line 280 and defined in /var/www/vendor/laravel/framework/src/Illuminate/Exception/PlainDisplayer.php:15
当我执行此操作时,我在laravel.log中遇到错误
<?php
function get_curl_content($url) {
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_HEADER, false);
$result = curl_exec($curl);
curl_close($curl);
return $result;
}
$urldata = get_curl_content('https://example.com/');
echo $urldata;
这里有什么问题?
更新
{{1}}
我只把它放在单独的文件中,所有内容都完美显示。