Viewing 1 replies (of 1 total)
  • Moderator bcworkz

    (@bcworkz)

    It depends on what you will do with the data. If it will be used to generate a page on your site, a custom page template would be a good option. The template code can handle everything any time a page that uses it is requested.

    Your code would first construct or define the required API request. Then request the data. You can use the WP_Http class to do this. If installed on your server, a lot of devs like cURL for making requests. You could just use PHP’s file_get_content().

    The response that comes back does not appear to be in any conventional structure like XML, JSON, CDF, etc. This means you’ll need to parse the returned data with your own code. The purpose is to place the data in an array structure in which it is easy for your code to access the individual elements within. An indexed array of associative arrays often works well. You can do a foreach loop to access each indexed element, and the individual associative elements can be accessed by key name.

    With non-conventional data structures, you are forced to use PHP string manipulation to extract the needed data. This is a little easier if you can use explode() to break up the data into manageable parts.

    If the data could be used for multiple page requests, you should cache the data to avoid needing to hit the API every time. WP transients are a good choice for this. Your code first checks for transient data to fulfill requests. If no transient is available, then get the data from the API.

    Once the data is parsed into a logical array structure, generating the page is much easier. The ability for PHP to expand variables within double quoted strings can be very useful when dealing with structured data.

Viewing 1 replies (of 1 total)
  • The topic ‘How do I integrate an external api’ is closed to new replies.