如何将file_get_contents转换为cURL

问题描述 投票:-1回答:1

虽然我正在使用file_get_contents,但是idk为什么我总是会出错。我听说cURL不会得到那个错误。

这是我想要转换为cURL的代码

<?php
$details1=json_decode(file_get_contents("http://2strok.com/download/download.json"));
 $details2=json_decode(file_get_contents($details1->data));
 header("Location: ".$details2->data); ?>

这个代码在localhost上没有任何问题,但是当我在我的Web服务器(由one.com托管)上执行它时,它不起作用。它显示了这个错误:

警告:file_get_contents(url):无法打开流:HTTP请求失败! HTTP / 1.1 401未经授权

php curl
1个回答
0
投票

您仍然可以获得401卷曲,但您可以尝试以下方法

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://2strok.com/download/download.json");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$output = curl_exec($ch);
curl_close($ch);

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, json_decode($output)->data);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$output = curl_exec($ch);
curl_close($ch);

header("Location: ".json_decode($output)->data);

如果您愿意,可以重复使用卷曲手柄

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://2strok.com/download/download.json");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$output = curl_exec($ch);
curl_setopt($ch, CURLOPT_URL, json_decode($output)->data);
$output = curl_exec($ch);
curl_close($ch);

header("Location: ".json_decode($output)->data)
© www.soinside.com 2019 - 2024. All rights reserved.