Accessing a remote API that requires a whitelisted referring domain name via Cordova

随声附和 提交于 2019-12-08 09:21:11

问题


I have a general use question before I go down a road that might not be possible. I want to build an app that can interface with an API (read data and update date) on an external website. The external site offers two methods to connect to the API, a client and server set of API calls. The client method requires whitelisting the referring domain and the server method requires whitelisting the IP of the referring site.

If I'm building a regular webpage and want to connect to the API then if HOST.com is the site I want to connect TO and CLIENT.com is the site I want to connect FROM, then I would whitelist CLIENT.com in the setup files of HOST.com.

Alternatively, if I want to access HOST.com via the server API's (like php) then I need to whitelist CLIENT.com's website IP in the setup files of HOST.com.

My question is, is this something that can be done within Cordova? I obviousy won't be able to whitelist an IP of the device, but can the device pass the a referring domain to the HOST.com so it passes the whitelist?

Or am I going to have to setup some kind of webapp hosted in the cloud that the app interfaces with and that hosted app then accesses HOST.com with a whitelisted domain (or even via Server API and a whitelisted IP).

I realize the generality of this question, but would be helpful with any feedback. Want to point out this is NOT about whitelisting on the Cordova app, this is a server configuration on the remote server. If CLIENT.com is not whitelisted then CLIENT.com can NOT access HOST.com Thanks in advance.


回答1:


You can make anything with simple ajax call. If you have PHP knowledge you can use this to send to another url(for example some data):

[PHP - called with ajax]

    $postdata = http_build_query(
        array(
            'name' => $name,
            'surname' => $surname
        )
    );

    $opts = array('http' =>
        array(
            'method'  => 'POST',
            'header'  => 'Content-type: application/x-www-form-urlencoded',
            'content' => $postdata
        )
    );

    $context  = stream_context_create($opts);
    //call some api
    $result = file_get_contents('http://someurl.com/write.php?key=2q2q3q&name='.$name.'&surname='.$surname.', false, $context);

if($context) {
  echo "success"; // ajax returned
} else {
  echo "error"; // ajax returned
} 

So after ajax call (updating or something), you can send that data also on other host.com.



来源:https://stackoverflow.com/questions/50752918/accessing-a-remote-api-that-requires-a-whitelisted-referring-domain-name-via-cor

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