使用 php 获取 json

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2100272/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 05:01:40  来源:igfitidea点击:

get json using php

phpjson

提问by Klanestro

I need to get the json data from, http://vortaro.us.to/ajax/epo/eng/+ 'word'+ "/?callback=?" working example (not enough reputation)

我需要从http://vortaro.us.to/ajax/epo/eng/+ 'word'+ "/?callback=?"获取 json 数据 工作示例(没有足够的声誉)

I know how to do it in javascript, But I need my php file to get this data, It needs to be server side, Thanks I'm new I have spent all day trying to figure this out. fopen and fread isn't working,

我知道如何在 javascript 中做到这一点,但我需要我的 php 文件来获取这些数据,它需要在服务器端,谢谢我是新手,我花了一整天的时间试图解决这个问题。fopen 和 fread 不起作用,

<?php
$vorto = $_GET['vorto']; // Get the Word from Outer Space and Search for it!

if (isset($vorto))
    {
    echo $vorto;
    } else {
        $Help = "No Vorto -> add ?vorto=TheWordYouWant to the end of this website";
        echo $Help;
    }
$url1 = "http://vortaro.us.to/ajax/epo/eng/"; 
$url2 = "/?callback=?";
$finalurl= $url1 . $vorto . $url2;

/*
PLEASE HELP

$v1 = fopen($finalurl ,"r");
echo $v1;


$frv1 = fread($v1,filesize($v1));
echo $frv1 ;

*/

?>

回答by Berzemus

file_get_contents()can be used on a URL. A simple and convenient way to handle http page download.

file_get_contents()可用于 URL。一种处理http页面下载的简单方便的方法。

That done, you can use json_decode()to parse the data into something useful.

完成后,您可以使用json_decode()将数据解析为有用的内容。

回答by DrDol

Take a look at PHP Curl.

看看PHP Curl

With this example you are able to the all the informations.

通过此示例,您可以了解所有信息。

<?php
// create a new cURL resource
$ch = curl_init();

// set URL and other appropriate options
curl_setopt($ch, CURLOPT_URL, "http://www.example.com/");
curl_setopt($ch, CURLOPT_HEADER, 0);

// grab URL and pass it to the browser
curl_exec($ch);

// close cURL resource, and free up system resources
curl_close($ch);
?>

Make sure that PHP Curl is enables in your php.ini. If you want to use fopen the setting allow_url_fopen must be 'ON' in your php.ini. Checkout phpinfo() for all the settings.

确保在您的 php.ini 中启用了 PHP Curl。如果您想使用 fopen,则 php.ini 中的设置 allow_url_fopen 必须为“ON”。检查 phpinfo() 以获取所有设置。

Since PHP 5.2.0 the function json_decodeis part of the core.

自 PHP 5.2.0 起,函数json_decode是核心的一部分。

回答by Chris

Old question, but still one of the top hits in Google, so here is my contribution on top of @DrDol's answer.

老问题,但仍然是谷歌的热门话题之一,所以这是我在@DrDol 回答基础上的贡献。

<?
    $url = "http://www.example.com/api/v1/endpoint";
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 

    $data = json_decode(curl_exec($ch));                

    curl_close($ch);
?>

Note the user of the CURLOPT_RETURNTRANSFERwhich sends the response into the return value (and returns false on fail).

请注意CURLOPT_RETURNTRANSFER将响应发送到返回值中的用户(并在失败时返回 false)。