file_get_contents,无法打开流:已达到重定向限制
file_get_contents, failed to open stream: Redirection limit reached
我的代码是:
if ( isset($_POST['gd_url']) && isset($_POST['accessToken'])) {
$url = "https://drive.google.com/uc?export=download&id=".$fileId;
// $url = $_POST['gd_url'];
$name = $_POST['file'];
$accessToken = $_POST['accessToken'];
$opts = array(
'http'=>array(
'method'=>"GET",
'header' => "Authorization: Bearer " . $accessToken
)
);
$context = stream_context_create($opts);
$content = file_get_contents($url, false, $context);
// echo $content;
if (!empty($content)){
file_put_contents($_SERVER['DOCUMENT_ROOT'].'/UPDRIVE/'.$name,$content);
}
}
我尝试添加
'follow_location' => false
和 'max_redirects' => 101
但结果相同
错误:
Warning: file_get_contents(https://drive.google.com/uc?export=download&id=0B6ijT2xI7CfebEt3d3g1dndtZlU): failed to open stream: Redirection limit reached, aborting in /.../uploadMediasSupdrive.php on line 24
如果我换线
$content = file_get_contents($url, false, $context);
到
$content = file_get_contents($url);
这只适用于 *.jpg 文件
谢谢
$param = $_REQUEST;
$oAuthToken = $param['oAuthToken'];
$fileId = $param['fileId'];
$getUrl = 'https://www.googleapis.com/drive/v2/files/' . $fileId . '?alt=media';
$authHeader = 'Authorization: Bearer ' . $oAuthToken;
ini_set('memory_limit', '512M');
$ch = curl_init();
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_URL, $getUrl);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
$authHeader
));
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
$data = curl_exec($ch);
$error = curl_error($ch);
curl_close($ch);
file_put_contents($param['name'], $data);
这是我找到的解决方案
由于其他解决方案对我不起作用,我找到了另一个解决方案。
在 $opts 数组中,在 'http' 下我添加了 'follow_location' = false.
$opts = [
'http' => [
'follow_location' => false
]
];
问题解决了。
我建议添加这个 HTTP 上下文选项 max_redirects
:
见:https://www.php.net/manual/en/context.http.php
$arrContextOptions = array(
"ssl" => array(
// skip error "Failed to enable crypto" + "SSL operation failed with code 1."
"verify_peer" => false,
"verify_peer_name" => false,
),
// skyp error "failed to open stream: operation failed" + "Redirection limit reached"
'http' => array(
'max_redirects' => 101,
'ignore_errors' => '1'
),
);
$file = file_get_contents($file_url, false, stream_context_create($arrContextOptions));
显然,我只是将它用于在我的本地环境中进行快速调试。 不用于生产。
我的代码是:
if ( isset($_POST['gd_url']) && isset($_POST['accessToken'])) {
$url = "https://drive.google.com/uc?export=download&id=".$fileId;
// $url = $_POST['gd_url'];
$name = $_POST['file'];
$accessToken = $_POST['accessToken'];
$opts = array(
'http'=>array(
'method'=>"GET",
'header' => "Authorization: Bearer " . $accessToken
)
);
$context = stream_context_create($opts);
$content = file_get_contents($url, false, $context);
// echo $content;
if (!empty($content)){
file_put_contents($_SERVER['DOCUMENT_ROOT'].'/UPDRIVE/'.$name,$content);
}
}
我尝试添加
'follow_location' => false
和 'max_redirects' => 101
但结果相同
错误:
Warning: file_get_contents(https://drive.google.com/uc?export=download&id=0B6ijT2xI7CfebEt3d3g1dndtZlU): failed to open stream: Redirection limit reached, aborting in /.../uploadMediasSupdrive.php on line 24
如果我换线
$content = file_get_contents($url, false, $context);
到
$content = file_get_contents($url);
这只适用于 *.jpg 文件
谢谢
$param = $_REQUEST;
$oAuthToken = $param['oAuthToken'];
$fileId = $param['fileId'];
$getUrl = 'https://www.googleapis.com/drive/v2/files/' . $fileId . '?alt=media';
$authHeader = 'Authorization: Bearer ' . $oAuthToken;
ini_set('memory_limit', '512M');
$ch = curl_init();
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_URL, $getUrl);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
$authHeader
));
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
$data = curl_exec($ch);
$error = curl_error($ch);
curl_close($ch);
file_put_contents($param['name'], $data);
这是我找到的解决方案
由于其他解决方案对我不起作用,我找到了另一个解决方案。 在 $opts 数组中,在 'http' 下我添加了 'follow_location' = false.
$opts = [
'http' => [
'follow_location' => false
]
];
问题解决了。
我建议添加这个 HTTP 上下文选项 max_redirects
:
见:https://www.php.net/manual/en/context.http.php
$arrContextOptions = array(
"ssl" => array(
// skip error "Failed to enable crypto" + "SSL operation failed with code 1."
"verify_peer" => false,
"verify_peer_name" => false,
),
// skyp error "failed to open stream: operation failed" + "Redirection limit reached"
'http' => array(
'max_redirects' => 101,
'ignore_errors' => '1'
),
);
$file = file_get_contents($file_url, false, stream_context_create($arrContextOptions));
显然,我只是将它用于在我的本地环境中进行快速调试。 不用于生产。