• Recent
  • Tags
  • Unsolved
  • Solved
  • MagicMirror² Repository
  • Documentation
  • 3rd-Party-Modules
  • Donate
  • Discord
  • Register
  • Login
MagicMirror Forum
  • Recent
  • Tags
  • Unsolved
  • Solved
  • MagicMirror² Repository
  • Documentation
  • 3rd-Party-Modules
  • Donate
  • Discord
  • Register
  • Login
A New Chapter for MagicMirror: The Community Takes the Lead
Read the statement by Michael Teeuw here.

call API (no CORS), used to do it with php proxy

Scheduled Pinned Locked Moved Development
12 Posts 3 Posters 10.8k Views 3 Watching
Loading More Posts
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D Offline
    doubleT Module Developer
    last edited by Dec 23, 2017, 9:11 PM

    With the old Mirror I had a module that called a changing price list from an API. There’s no CORS, so I used a php proxy file. Ajax would call the php with the URL to the API. The php echo’d the API content and the AJAX would grab the echo’d content.

    Now I understand I can’t use PHP? I saw someone mentioning node_helper in a similar context, but no further explanation.

    I’ve got this code:

    var proxy  = this.data.path + "myproxy.php?url=";
    //  source = "http://apisource.de/api/getPrices.php?id="
    var source = "http%3A%2F%2Fapisource.de%2Fapi%2FgetPrices.php%3Fid%3D";
    var id =  this.config.station;
    $.ajax({
        url: proxy + url + id,
        dataType: "json",
        success: function (json) {
            data = json.response.prices;
            return data;
        }
    });
    

    proxy.php (within php-tags, of course, the codebox won’t show them):

    $file = file_get_contents($_GET['url'], true);
    echo $file;
    

    This works in basic html, I tested it. I’m running the mirror in dev mode with npm start dev on my Windows 10 machine.

    I’d be really happy and grateful if someone could point me in the right direction on how to get the json from the API.

    Thx,
    Torben

    1 Reply Last reply Reply Quote 0
    • Y Offline
      yawns Moderator
      last edited by Dec 23, 2017, 9:29 PM

      Instead of using the XMLHttpRequest in your main module file you use request() in node_helper

      Take a look at this code: https://forum.magicmirror.builders/topic/5743/mmm-solartemp

      I’m sure you will understand the workflow. If not, just let us know what you have and where you are stuck

      1 Reply Last reply Reply Quote 2
      • D Offline
        doubleT Module Developer
        last edited by doubleT Dec 24, 2017, 12:24 AM Dec 24, 2017, 12:10 AM

        Thank you. I got a bit further. It took me a while to figure out that I had to close the session and run npm start dev again for node_helper to work.

        I found that strawberry 3.141’s answer in that topic helped me a lot to get node_helper running.

        I got to the module to notificate node_helper and I also got node_helper to talk back. What I’m having trouble with is the request():

        request({
        	url: "modules/station_price/proxytest.txt",
        	method: 'GET',
        }, function (error, response, body) {
        	if (!error && response.statusCode == 200) {
        		self.sendSocketNotification("MSG", body);
        	}
        	else {
        		self.sendSocketNotification("MSG", error + " // " + response);
        	}
        });
        
        Error: Invalid URI "proxytest.txt" // undefined
        

        I tried a few variations for the URI - what’s the path? Or maybe I’m just not seeing some basic mistake here.
        this.data.path obviously isn’t working within node_helper.

        And is there a documentation for request()? Can/should I use dataType: “json”?

        I got this all running on my old mirror and now I just feel like running into a wall over and over again.

        Y S 2 Replies Last reply Dec 24, 2017, 9:01 AM Reply Quote 0
        • Y Offline
          yawns Moderator @doubleT
          last edited by Dec 24, 2017, 9:01 AM

          @doubleT
          If you want to you can send some more details in the chat or by email (yawns@live.de) and I’ll have a look at it.

          1 Reply Last reply Reply Quote 0
          • S Offline
            strawberry 3.141 Project Sponsor Module Developer @doubleT
            last edited by Dec 24, 2017, 9:59 AM

            @doubleT you can only do requests to servers, so if you want to access your file like this, you have to name the uri similar to localhost:8080/station_price/public/proxytest.txt and piut the file in a public folder, as i remember the public folder in a module directory gets exposed by default to the express server of the magicmirror. But if you want to read a local file, i would rather use the fs module that comes natively with nodejs than request. the filesystem module allows you to read and write files on your harddrive https://nodejs.org/dist/latest-v8.x/docs/api/fs.html. Be aware of that there are synchronous and asynchronous functions to read a file

            Please create a github issue if you need help, so I can keep track

            1 Reply Last reply Reply Quote 1
            • D Offline
              doubleT Module Developer
              last edited by Dec 25, 2017, 12:36 AM

              Thank you, that sounds like it might be what I need, I’ll check that out.
              Hope this works with a php file that grabs and forwards the content of the API.

              S 1 Reply Last reply Dec 25, 2017, 7:42 AM Reply Quote 0
              • S Offline
                strawberry 3.141 Project Sponsor Module Developer @doubleT
                last edited by Dec 25, 2017, 7:42 AM

                @doubleT the fs module will just read the content of the file, if it needs to be executed you need a different solution.

                Please create a github issue if you need help, so I can keep track

                1 Reply Last reply Reply Quote 0
                • D Offline
                  doubleT Module Developer
                  last edited by doubleT Dec 26, 2017, 1:08 AM Dec 26, 2017, 12:57 AM

                  That’s what I thought.

                  I got it done thanks to this blog entry about calling PHP within node.js via Child Process Dependency. Actually, now that I know about this, it’s quite easy, you just have to have PHP installed and in your PATH (that took me a while …).

                  (relevant part of) node_helper.js

                  getData: function(payload) {
                       var self = this;
                       var runner = require("child_process");
                       var proxy = "modules/my_module/proxy.php";
                       var source = "http://apisource.de/api/getPrices.php?id=12300123";
                       runner.exec("php " + proxy + " " + source, function(err, data, stderr) {
                            var json = JSON.parse(data);
                            self.sendSocketNotification("MSG", json);
                       });
                  }
                  

                  proxy.php is nearly unchanged from my first post: (php tags not showing up)

                  $params = explode(",", $argv[1]);
                  $file = file_get_contents($params[0], true);
                  echo $file;
                  

                  A node child process is started, it uses PHP (has to be in the env. PATH) to open the file it is handed (proxy) and attaches the parameter “source” to that call. The proxy.php gets the contents of “source” and echo’s it.
                  The child process receives the echo’ed data, I parse it to JSON and send that back to the module.

                  It’s working, but let me know if you see any flaws or anything I could improve. Thanks a lot.

                  Oh, and no, there’s no way around that “file_get_contents => echo proxy” atm.

                  Torben

                  S 1 Reply Last reply Dec 30, 2017, 12:21 AM Reply Quote 1
                  • S Offline
                    strawberry 3.141 Project Sponsor Module Developer @doubleT
                    last edited by Dec 30, 2017, 12:21 AM

                    @doubleT personally i wouldn’t take php in the middle just to request the webiste, you could do that in the nodehelper directly

                    Please create a github issue if you need help, so I can keep track

                    1 Reply Last reply Reply Quote 2
                    • D Offline
                      doubleT Module Developer
                      last edited by Dec 30, 2017, 12:35 AM

                      The API doesn’t allow CORS, so JS can’t get to it.

                      S 1 Reply Last reply Dec 30, 2017, 8:33 AM Reply Quote 0
                      • 1
                      • 2
                      • 1 / 2
                      1 / 2
                      • First post
                        1/12
                        Last post
                      Enjoying MagicMirror? Please consider a donation!
                      MagicMirror created by Michael Teeuw.
                      Forum managed by Sam, technical setup by Karsten.
                      This forum is using NodeBB as its core | Contributors
                      Contact | Privacy Policy