We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

API call from 3rd party vendor returns a ZIP file, and I can't get it to unzip and parse in Phalcon

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"];
                  }
              }
          }
      }
}

hi @driscojs what is the error that is showing? do you have a view file related with this action?



6.6k

I can't get much to show. I disabled the view, and was trying to var_dump any kind of result. If I var_dump($res) right after I open the $zip, I get: int(9)

If I var_dump($zip->extractTo($pathToExtractedJson));, I get: bool(false);

I suspect, although I can't be sure, that the extractTo is failing ot create the extracted files. Permissions on the destination folder are 775. But I don't know if I'm setting the path correctly for Phalcon. I tried multiple permutations, like APP_PATH . "/../public/files/myfolder", "/files/myfolder", "files/myfolder", etc.

hi @driscojs what is the error that is showing? do you have a view file related with this action?

you get a 9because the file doens't exists ZipArchive::ER_NOENT. Check paths and permissions

Good luck



6.6k

It's something about the folder I'm targeting; I just got it to spit out the ZIP, then the JSOn files inside to the temp folder....both Files/myfolder and temp are 775, though....

I had it send the ZIP to /html/myapp/public/temp, and it created it fine. I changed the destination for the extrated files to $pathToExtractedJson = "temp";, and it extracted them there. I'd really like to put them in files/myfolder, any ideas?



6.6k

I cross-posted an update that was not a reply to you, so please see that, but I did get them to extract to temp, instead of files/myfolder. Can't figure out how to get them into files yet. Confirmed the ZIP exists by putting into temp first, them attempting the extract from there (figured phalcon might have the "source" zip in a weird place, so "saving" it ot temp confirms it downloaded, etc ok)

you get a 9because the file doens't exists ZipArchive::ER_NOENT. Check paths and permissions

Good luck



6.6k

I "kind of" got it. I created another folder in the public folder, called myfolder (/public/myfolder). I can save the zip and it's extracted contents there. I think it was auto-removing files I saved to files/myfolder, but I can't figure out why. Thank you for the responses.

you get a 9because the file doens't exists ZipArchive::ER_NOENT. Check paths and permissions

Good luck