-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
better implementation for: make InterConnect Microservice example han…
…dle missing catalog
- Loading branch information
Showing
3 changed files
with
55 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
<?php | ||
|
||
include 'getItems.php'; | ||
$result = RetrieveItems($catalogRoute . "/items"); | ||
if (isset(json_decode($result)->errno)) { | ||
http_response_code(500); | ||
} | ||
echo $result; | ||
|
||
?> | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
<?php | ||
|
||
function RetrieveItems() | ||
{ | ||
$services = getenv("VCAP_SERVICES"); | ||
$services_json = json_decode($services, true); | ||
for ($i = 0; $i < sizeof($services_json["user-provided"]); $i++){ | ||
if ($services_json["user-provided"][$i]["name"] == "catalogAPI"){ | ||
$catalogHost = $services_json["user-provided"][$i]["credentials"]["host"]; | ||
} | ||
} | ||
$parsedURL = parse_url($catalogHost); | ||
$catalogRoute = $parsedURL["scheme"] . "://" . $parsedURL["host"]; | ||
$url = $catalogRoute . "/items"; | ||
|
||
$curl = curl_init(); | ||
curl_setopt($curl, CURLOPT_URL, $url); | ||
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); | ||
$curlResult = curl_exec($curl); | ||
$curlError = curl_error($curl); | ||
$curlErrno = curl_errno($curl); | ||
curl_close($curl); | ||
$firstChar = substr($curlResult, 0, 1); /* should check if $curlResult === FALSE if newer PHP */ | ||
if ($firstChar != "{") { | ||
$errorObject = new stdClass(); | ||
$errorObject->error = $curlError; | ||
$errorObject->errno = $curlErrno; | ||
return json_encode($errorObject); | ||
} | ||
return $curlResult; | ||
} | ||
|
||
?> | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters