首页 > 解决方案 > 试图从 URL 中提取 json,但不起作用

问题描述

我正在尝试从 PHP 中的 URL 中提取产品的 json 文件。我有以下代码,但它不工作。有人可以告诉我哪里出错了吗?

function getJson() {
$request = file_get_contents('https://3rdParty.guntrader.uk/ShootingSuppliesLtd/jsonGuns');
$json = json_decode($request);
return $json;
};

echo $json;

未定义的变量:第 36 行 C:\Users\darry\dev\VVV\www\ssheadless\importJson.php 中的 json

标签: php

解决方案


你没有调用方法getJson()。如果不调用方法,则无法访问返回值。调用方法。

function getJson() {
    $request = file_get_contents('https://3rdParty.guntrader.uk/ShootingSuppliesLtd/jsonGuns');
    $json = json_decode($request);
    return $json;
};

echo getJson();

推荐阅读