I'm connecting to an API to grab a ZIP, which contains JSON files. The vendor provides PHP code to connect, grab the ZIP, open it, and parse the JSON within. But I can't get it to work in Phalcon. Below is the code I'm using. It gets the ZIP, and Postman confirms things like my token, etc.I'm setting $token, etc, just left them out here. I suspect, but I'm not sure, that the issue might be the path to save the json, "files/myfolder"?
<?php
use Phalcon\Mvc\View;
class ReportController extends ControllerBase
{
public function indexAction()
{
$projectName = "project+name";
$token = "myAPItoken";
$url = "https://api.awrcloud.com/v2/get.php?action=get_dates&token=" . $token . "&project=" . $projectName;
$response = json_decode(file_get_contents($url), true);
$dates = $response["details"]["dates"]; // date and search depth arrays
foreach ($dates as $dateAndDepth)
{
$date = $dateAndDepth["date"];
$depth = $dateAndDepth["depth"];
$url = "https://api.awrcloud.com/get.php?action=list&project=" . $projectName . "&date=" . $date . "&token=" . $token . "&compression=zip";
$rankingResultsResponse = file_get_contents($url);
$responseRows = explode("\n", $rankingResultsResponse);
if ($responseRows[0] != "OK")
{
echo "No results for date: " . $date;
continue;
}
$dateFilesCount = $responseRows[1];
if ($dateFilesCount == 0)
{
continue;
}
for ($i = 0; $i < $dateFilesCount; $i++)
{
$urlRequest = $responseRows[2 + $i];
$urlHandle = fopen($urlRequest, 'r');
$tempZip = fopen("tempfile.zip", "w");
while (!feof($urlHandle))
{
$readChunk = fread($urlHandle, 1024 * 8);
fwrite($tempZip, $readChunk);
}
fclose($tempZip);
fclose($urlHandle);
$pathToExtractedJson = "files/myfolder";
$zip = new ZipArchive;
$res = $zip->open("tempfile.zip");
if ($res === FALSE)
{
echo "Could not extract JSON files from the zip archive";
continue;
}
$zip->extractTo($pathToExtractedJson);
$zip->close();
$dir_handle = opendir($pathToExtractedJson);
while (false !== ($entry = readdir($dir_handle)))
{
if ($entry == ".." || $entry == ".")
{
continue;
}
$rankings = json_decode(file_get_contents($pathToExtractedJson . $entry), true); // the json file contains nested json objects, make sure you use associative arrays
echo "";
echo "Search Engine: " . $rankings["searchengine"];
echo "Search Depth: " . $rankings["depth"];
echo "Location: " . $rankings["location"];
echo "Keyword: " . $rankings["keyword"];
$rank_data_array = $rankings["rankdata"];
foreach ($rank_data_array as $rank_data)
{
echo "<br/>" . $rank_data["position"] . ". " . $rank_data["url"] . " " . $rank_data["typedescription"] . " result on page " . $rank_data["page"];
}
}
}
}
}