Cross-domain getJson request with Simple PHP proxy

非 Y 不嫁゛ 提交于 2019-12-14 02:28:34

问题


Hi I'm trying to get data from an api that ONLY returns JSON rather than JSONP. Every time I try to get the data connecting as JSONP it doesn't work because I am not expecting JSON. So, I guess I have to use a PHP proxy to get past the cross-domain issue so I can interpret the JSON request. [Simple PHP Proxy][1] is one that I'm trying to use but I am having a hell of a time trying to get the most basic functionality to work. When I type the url I want to query into his example on the example page, it works. When I do it, it doesn't, sometimes I get a "Missing Command" (the API's way of telling me that there are too many or too little arguements) or a httpcode/jsoncode 404.

Expensify is the API I am querying. I am first trying the Authenticate command. IF you type this URL in your browser, you get the results I want: https://api.expensify.com?command=Authenticate&partnerName=applicant&partnerPassword=d7c3119c6cdab02d68d9&partnerUserID=expensifytest%40mailinator.com&partnerUserSecret=hire_me

Using the Simple PHP Proxy my request URL turns out to be: http://people.rit.edu/~cjs6948/exp/ba-simple-proxy.php?url=https://api.expensify.com?command=Authenticate&partnerName=applicant&partnerPassword=d7c3119c6cdab02d68d9&partnerUserID=expensifytest%40mailinator.com&partnerUserSecret=hire_me

Here is my very simple jQuery code:

var proxy = 'ba-simple-proxy.php';
url = proxy + '?' + "url=" + "https://api.expensify.com?    command=Authenticate&partnerName=applicant&partnerPassword=d7c3119c6cdab02d68d9&partnerUserID=expensifytest%40mailinator.com&partnerUserSecret=hire_me";
$.getJSON( url, function(data){});

"ba-simple-proxy.php" can be found [here][3]. Does anyone have any experience trying to connect to a cross-domain API via a proxy? Maybe even this one? Have any better ideas? Any help is appreciated, thanks.


回答1:


You need to encode the URL as the parameter "partnerName" will be passed to your ba-simple-proxy.php script and not the api.expensify.com website. I do believe the function you're looking for is: encodeURI(uri)



来源:https://stackoverflow.com/questions/10675241/cross-domain-getjson-request-with-simple-php-proxy

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!