Skip to content

Commit cc58bff

Browse files
authored
Merge pull request #1 from styladev/986N_offsetParam404
#986N added offset parameter to SEO API request and 404 for non exist…
2 parents 8e3c1d5 + da0a23f commit cc58bff

File tree

2 files changed

+22
-7
lines changed

2 files changed

+22
-7
lines changed

README.md

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,17 @@ This is an example implementation for fetching SEO content for a Styla magazine
55

66
## GET IT RUNNING
77

8-
In order to run the example locally on your machine you need a local server that supports PHP. To get a server like this you can utilize common software like e.g. [MAMP](https://www.mamp.info/en/downloads/) which will serve as a reference for this guide.
8+
#### Option A
99

10-
Download this repository as [a zip file](https://github.com/styladev/php-sdk/archive/master.zip) or clone and extract it. Once MAMP is installed and running, place all extracted files in the “htdocs” directory. Alternatively you can change the path to the extracted `index.php` within the MAMP settings.
10+
Clone this repository or download and extract it as [a zip file](https://github.com/styladev/php-sdk/archive/master.zip). Open a terminal window and change directory to the cloned directory containing the `index.php` file. Run `php -S localhost:8000` to ramp up a local PHP server. You should now be able to navigate to http://localhost:8000/ in your browser.
11+
12+
#### Option B
13+
14+
Another way to get a server like this running you can utilize common software like e.g. [MAMP](https://www.mamp.info/en/downloads/) which will serve as a reference for this guide. Once MAMP is installed and running, place all extracted files in the “htdocs” directory. Alternatively you can change the path to the extracted `index.php` within the MAMP settings.
1115

1216
---
1317

14-
As an example for this README we are going to use "Kiveda" as an client:
18+
As an example for this README we are going to use "Kiveda" as an client and use MAMP for our local server:
1519

1620
* If your magazine is using a specific rootpath (e.g. /magazin is the rootpath for Kiveda) create new folder within htdocs named after that rootpath and place all files except the .htaccess in there:
1721

seo.php

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,18 @@
1919
$key = array_key_exists("origUrl", $_REQUEST) ? $_REQUEST["origUrl"] : "/";
2020
}
2121
else{
22+
// Extract path and query information from current URL
2223
$url = parse_url($_SERVER['REQUEST_URI']);
2324
$path = $url['path'];
25+
$query = isset($url['query']) ? $url['query'] : "";
26+
27+
// search for rootpath in $path and remove it
2428
$key = str_replace("/".$config['rootpath'], "", $path);
29+
30+
// Append ?offset parameter if set
31+
if (strpos($query, 'offset=') !== false) {
32+
$key .= '?'.$query;
33+
}
2534
}
2635

2736
// escapes the key string
@@ -43,7 +52,6 @@ function fetchFromSeoApi($key, $url){
4352
if(isset($SEO_json->status)){
4453
// check if response code is 2XX
4554
if(substr((string)$SEO_json->status, 0, 1) == '2'){
46-
4755
// extract caching duration and SEO information from JSON
4856
$data["expire"] = isset($SEO_json->expire) ? $SEO_json->expire / 60 : 60; // in s
4957
$data["head"] = isset($SEO_json->html->head) ? $SEO_json->html->head : "";
@@ -55,7 +63,10 @@ function fetchFromSeoApi($key, $url){
5563

5664
// return JSON to use it in index.php
5765
return $SEO_json;
58-
}
66+
} else {
67+
// non existent pages/sites should return 404
68+
header("HTTP/1.0 404 Not Found");
69+
}
5970
}
6071
}
6172
}
@@ -70,10 +81,10 @@ function fetchFromSeoApi($key, $url){
7081
$SEO_json = fetchFromSeoApi($key, $url);
7182

7283
// Set SEO <head> information for index.php
73-
$SEO_head = $SEO_json->html->head;
84+
$SEO_head = isset($SEO_json->html->head) ? $SEO_json->html->head : "";
7485

7586
// set SEO <body> information for index.php
76-
$SEO_body = $SEO_json->html->body;
87+
$SEO_body = isset($SEO_json->html->body) ? $SEO_json->html->body : "";
7788
}
7889
else{
7990
$debug_cache = "true";

0 commit comments

Comments
 (0)