Secure API calls with AJAX and PHP to 3rd party API

前端 未结 6 463
小鲜肉
小鲜肉 2021-01-11 18:02

I want to make GET, POST & PUT calls to a 3rd party API and display the response on the client side via AJAX. The API calls require a token, but I need to keep that toke

6条回答
  •  悲&欢浪女
    2021-01-11 18:28

    Because all you want is to add token to http headers, which i am assuming is Authorization a simple way would be to implement a proxy server that makes calls to your api endpoint after adding up those. A sample file for nginx would be

    location /apiProxy {
        proxy_pass http://www.apiendPoint.com/;
        proxy_set_header Authorization ;
    }
    

    This is a much more smarter approach rather than writing a program and gets you off with 4 lines of code. Make sure to change your parameters accordingly and add other parameters as needed by api client you are using. The only difference on javascript side would be to use the location url rather than one provided by service which acts as a proxy.

    Edit

    The configuration for apache would be

    NameVirtualHost *
    
       
          ProxyPass http://www.apiendPoint.com/
          ProxyPassReverse http://www.apiendPoint.com/
          Header add Authorization ""
          RequestHeader set Authorization ""   
       
    
    

提交回复
热议问题