How can I call a PHP function from another server? - php

Is there a way that I can call a PHP function from another server?
I have access to both these 2 servers. I want to call a PHP function from server 1 and use it in server 2.
What do I need to change on both servers to make it possible? Thanks!

You could use an API to do this. On Server 1 using Curl or GuzzleHttp you want to make an API call to Server 2
When Server 2 receives the request from Server 1 you can have the function run.
One thing to consider here is security, you will need a way of verifying that only Server 1 is making requests and not somebody who's figured out your API. Usually an API Key is passed when making the request and it's checked on the API.

Essentially what you're asking is how to do a remote procedure call (RPC) between two PHP applications. To communicate between two applications (PHP applications in your case) that are hosted on different servers you need to choose some kind of communication protocol.
The protocol you choose would depend on many things, including what your applications do, how often they would communicate, what kind of data you want to send between them, etc.
I'm no expert myself, but from what I know gRPC and REST are two of the more popular API communication protocols. See these articles for comparisons between the two. If your servers are going to be communicating back and forth very frequently you might want to consider WebSockets or MQTT.
For most of these protocols you're going to have to set up a service/server on the one server (the server on which your function is that you want to be called) and a client on the other (the server from which you want to call the function).
To set up gRPC, have a look at this PHP tutorial.
To set up REST, you would need to expose a HTTP endpoint on your server-host and consume the endpoint from your client-host. Exposing the endpoint is most easily done with an API framework of which there are many (just Google "PHP REST framework"). Consuming the endpoint can be done with a HTTP client, such as Guzzle, like some of the other answers mention.

Sorry this is not possible like sesssion if not same server session will not take effect on other pages.

Related

How web-services handle requests and responses, and how PHP could access this correctly?

I'm having troubles to figure out how web services handle requests and send responses, and how my site/module will handle it. So these are my questions and bellow a little about why i am asking this:
What is the right way to send a request to web service?
How a web-service will usually answer a site requesting?
How a PHP site can send a request to a web-service? Is cURL the right way?
I'm a student who are learning PHP and a lot of other things, and my job now is create a Joomla Module to show information from a web service (probably created in java or something, probably created by me, when i learn java...).
I know i will use http requests to talk with the web service, but i worry im using it wrong (making a request to the url, like the browser).
So, i did a little example site and a example API. The api uses Slim microframework to create routes, access my database and return the content in json (what means: if i access 'api.com/api/something' in my browser i see a plain white page with a lot of json). I pretend this is my web service.
The example site send a request to the API with cURL, reads the content and decode the json and do things. It seems forced to me.
I have not much support to understand web services and if i am in the right way (or far from this).
I would appreciate your help.
You are on the right track. A web service is simply a processing or storage facility intended to be accessed by some other program just like a database or fileserver service.
What is the right way to send a request to a web service
It depends. The most common implementations use SOAP or REST which define additional semantics on top of the HTTP protocol. SOAP uses a single URL as a gateway to the service and more specific selection of the functionality and the relevant data is embedded within an XML payload presented via POST. HTTP is merely a bearer for the message exchange. In REST, the HTTP part is integrated into the semantics of the transaction.
The URL identifies the location of the data (or processing function)
The payload contains only data, usually presented as POSTed JSON or XML,
further REST uses the HTTP verb (GET, POST, PUT, DELETE) to indicate the requested action
the HTTP headers are used to convey transaction meta-data.
How a web service will usually answer a request
I'm not sure what you are asking here. It should report back on the state of the request, any relevant error messages and possibly some data.
The speciifics would be unique to the API and documented.
Is cURL the right way?
While it is possible to do a lot with the HTTP wrappers functionality in PHP, libcurl offers an lot more flexibility. So, yes this it would be hard to implement a REST client without using cURL, OTOH a SOAP client is (usually) less complex at the HTTP tier but life is a lot simpler if you use a SOAP library/framework to abstract the more complex protocol.
For future questions please have one question per entry.
What is the right way to send a request to web service?
That really depends on the web service, what they require. It can be as simple as a short text string, to sending a XML formatted or JSON formatted array. You need to research the relevant web service.
How a web-service will usually answer a site requesting?
Again it depends on the web service. They are not the same. A web service will have documentation on what it expects and how it will respond.
How a PHP site can send a request to a web-service? Is cURL the right way?
Curl is a good method and is usually the method used from within PHP.

Node, React web-server with a PHP api server

When communicating between a Node/React web server and a PHP api server is it best practice to "proxy" all requests by the client (React) through the web server and not directly to the api server?
Im using universal (isomorphic) React rendered on the Node web server. It would also use the api server to build the initial page loads (isomorphic).
That's what I would do. Actually that's what i'm doing (with a java based backend/api). It allows you to hide the real structure of your api and also to eventually parse the result in the node server if needed. Also it allows you to adapt the request method to fit exactly your client services (if for example your api is not exactly sending the data the format and the way you want).
For me it seems to be a good practice (and I agree trying to close this question is crazy, this is a good question ...)

Get data from SAP CRM to PHP

I'm trying to automate getting(posting) data from SAP CRM using my server (cURL/PHP+MSSQL), but have some problems with it.
There is no access allowed except this web interface.
I tried to use cURL, using POST fields from FireBug, but it returned only logon error.
Questions:
Is there any solution or API?
Has anybody tried it?
Thanks.
Yes, there are APIs, though you'll have to be more specific about exactly what data you want in order to say if a particular API to serve your needs exists. SOAP web services are an option that may provide the information you need and there are PHP SOAP libraries. You'll need to talk to your SAP team to get access to the system, and you will probably want to be much more specific about the exact data you need access to.
If your company has Gateway, you can also define OData APIs. And it is, of course, possible to define your own RESTful (or other) HTTP APIs using SAP's HTTP stack. Again, you'll need to talk to your SAP team about this.

How do I use a server for mobile application?

I recently started to do development in the Haxe language with OpenFL (AS3 background).
But I have never worked on an app that communicates with a server - or never done any programming for servers for that matter!
I have to make a mobile app (for which I intend to use Haxe) where the new user creates an account on the server, and thus also interact withe other user accounts in a desired way.
So could someone guide me in the right direction to approach this situation? I'm guessing I will need to use PHP or ruby etc.
or can I use Haxe to program on the server? are there any good libraries that also provide security while making facility for user accounts? Is AWS or Google app engine something I can use?
check this simple but complete tutorial by filtreck
http://mromecki.fr/blog/post/haxite-writing-entire-website-using-haxe
You will want to create normal web pages that you can host on the server which will retrieve the needed information.
After having uploaded these, use a type of webview in the application to load the pages and retrieve this information.
You can write your server in Haxe if you want, and if you use a platform that supports it you could use TCP and haxe.remoting to pass data between the client and the server.
haxe.remoting is intended to make calling haxe function in a server from a client easier so that may be what you want.
If you don't feel confortable with using TCP you could do as Max wrote, just make some HTTP API (you can do this too in haxe) and do normal HTTP request from the client.

Securing a PHP REST Service

I am a complete newbie in this topic so please excuse me for my ignorance. Our company has a PHP based REST service created for consumption by an iPhone app. We make several POST calls to the REST API: www.mywebsite.com/api/rest and we send the method name and the parameters to the service. The service in turn returns a XML response which is GZip encoded. This is working fine.
I am assigned with a task to secure these call so that data that is getting passed to the service is not in plain text/is not visible using tools like Charles Proxy and other sniffing tools. In the past I have used some iPhone apps which also makes REST calls but they don't show up in such tools. I would like to know how to implement such a thing in a normal PHP based REST service so the data getting transmitted is not visible/ can be made secure.
I thought about using HTTPS but if we use HTTPS, I hear from others that the service/server will become slow and I am not too sure if the iPhone client will accept HTTPS calls.
Please advice.
Thanks in advance,
Senthil
It sounds like you're trying to secure the transport layer, and assuming that's the case then SSL/TLS is surely the way to go. Yes, chances are there will be some performance impact, but the security is probably worth it.

Categories