Php 为什么需要两个请求来传输文件?
我使用下面的方法从API服务器下载文件。但是它不能正常工作,当用户单击调用getFile()方法的按钮时,它首先出现在错误页面中 正在尝试获取非对象的属性 但是,如果用户单击浏览器刷新按钮,文件将被下载 所以看来,对于要生成的文件,有必要发送两个请求并在第一个出现 正在尝试获取非对象的属性 第二个文件被传输,第三个文件再次出现 正在尝试获取非对象的属性 API中说下载文件是一个异步操作,这意味着文件可能不会立即准备好。我不知道这个问题是否就是因为这个 代码:Php 为什么需要两个请求来传输文件?,php,laravel,Php,Laravel,我使用下面的方法从API服务器下载文件。但是它不能正常工作,当用户单击调用getFile()方法的按钮时,它首先出现在错误页面中 正在尝试获取非对象的属性 但是,如果用户单击浏览器刷新按钮,文件将被下载 所以看来,对于要生成的文件,有必要发送两个请求并在第一个出现 正在尝试获取非对象的属性 第二个文件被传输,第三个文件再次出现 正在尝试获取非对象的属性 API中说下载文件是一个异步操作,这意味着文件可能不会立即准备好。我不知道这个问题是否就是因为这个 代码: 哪一行生成错误?请显示所有错误消息并
哪一行生成错误?请显示所有错误消息并在代码中标识该行。谢谢,错误是“$url=$response->output->pdfUrl;”中的“ErrorException(E_NOTICE)试图获取非对象的属性”。但是为了显示这个错误,代码没有“if(!empty($response)){”部分,它只需要“$response=json_decode($response);$url=$response->output->pdfUrl”;在“if(!empty($response)){”部分中,它在第一个请求中显示一个空白页面,刷新后传输文件。
public function getFile($regId){
$client = new \GuzzleHttp\Client();
$user = Auth::user();
$registration = $user->registrations()->with(["proforma"])->where("id", $regId)->first();
$proforma = $registration->proforma->proforma_number;
$getProforma = $client->request('GET', 'https://...'.$proforma.'.json', [
'query' => ['api_key' => '...'],
]);
$response = $getProforma->getBody()->getContents();
$url = null;
if(!empty($response)) {
$response = json_decode($response);
$url = !empty($response->output->pdfUrl) ? $response->output->pdfUrl : '';
}
header("Location: $url");
}
public function getFile($regId)
{
$registration = auth()->user()->registrations()->with(["proforma"])->where("id", $regId)->first();
$proforma = $registration->proforma->proforma_number;
$getProforma = json_decode(file_get_contents('https://...' . $proforma . '.json?api_key=.....'));
// $getProforma->output here was your problem so I advice you to: dd($getProforma);
$url = ! empty(optional($getProforma->output)->pdfUrl) ? optional($getProforma->output)->pdfUrl : '/'; // it will redict home if it couldn't find the PDF URL
return redirect($url);
}