PHP code example of goteborgco / open-api-php-client

1. Go to this page and download the library: Download goteborgco/open-api-php-client library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

goteborgco / open-api-php-client example snippets


use GBGCO\API;

$apiUrl = 'https://apim-openapi-gbgco-prod.azure-api.net/gql';
$subscriptionKey = 'your-subscription-key';

$api = new API($apiUrl, $subscriptionKey);

$fields = <<<GQL
    guides {
        id
        title
        excerpt
        areas
        categories
        category_heading
        dates {
            end
            start
        }
        featuredmedia {
            sizes {
                full {
                    height
                    source_url
                    width
                }
            }
        }
    }
GQL;

$guides = $api->guides()->list(
    [
        'per_page' => 5,
        'page' => 1,
        'lang' => 'sv'
    ],
    $fields
);

// Work with the returned typed objects
foreach ($guides as $guide) {
    // All properties are properly typed
    echo $guide->getTitle();
    echo $guide->getExcerpt();
    
    // Access nested objects with type safety
    $location = $guide->getLocation();
    if ($location) {
        echo $location->getAddress();
        echo sprintf('Coordinates: %f, %f', $location->getLat(), $location->getLng());
    }
    
    // Work with media and images
    foreach ($guide->getFeaturedMedia() as $media) {
        $sizes = $media->getSizes();
        if ($sizes && $full = $sizes->getFull()) {
            echo $full->getSourceUrl();
            echo sprintf('Size: %dx%d', $full->getWidth(), $full->getHeight());
        }
    }
    
    // Handle dates
    foreach ($guide->getDates() as $date) {
        echo sprintf('From %s to %s', $date->getStart(), $date->getEnd());
    }
}

$fields = <<<GQL
    guide {
        id
        title
        excerpt
        areas
        categories
        category_heading
        dates {
            end
            start
        }
        featuredmedia {
            sizes {
                full {
                    height
                    source_url
                    width
                }
            }
        }
    }
    related {
        events {
            excerpt
            title
        }
        guides {
            excerpt
            title
        }
        places {
            excerpt
            title
        }
    }
GQL;

$result = $api->guides()->getById(8337, 'sv', $fields);

// Work with the returned typed objects
$guide = $result->guide;
echo $guide->getTitle();

// Access contact information
if ($contact = $guide->getContact()) {
    echo $contact->getEmail();
    echo $contact->getPhone();
    echo $contact->getWebsite();
}

// Access related content
if ($related = $result->related) {
    foreach ($related->getGuides() as $relatedGuide) {
        echo $relatedGuide->getTitle();
    }
    
    foreach ($related->getEvents() as $event) {
        echo $event->getTitle();
    }
    
    foreach ($related->getPlaces() as $place) {
        echo $place->getTitle();
    }
}

try {
    $guides = $api->guides()->list(['lang' => 'sv']);
} catch (\InvalidArgumentException $e) {
    // Handle validation errors (e.g., empty fields)
    echo "Validation Error: " . $e->getMessage();
} catch (\Exception $e) {
    // Handle API errors
    echo "API Error: " . $e->getMessage();
}

$fields = <<<GQL
    places {
        id
        title
        excerpt
        categories
        areas
        tags
    }
    markers {
        features {
            geometry {
                coordinates
                type
            }
            properties {
                icon
                id
                name
                slug
                type
            }
            type
        }
        type
    }
GQL;

$result = $api->places()->list(
    [
        'lang' => 'sv',
        'categories' => [10],
        'areas' => [20],
        'tags' => [30],
        'per_page' => 5,
        'page' => 1
    ],
    $fields
);

// Access the typed results
foreach ($result->items as $place) {
    echo $place->getTitle();
    echo $place->getExcerpt();
}

// Work with map markers
if ($result->markers) {
    foreach ($result->markers->getFeatures() as $feature) {
        $geometry = $feature->getGeometry();
        $properties = $feature->getProperties();
        
        echo sprintf(
            'Location: %s at coordinates [%s]',
            $properties->getName(),
            implode(', ', $geometry->getCoordinates())
        );
    }
}

$fields = <<<GQL
    place {
        id
        title
        excerpt
        content
        areas
        categories
    }
    markers {
        features {
            geometry {
                coordinates
                type
            }
            properties {
                id
                name
                icon
            }
            type
        }
    }
    related {
        events {
            title
            excerpt
        }
        guides {
            title
            excerpt
        }
        places {
            title
            excerpt
        }
    }
GQL;

$result = $api->places()->getById(1234, 'sv', $fields);

// Access the place data
$place = $result->place;
echo $place->getTitle();

// Access location information
if ($location = $place->getLocation()) {
    echo $location->getAddress();
    echo sprintf('Coordinates: %f, %f', $location->getLat(), $location->getLng());
}

$fields = <<<GQL
    events {
        id
        title
        excerpt
        date
        categories
        areas
        tags
        category_heading
    }
    markers {
        features {
            geometry {
                coordinates
                type
            }
            properties {
                id
                name
                icon
            }
            type
        }
    }
GQL;

$result = $api->events()->list(
    [
        'lang' => 'sv',
        'categories' => [10],
        'areas' => [20],
        'tags' => [30],
        'start' => '2024-01-01',
        'end' => '2024-12-31',
        'free' => 1,
        'per_page' => 5,
        'page' => 1
    ],
    [
        'fields' => ['title', 'date'],
        'orders' => ['desc']
    ],
    $fields
);

// Access the events
foreach ($result->events as $event) {
    echo $event->getTitle();
    echo $event->getDate();
}

$fields = <<<GQL
    event {
        id
        title
        excerpt
        date
        areas
        categories
        category_heading
        tags
    }
    markers {
        features {
            geometry {
                coordinates
                type
            }
            properties {
                id
                name
                icon
            }
            type
        }
    }
    related {
        events {
            title
            excerpt
        }
        guides {
            title
            excerpt
        }
        places {
            title
            excerpt
        }
    }
GQL;

$result = $api->events()->getById(2608, 'sv', $fields);

// Access the event data
$event = $result->event;
echo $event->getTitle();
echo $event->getDate();

$fields = <<<GQL
    results {
        id
        title
        excerpt
        date
        categories
        areas
        tags
        featuredmedia {
            caption
            credit
            id
            media_type
        }
    }
GQL;

$results = $api->search()->query(
    filter: [
        'query' => 'Fika',  // Required search term
        'lang' => 'sv'      // Optional language filter (enum: 'en' or 'sv')
    ],
    sortBy: [
        'fields' => 'title',
        'orders' => 'desc'
    ],
    fields: $fields
);

// Work with search results
foreach ($results->results as $item) {
    echo $item->getTitle();
    echo $item->getExcerpt();
}

$fields = <<<GQL
    name
    description
    value
    types
GQL;

$taxonomies = $api->taxonomies()->list(
    ['lang' => 'sv'],
    $fields
);

// Work with taxonomy results
foreach ($taxonomies as $taxonomy) {
    echo $taxonomy->getName();
    echo $taxonomy->getDescription();
    echo $taxonomy->getValue();
    
    // Access available types
    foreach ($taxonomy->getTypes() as $type) {
        echo $type;
    }
}

$fields = <<<GQL
    id
    name
    count
    description
    parent
GQL;

$terms = $api->taxonomy()->list(
    taxonomyName: 'categories',
    filter: ['lang' => 'sv'],
    fields: $fields
);

// Work with taxonomy terms
foreach ($terms as $term) {
    echo $term->getId();
    echo $term->getName();
    echo $term->getCount();
    echo $term->getDescription();
    
    if ($term->getParent() !== null) {
        echo "Parent ID: " . $term->getParent();
    }
}

$fields = <<<GQL
    id
    name
    count
    description
    parent
GQL;

$tree = $api->taxonomy()->list(
    taxonomyName: 'categories',
    filter: ['lang' => 'sv'],
    fields: $fields,
    hierarchical: true
);

// Work with hierarchical structure
foreach ($tree as $node) {
    $term = $node['term'];
    echo $term->getName() . '<br>';
    
    // Process children
    foreach ($node['children'] as $childNode) {
        $childTerm = $childNode['term'];
        echo "- " . $childTerm->getName() . '<br>';
        
        // Process grandchildren
        foreach ($childNode['children'] as $grandchildNode) {
            echo "-- " . $grandchildNode['term']->getName() . '<br>';
        }
    }
}

$result = $api->guides()->getById(
    8337, 
    'sv',
    [
        'guide' => [
            'id',
            'title',
            'excerpt',
            'featuredmedia' => [
                'sizes' => [
                    'full' => [
                        'height',
                        'width',
                        'source_url'
                    ]
                ]
            ]
        ],
        'related' => [
            'events' => [
                'excerpt',
                'title'
            ],
            'guides' => [
                'excerpt',
                'title'
            ],
            'places' => [
                'excerpt',
                'title'
            ]
        ]
    ]
);

$query = <<<GQL
    query {
        guides(filter: { lang: "sv", per_page: 10, page: 1 }) {
            guides {
                date
                dates {
                    end
                    start
                }
                excerpt
                title
            }
        }
    }
GQL;

$result = $api->query($query);

// Simple query with basic fields
$result = $api->query('
    query {
        guides {
            guides {
                id
                title
            }
        }
    }
');

try {
    $result = $api->query($query);
} catch (\InvalidArgumentException $e) {
    // Handle empty or invalid queries
    echo "Query Error: " . $e->getMessage();
} catch (\Exception $e) {
    // Handle API errors
    echo "API Error: " . $e->getMessage();
}