well...just about all I needed to say...lookie here, I'm a total noob in web programming so go easy on me..and oh...if ever it is...could you point me to a simple example? i've worked wiht cURL on a payment API (Authorize.net) but I can't seem to get the picture of how it applies to web services
cURL is a library used to retrieve remote URLs.
There are plenty of other ways to do this in PHP, for instance using file_get_contents() but cURL offers much more flexibility.
It doesn't do anything special with web services - all it does is request URLs. Depending on the type of service you're using you might want to look at the PEAR SOAP module, PHP's SOAP classes or PHP's XML-RPC functions.
you can use curl to fetch a url (usually with a post request), and use the data that was fetched in you application.
curl operates as a client or useragent, in much the same way as requesting data from a website would be done from a web browser.
if you are doing strictly get requests there are other simpler was then curl, but curl gives a lot more flexibility, such as reading headers, raw data, etc
Related
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.
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.
I am curious to know about different ways of implementing web services in PHP? Is there other ways except cURL and SOAP? How SOAP is different than cURL? Is there any similarities also between them?
Thanks,
ursitesion
You can implement a web service in PHP with SoapServer or with PHP - return JSON creating a REST interface. There are other ways, such as XML-RPC, but I usually end using either SOAP or REST, I prefer REST whenever possible.
Curl is a tool that you can use to consume a web service, it's not used to create a web service.
REST or SOAP reading that might help.
Probably best to think of cURL as just a way to send a request (eg GET,POST) to another server.
There are alternatives to cURL, including just straight file_get_contents()
Edit : replaced a rotted link. Also, having used SOAP quite a lot since this first reply, I'd add that another main reason you'd prefer SOAP to REST would be if you have to absolutely, positively double check that every transaction was received, else have the opportunity to resend it. No doubt you can do this with a suitably designed REST webservice, but that would need careful design and testing.
I would like to know what existing PHP libraries/classes have you used for your REST+JSON projects.
If I need to create a wrapper for social engines API such as Facebook and/or Twitter API, what existing PHP REST JSON architecture do you recommend?
JSON CONSUMING DEVICE (not PHP based) <--> PHP REST SERVICE <--> SOCIAL API
If all you need is a RESTful client, then I don't really see why you need a framework at all.
You can use e.g. the curl libraries to do an HTTP request (or even plain sockets), and then json_decode & json_encode for the JSON. That's it. That's your client right there. REST is not more than that.
I'm sure there are wrappers around PHP's curl library that are a little bit more intuitive for the RESTful "I just want to GET/POST/PUT/DELETE some data" approach, but then again it's at most 20 lines of code to roll a class that does that yourself.
Zend Framework has a REST library available, with client and server, as well as the REST route and controller. It also has context switching available, so it's easy to switch output to XML, JSON, or whatever you want if you extend it. There is also a Twitter service available, although I haven't tried it.
I have been approached to create a website using Sabre Web Services to power the reservations system. All documentation I have seen refers to .NET or Java solutions, and I was in doubt as to whether PHP can be used, as access is performed using SOAP.
I have found no further information about this, and I assume the answer is yes, but I wonder why there is not a single reference to this being possible. All solutions seem to be .NET!
SOAP is language independent, which means that any language can communicate with the web service if it can generate SOAP requests and handle responses.
PHP's SOAP documentation can be found in the php manual
PHP can be used to call SOAP pretty effectively.
There's a very good tutorial on devzone on how you can use SOAP well.
I've just done a little digging around and it looks like you can use stream_context_create() to create a custom HTTP stream context. This would include the HTTP content type header you need. The resource returned from this function call can then be passed to the SoapClient constructor to be used in SOAP calls. Have a look at http://ca.php.net/stream_context_create and the PHP manual page for the SoapClient constructor (sorry, I can only post one link as a new user) for more information.
Yes, PHP can be be used to connect to SOAP web services - take a look at NuSOAP. It allows a nice & easy object oriented way to consume web services.