Jump to content

API integration


cbrei
Go to solution Solved by QuickOldCar,

Recommended Posts

I'm attempting to set up menus (and other info) from the SinglePlatform service: https://www.singleplatform.com/ to a WordPress site

 

They have some resources:  

 

I'm running into trouble with structuring the data retuned from the API into a PHP array, where I can then use to populate the site with individual menu items, etc.

 

 

I have this php code:

<?php
$client_key = 'c3sbrpbtkbs5gverurfkxpemc';
$sig_key = 'PkkJXNRpO2l0r5u6u0PlCNme1KA8akGejMIjHI5Gkj8';
$base_url = '/locations/horse-inn/menus/?client=' . $client_key;
$url = hash_hmac('sha1',$base_url, $sig_key, true);
$str = urlencode(base64_encode($url));
$fnl_url = 'http://publishing-api.singleplatform.com'.$base_url.'&signature='.$str;
$ch = curl_init($fnl_url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec( $ch );
$result = json_decode($response, true);

parse_str($response, $valueArray);
echo "<pre>", print_r($valueArray, true), "</pre>";
?>

where $client_key  and $sig_key are unique variables for my SinglePlatform account

 

 

the return looks like this:

Array
(
    [{"code":200,"data":_{"id":1542722,"location_id":"horse-inn","name":"Main Menu","description":"","menu_type":"Menu","footnote":"","order_num":0,"currency":"USD","created":"2015-05-29T03:25:24","updated":"2015-06-02T09:34:01","attribution_image":"http://a.singleplatform.com/DirectBusinessIntegration/horse-inn/provided_by_2.png","attribution_image_link":"http://www.singleplatform.com/partner-lp?ref] => DirectBusinessIntegration
    [sp_channel] => viral
    [sp_source] => publisher
    [sp_campaign] => DirectBusinessIntegration","sections":[{"order_num":0,"items":[{"name":"Manhattan #1","choices":[],"photos":[],"order_num":0,"attributes":{},"additions":[],"id":61836457,"description":"Spirit, Angostura 
    [Sam's_French_bread"},{"name":"Beet_] => 
    [goat_cheese_Salad","choices":] => Array
        (
            [{"prices":{"max":"","min":10},"order_num":0,"calories":{"max":"","min":""},"name":"","unit":""}] => 
        )

    [extra_virgin_olive_oil"},{"name":"Confit_pork_lettuce_wraps","choices":] => Array
        (
            [{"prices":{"max":"","min":8},"order_num":0,"calories":{"max":"","min":""},"name":"3 for","unit":""},{"prices":{"max":"","min":15},"order_num":1,"calories":{"max":"","min":""},"name":"6 for","unit":""}] => 
        )

    [cucumber__Choice_of_house_made_dressing:_Buttermilk_ranch_or_honey_lemon_vinaigrette"},{"name":"Local_strawberry_] => 
    [spinach","choices":] => Array
        (
            [{"prices":{"max":"","min":8},"order_num":0,"calories":{"max":"","min":""},"name":"","unit":""}] => 
        )

    [64°_Egg","choices":] => Array
        (
            [{"prices":{"max":"","min":12},"order_num":0,"calories":{"max":"","min":""},"name":"","unit":""}] => 
        )

    [Aged_provolone_cheese,_garlic_heavy_cream,_house_hot_sauce"},{"name":"Gyro","choices":] => Array
        (
            [{"prices":{"max":"","min":11},"order_num":0,"calories":{"max":"","min":""},"name":"","unit":""},{"prices":{"max":"","min":14},"order_num":1,"calories":{"max":"","min":""},"name":"With Fries","unit":""}] => 
        )

    [tenderloin,_lindendale_Farms_Goats_milk_yogurt_tzatziki,_grilled_tomatoes_] => 
    [onion,_Brogue_Hydroponics_spring_mix"},{"name":"Fried_Green_tomato_BLT","choices":] => Array
        (
            [{"prices":{"max":"","min":11},"order_num":0,"calories":{"max":"","min":""},"name":"","unit":"11"},{"prices":{"max":"","min":14},"order_num":1,"calories":{"max":"","min":""},"name":"With Fries","unit":""}] => 
        )

    [10%_dry_aged_beef,_lettuce,_tomato_Red_onion_white_American_cheese,_special_sauce,_Alfred_] => 
    [Sam's_bun"},{"name":"Littleneck_clams","choices":] => Array
        (
            [{"prices":{"max":"","min":15},"order_num":0,"calories":{"max":"","min":""},"name":"","unit":""}] => 
        )

    [toast","choices":] => Array
        (
            [{"prices":{"max":"","min":17},"order_num":0,"calories":{"max":"","min":""},"name":"","unit":""}] => 
        )

    [Sam's_French_bread,_red_wine_demi-glace"},{"name":"Seasonal_vegetable_plate","choices":] => Array
        (
            [{"prices":{"max":"","min":12},"order_num":0,"calories":{"max":"","min":""},"name":"","unit":""}] => 
        )

    [herbs"}],"id":9172490,"description":"","name":"Main"},{"order_num":1,"items":] => Array
        (
            [{"name":"Brown Buttered Cauliflower","choices":[{"prices":{"max":"","min":5},"order_num":0,"calories":{"max":"","min":""},"name":"","unit":""}] => DirectBusinessIntegration
        )

    [Boker's_bitters,_Luxardo_maraschino_liquor,_Carpano_Antica,_orange"},{"name":"Old_Fashioned_#2","choices":] => Array
        (
            [0] => 
        )

    [orange,_house_made_grenadine,_egg_white,_orange_bitters"},{"name":"Mint_Julep","choices":] => Array
        (
            [0] => 
        )

)

I have never done an API integration into PHP and am somewhat knowledgable of PHP, but obviously not enough to figure out how to manipulate the API data

 

the menu looks like this on the SinglePlatform site: http://w.singlepage.com/horse-inn/menu

 

 

many thanks in advance for assistance in resolving this issue

 

Link to comment
Share on other sites

Correct, it is JSON - and that's part of my issue (I think)  -  I have a line of code in there:

$result = json_decode($response, true);

but I'm not sure if I'm using it properly, or if the JSON is structured properly to begin with

 

thanks

Link to comment
Share on other sites

More like this and now can access any array or loop through them.

<?php
$client_key = 'c3sbrpbtkbs5gverurfkxpemc';
$sig_key = 'PkkJXNRpO2l0r5u6u0PlCNme1KA8akGejMIjHI5Gkj8';
$base_url = '/locations/horse-inn/menus/?client=' . $client_key;
$url = hash_hmac('sha1',$base_url, $sig_key, true);
$str = urlencode(base64_encode($url));
$fnl_url = 'http://publishing-api.singleplatform.com'.$base_url.'&signature='.$str;
$ch = curl_init($fnl_url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec( $ch );
$result = json_decode($response, true);

//echo "<pre>", print_r($result), "</pre>";

if($result['data']){
	foreach($result['data'] as $data){
		echo "<pre>", print_r($data), "</pre>";
	}
	
}
?>
Link to comment
Share on other sites

  • Solution

I'll help a bit more in case not familiar with what to do the rest.

You can assign variables, style as want, however you would like to use the data.

<?php
$client_key = 'c3sbrpbtkbs5gverurfkxpemc';
$sig_key    = 'PkkJXNRpO2l0r5u6u0PlCNme1KA8akGejMIjHI5Gkj8';
$base_url   = '/locations/horse-inn/menus/?client=' . $client_key;
$url        = hash_hmac('sha1', $base_url, $sig_key, true);
$str        = urlencode(base64_encode($url));
$fnl_url    = 'http://publishing-api.singleplatform.com' . $base_url . '&signature=' . $str;
$ch         = curl_init($fnl_url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
$result   = json_decode($response, true);

//echo "<pre>", print_r($result), "</pre>";

if ($result['data'] && $result['code'] == "200") {
    foreach ($result['data'] as $data) {
        
        //echo "<pre>", print_r($data), "</pre>";
        
        echo $data['id'] . "<br />";
        echo $data['location_id'] . "<br />";
        echo $data['name'] . "<br />";
        echo $data['description'] . "<br />";
        echo $data['menu_type'] . "<br />";
        echo $data['footnote'] . "<br />";
        echo $data['order_num'] . "<br />";
        echo $data['currency'] . "<br />";
        echo $data['created'] . "<br />";
        echo $data['updated'] . "<br />";
        echo $data['attribution_image'] . "<br />";
        echo $data['attribution_image_link'] . "<br />";
        
        foreach ($data['sections'] as $sections) {
            echo $sections['order_num'] . "<br />";
            
            foreach ($sections['items'] as $items) {
                echo $items['name'] . "<br />";
                echo $items['description'] . "<br />";
                
                //got lazy, you can keep going
                echo "<pre>", print_r($items['choices']), "</pre>";
                
                
                
            }
            
        }
   
    }
    
}
?>
Link to comment
Share on other sites

wow - this was SUPER helpful!

 

I am nearly there I think - I just can't get the price to display, here is what I have:

<?php
$client_key = 'c3sbrpbtkbs5gverurfkxpemc';
$sig_key    = 'PkkJXNRpO2l0r5u6u0PlCNme1KA8akGejMIjHI5Gkj8';
$base_url   = '/locations/horse-inn/menus/?client=' . $client_key;
$url        = hash_hmac('sha1', $base_url, $sig_key, true);
$str        = urlencode(base64_encode($url));
$fnl_url    = 'http://publishing-api.singleplatform.com' . $base_url . '&signature=' . $str;
$ch         = curl_init($fnl_url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
$result   = json_decode($response, true);

if ($result['data'] && $result['code'] == "200") {
    foreach ($result['data'] as $data) {

        if ($data['id'] == "1542722") {
        
        foreach ($data['sections'] as $sections) {
    
            foreach ($sections['items'] as $items) {
                echo "<ul id='menu_item'>";
                echo "<li id='title'>" . $items['name'] . "</li>";
                echo "<ul id='ingredients'><li>" . $items['description'] . "</li></ul>";
                
                    foreach ($items['choices'] as $choices) {

                        foreach ($choices['prices'] as $prices) {
                            echo "<li id='price'>" . $prices['min'] . "</li>";
                        }

                    }
                
                echo "</ul>";
                
                
            }
            
        }
            
        }
   
    }
    
}
?>

thoughts?

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.