【问题标题】:Is there an alternative to file_get_contents?是否有 file_get_contents 的替代方法?
【发布时间】:2023-03-21 22:13:01
【问题描述】:

是否有 file_get_contents 的替代方法?这是我遇到问题的代码:

if ( !$img = file_get_contents($imgurl) ) { $error[] = "Couldn't find the file named $card.$format at $defaultauto"; }
else {
    if ( !file_put_contents($filename,$img) ) { $error[] = "Failed to upload $filename"; }  
    else { $success[] = "All missing cards have been uploaded"; }
}

我尝试使用 cURL,但无法完全弄清楚如何完成此操作。任何帮助表示赞赏!

【问题讨论】:

  • 查看this answer 了解curl_get_contents() 函数。

标签: php curl file-get-contents


【解决方案1】:

file_get_contents 有很多替代方案,我在下面发布了几个替代方案。

打开

function fOpenRequest($url) {
   $file = fopen($url, 'r');
   $data = stream_get_contents($file);
   fclose($file);
   return $data;
}
$fopen = fOpenRequest('https://www.example.com');// This returns the data using fopen.

卷曲

function curlRequest($url) {
   $c = curl_init();
   curl_setopt($c, CURLOPT_URL, $url);
   curl_setopt($c, CURLOPT_RETURNTRANSFER, true);
   $data = curl_exec($c);
   curl_close($c);
   return $data;
}
$curl = curlRequest('https://www.example.com');// This returns the data using curl.

您可以将这些可用选项之一与存储在变量中的数据一起使用,以执行您需要的操作。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-11-06
    • 1970-01-01
    • 2012-05-28
    • 2022-01-01
    • 1970-01-01
    • 2015-11-14
    • 1970-01-01
    相关资源
    最近更新 更多