How to Apply SOAP API in WordPress Without Using Any Plugin ?

In today’s digital landscape, integrating APIs is crucial for enhancing the functionality of your website. This guide will teach you how to implement a SOAP API in WordPress without relying on plugins. By opting for a custom integration approach, you’ll have more control and flexibility over your API interactions.

 

Why Avoid Plugins?

 

While plugins are often a convenient way to add features to WordPress, there are several reasons why you might choose to avoid them:

 

  • Performance Impact: Plugins can slow down your website, affecting load times and user experience.
  • Bloat: Many plugins come with unnecessary features that can clutter your site and make it harder to manage.
  • Security Risks: Relying on plugins introduces potential vulnerabilities, as they may not always be updated or secure.
  • Loss of Control: Plugins limit your ability to fully customize features, as they rely on predefined settings.
  • Future Costs: While many plugins are free today, they could become chargeable in the future, leading to additional expenses we may not want to incur.
  • Custom Functionality: Some plugins may not provide the specific functionality required, whereas custom code allows for tailored solutions.

 

By integrating SOAP APIs directly into WordPress, you can create a lightweight, secure solution tailored to your specific needs.

 

What is SOAP API?

 

SOAP (Simple Object Access Protocol) is a protocol used for exchanging structured information in web services. It enables communication between different systems over the internet, making it possible for applications to interact with one another. SOAP is highly structured, relying on XML, and is widely used for its reliability and security in enterprise applications.

 

Implementing SOAP API in WordPress

 

Let’s walk through the steps to implement a SOAP API in WordPress without using any plugin.

 

1. Using functions.php

 

To start, you'll want to create a function in your theme's functions.php file.

 

function call_tadawul_api() {
    $url = 'soap api url'; // Add your SOAP API URL here

    // SOAP XML body
    $xml_body = '
   
   
   
       
            Company Id
            Security Key
       

   

   
';

    // Setting up the headers for SOAP
    $headers = array(
        'Content-Type' => 'text/xml; charset=utf-8',
        'SOAPAction'   => '' // You might need to set the SOAPAction URL here based on the API documentation
    );

    // Send the POST request
    $response = wp_remote_post($url, array(
        'method'    => 'POST', 
        'body'      => $xml_body,
        'headers'   => $headers,
        'timeout'   => 45,
    ));

    // Check if there was an error
    if (is_wp_error($response)) {
        $error_message = $response->get_error_message(); 
        return 'Something went wrong: ' . $error_message;
    } else {
        // Parse the response
        $response_body = wp_remote_retrieve_body($response);

        // Clean up the response for easier parsing
        $soap_response_clean = preg_replace('/<(\/?) (\w+):([^>]*>)/', '<$1$2$3', $response_body);

        // Load the XML response as an object
        $xml_object = simplexml_load_string($soap_response_clean);

        // Convert the XML object to JSON
        $json = json_encode($xml_object);

        // Convert the JSON to an associative array
        $array = json_decode($json, true);

        // Extract the data you want to display
        if (isset($array['Body']['getDetailQuoteForCompanyResponse']['getDetailQuoteForCompanyReturn'])) {
            $company_data = $array['Body']['getDetailQuoteForCompanyResponse']['getDetailQuoteForCompanyReturn'];

            // Output the data in an HTML table
            $output = '

';
            $output .= '';
            $output .= '';
            $output .= '';
            $output .= '';
            $output .= '
FieldValue
Company ID' . $company_data['id'] . '
Company Name' . $company_data['companyName'] . '
Last Trade Price' . $company_data['lastTradePrice'] . '
';

            return $output;
        } else {
            return 'No data found.';
        }
    }
}

// Call the function and output the response
add_shortcode('tadawul_api', 'call_tadawul_api');

 

Step 2: Display the API Output on a Page

 

Now, go to the page where you want to display the SOAP API data and paste the following code:

 

 

2. Using Code Directly on a Page

 

If you prefer to include the SOAP API call directly on a specific page, you can do so by adding the following code to the top of that page.

 

Code:

 

$url = 'soap api url'; // Replace with the actual SOAP API URL

// SOAP XML body
$xml_body = '

   
   
     
         9593
         458439935
     

   

';

// Setting up the headers for SOAP
$headers = array(
    'Content-Type' => 'text/xml; charset=utf-8',
    'SOAPAction'   => ''
);

// Send the POST request
$response = wp_remote_post($url, array(
    'method'    => 'POST',
    'body'      => $xml_body,
    'headers'   => $headers,
    'timeout'   => 45,
));

if (is_wp_error($response)) {
    $error_message = $response->get_error_message();
    return 'Something went wrong: ' . $error_message;
} else {
    // Parse the response
    $response_body = wp_remote_retrieve_body($response);
    
    // Clean up the response for easier parsing
    $soap_response_clean = preg_replace('/(<\/?)(\w+):([^>]*>)/', '$1$2$3',  $response_body);
    
    // Load the XML response as an object
    $xml_object = simplexml_load_string($soap_response_clean);
    
    // Convert the XML object to JSON
    $json = json_encode($xml_object);
    
    // Convert the JSON to an associative array
    $array = json_decode($json, true);

    // Extract the data you want to display
    $company_data = $array['soapenvBody']['p451getDetailQuoteForCompanyResponse']['getDetailQuoteForCompanyReturn'];
}
?>

 

After executing this code, you can print $company_data to retrieve all relevant information. Customize the output according to your requirements.

 

Conclusion

 

Implementing a SOAP API in WordPress without relying on plugins gives you greater control and flexibility. By following the steps outlined in this guide, you can successfully integrate SOAP API calls, manage requests and responses, and handle errors effectively. This approach improves site performance, reduces reliance on external plugins, and provides custom functionality tailored to your specific needs.

 

With this method, developers can create lean, efficient WordPress websites while expanding their skills in API integration.

Comments

We Serve clients globally in diverse industries

Stay Upto Date With Our Newsletter.