get json using php

前端 未结 3 1605
遇见更好的自我
遇见更好的自我 2020-12-06 10:09

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

I know how to do it in jav

相关标签:
3条回答
  • 2020-12-06 10:35

    Take a look at 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.

    Since PHP 5.2.0 the function json_decode is part of the core.

    0 讨论(0)
  • 2020-12-06 10:38

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

    <?
        $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_RETURNTRANSFER which sends the response into the return value (and returns false on fail).

    0 讨论(0)
  • 2020-12-06 10:41

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

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

    0 讨论(0)
提交回复
热议问题