简体   繁体   中英

Webservices in PHP

Am a stranger to web services and I need to integrate web services into an existing application. The details to be passed to the web services are

  • Unique reference
  • Amount
  • Status
  • Status Description

This web service will update the website on the status of a transaction.

what is the best way to start? where do i go from here?

While there are many different types of web services and modes of integrating them into anything, many services these days are integrated with similar steps. Since you posed the question in abstract form, I'll offer an abstracted answer — maybe it'll trigger further questions.

I'll use payments as an example.

  1. User does something on a machine (orders a t-shirt). Their actions update a database, session, or some sort of storage (or chain of actions).

  2. If appropriate, one of the results of the users action (1) will also trigger the system to push content to a web service (like PayPal) . Often formatting in XML, you'll send a response with nested parameters. Your store would send the t-shirt price and quantity.

  3. The service receives the XML request (often containing an 'API Key' or such) and processes it. In PayPal's case after the user completes the t-shirt order, it will redirect you back to your own website, along with order information. (synchronous)

  4. The service optionally completes processing (or performing some asynchronous action) and then sends an XML request to your server. You parse the request in PHP and then process the data. For instance, the request may have a property like 'order_status' . The property could contain 'complete','pending', 'failed' . You could then use PHP to update your database to reflect this.

  5. Your web site reflects the changes made to the database by the service to the user. For instance, information on an 'Order Status' or 'Recent Orders' page.

There are nice libraries and many previously created PHP classes for many different web services. You could even try searching GitHub. And: UPS, PayPal, Google Checkout, Facebook, and many others have examples and API documentation to write your own implementations if you so desire/require.

Edit: PayPal-specific information When PayPal sends the (asynchronous) IPN response, it is in POST data, which you can read and validate according to their specs.

The easiest way I found to handle IPN (to avoid the tinker time) is Micah Carrick's PayPal class . I disliked the naming and wanted to polish the waiting screen (just in case), you can see my changes on this gist .

restful ajax driven webservices in PHP are almost trivially simple to write, esp if you use jQuery. You have code on the server like:

// index.php:
if (isset($_GET['transact'])) {
    $ref = $_POST['ref'];
    $amount = $_POST['amount'];
    $status = $_POST['status'];
    $description = $_POST['description'];
    $result = transact($ref, $amount, $status, $description);

    header('Content-Type: application/json');
    return json_encode(array(status => 'success', result => $result));
}

and your client-side web app could be:

// webapp.js
....
var data = { ref:4343, amount:'12.34', status:'pending', description:'deduction' };
$.post('?transact', data, function(result) {
    if (result.status == 'success') {
        var thing = result.thing; //etc..

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM