JQuery美元。来自PHP file_get_contents的蒸汽社区JSON

时间:2021-10-11 09:51:03

I try to use http://steamcommunity.com/inventory/XXXXXXXXXX/753/6 to fetch JSON items.

我尝试使用http://steamcommunity.com/inventory/xxxxxxxxxxxx/753/6来获取JSON项目。

I can't put the url in my $.getJson because there is a CORS « Access-Control-Allow-Origin » error. How can i bypass that if i don't want to allow CORS I read somewhere that it's possible to use PHP file_get_contents to redirect the json file.

我不能把url放在$中。getJson因为有一个CORS«Access-Control-Allow-Origin»错误。如果我不想让我在某个地方读到的CORS允许使用PHP file_get_contents重定向json文件,我怎么能绕过它呢?

Steamcommunity doesn't work with JSONP

蒸汽社区与JSONP不合作。

var exercice = {

modules: {}
};

exercice.modules.ajax = (function () {

return {

    recupererJson: function () {
         initial= $.getJSON('proxy.php',function(data){
        })
            initial.done(function(donnes){
                var i = 0;

                $.each(donnes.descriptions,function(key,value){
                        $('tbody').append("<tr>");
                        $('tbody').append("<td>"+value.name+"</td>");
                        $('tbody').append("<td><img src=http://cdn.steamcommunity.com/economy/image/"+value.icon_url+" alt ="+value.name+"/></td>");
                        $('tbody').append("</tr>");


                    });
                    });
    },
    init: function () {
        exercice.modules.ajax.recupererJson();
    }
}})();

$(document).ready(function () {
exercice.modules.ajax.init();
});

Can someone help me ?

谁能帮帮我吗?

1 个解决方案

#1


1  

If php.ini has allow_url_fopen=1 (http://php.net/manual/en/filesystem.configuration.php), which should be by default, then you can make such a php file:

如果php。ini有allow_url_fopen=1 (http://php.net/manual/en/filesystem.configur.php),默认情况下应该是这样的,然后您可以创建这样一个php文件:

<?php echo file_get_contents('http://steamcommunity.com/inventory/76561198033103987/753/6');

or

<?php readfile('http://steamcommunity.com/inventory/76561198033103987/753/6');

Otherwise if it is disabled and you cannot access php.ini, but you have CURL extension enabled, then you can do so:

否则,如果它被禁用,并且您不能访问php。ini,但是你启用了旋度扩展,你可以这样做:

<?php
$ch = curl_init();
curl_setopt($ch, 'http://steamcommunity.com/inventory/76561198033103987/753/6');
curl_setopt($ch, CURLOPT_HEADER, 0);
echo curl_exec($ch);
curl_close($ch);

See http://php.net/manual/en/function.curl-init.php

参见http://php.net/manual/en/function.curl-init.php

EDIT: Looking at your code it seems to me that there is only one issue here:

编辑:看你的代码,我觉得这里只有一个问题:

initial= $.getJSON('proxy.php',function(data){

should be

应该是

initial= $.getJSON('http://example.com/proxy.php',function(data){

You should use full URLs.

您应该使用完整的url。

EDIT2: I tried this code and it worked fine for me.

我试过这个代码,它对我来说很有用。

http://127.0.0.1/test.html:

http://127.0.0.1/test.html:

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8" />
    <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
    <script type="text/javascript">
        var exercice = {
            modules: {}
        };

        exercice.modules.ajax = (function () {

        return {

            recupererJson: function () {
                 initial= $.getJSON('http://127.0.0.1/proxy.php',function(data){
                })
                    initial.done(function(donnes){
                        var i = 0;

                        $.each(donnes.descriptions,function(key,value){
                                $('tbody').append("<tr>");
                                $('tbody').append("<td>"+value.name+"</td>");
                                $('tbody').append("<td><img src=http://cdn.steamcommunity.com/economy/image/"+value.icon_url+" alt ="+value.name+"/></td>");
                                $('tbody').append("</tr>");


                            });
                            });
            },
            init: function () {
                exercice.modules.ajax.recupererJson();
            }
        }})();

        $(document).ready(function () {
            exercice.modules.ajax.init();
        });
    </script>
</head>
<body>
    <table>
        <tbody>
        </tbody>
    </table>
</body>
</html>

http://127.0.0.1/proxy.php:

http://127.0.0.1/proxy.php:

<?php
header("Content-Type: application/json; charset=utf-8");
header("Access-Control-Allow-Origin: *");
readfile('http://steamcommunity.com/inventory/76561198033103987/753/6');

I added Content-Type header so that the proxy.php would form a more browser-friendly. Also Access-Control-Allow-Origin: * header should prevent CORS from blocking the ajax request if you open the test.html using file URI file:///C:/www/test.html.

我添加了内容类型头,以便代理。php将形成一个更友好的浏览器。如果您打开测试,那么还应该防止CORS阻塞ajax请求。html使用文件URI文件:///C:/www/test.html。

#1


1  

If php.ini has allow_url_fopen=1 (http://php.net/manual/en/filesystem.configuration.php), which should be by default, then you can make such a php file:

如果php。ini有allow_url_fopen=1 (http://php.net/manual/en/filesystem.configur.php),默认情况下应该是这样的,然后您可以创建这样一个php文件:

<?php echo file_get_contents('http://steamcommunity.com/inventory/76561198033103987/753/6');

or

<?php readfile('http://steamcommunity.com/inventory/76561198033103987/753/6');

Otherwise if it is disabled and you cannot access php.ini, but you have CURL extension enabled, then you can do so:

否则,如果它被禁用,并且您不能访问php。ini,但是你启用了旋度扩展,你可以这样做:

<?php
$ch = curl_init();
curl_setopt($ch, 'http://steamcommunity.com/inventory/76561198033103987/753/6');
curl_setopt($ch, CURLOPT_HEADER, 0);
echo curl_exec($ch);
curl_close($ch);

See http://php.net/manual/en/function.curl-init.php

参见http://php.net/manual/en/function.curl-init.php

EDIT: Looking at your code it seems to me that there is only one issue here:

编辑:看你的代码,我觉得这里只有一个问题:

initial= $.getJSON('proxy.php',function(data){

should be

应该是

initial= $.getJSON('http://example.com/proxy.php',function(data){

You should use full URLs.

您应该使用完整的url。

EDIT2: I tried this code and it worked fine for me.

我试过这个代码,它对我来说很有用。

http://127.0.0.1/test.html:

http://127.0.0.1/test.html:

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8" />
    <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
    <script type="text/javascript">
        var exercice = {
            modules: {}
        };

        exercice.modules.ajax = (function () {

        return {

            recupererJson: function () {
                 initial= $.getJSON('http://127.0.0.1/proxy.php',function(data){
                })
                    initial.done(function(donnes){
                        var i = 0;

                        $.each(donnes.descriptions,function(key,value){
                                $('tbody').append("<tr>");
                                $('tbody').append("<td>"+value.name+"</td>");
                                $('tbody').append("<td><img src=http://cdn.steamcommunity.com/economy/image/"+value.icon_url+" alt ="+value.name+"/></td>");
                                $('tbody').append("</tr>");


                            });
                            });
            },
            init: function () {
                exercice.modules.ajax.recupererJson();
            }
        }})();

        $(document).ready(function () {
            exercice.modules.ajax.init();
        });
    </script>
</head>
<body>
    <table>
        <tbody>
        </tbody>
    </table>
</body>
</html>

http://127.0.0.1/proxy.php:

http://127.0.0.1/proxy.php:

<?php
header("Content-Type: application/json; charset=utf-8");
header("Access-Control-Allow-Origin: *");
readfile('http://steamcommunity.com/inventory/76561198033103987/753/6');

I added Content-Type header so that the proxy.php would form a more browser-friendly. Also Access-Control-Allow-Origin: * header should prevent CORS from blocking the ajax request if you open the test.html using file URI file:///C:/www/test.html.

我添加了内容类型头,以便代理。php将形成一个更友好的浏览器。如果您打开测试,那么还应该防止CORS阻塞ajax请求。html使用文件URI文件:///C:/www/test.html。