Content from XML link not being saved, file saving empty

Hi,
I would appreciate some help. I have this script (written by someone else before I joined) which would get content from XML link and save into a file for apple news. Most part of code is working but the only thing is not working is that when file is saved, it’s blank, content is not being saved from XML.

          <?php


               $cu = 'XML FEED LINK'; 
               $tu = 'btemplate.txt';                                             
               $ld = '/var/www/ssl/apple2/doneFash/';
               $wd = '/var/www/ssl/apple2/workingFash/';

               $sectionId = 'https://news-api.apple.com/sections/Section ID';
                $metadata = '{
               "data": {
               "isSponsored": false,
              "isPreview": false,
              "maturityRating": null,
              "accessoryText": "news",
              "links": {
                "sections": [
                    "' . $sectionId . '"
                 ]
              }
              }
               }';


              $cu = 'XML FILE LINK'
              $tu = 'APPLE JSON TEMPLATE';  

            $c = file_get_contents($cu);
            $t = file_get_contents($tu);
            //$t = file_get_contents($tu);

            $a1 = explode('__ENDI__', $c);
            $ia = array();

            foreach ($a1 as $i){
            if (strlen($i) > 50){
            $ta = explode('__ENDF__', $i);
            $o = new stdClass;
            $o->storyid = $ta[0];
            $o->img1 = $ta[1];
            $o->caption = json_encode($ta[2]);
            $o->headline = json_encode($ta[3]);
            $tmpa = explode('<BR>', $ta[6]);
        
            $o->abst = json_encode($tmpa[0]);
        
           // $o->abst = json_encode($ta[4]);
         
            $o->auth = json_encode($ta[5]);
            $o->body = str_replace("<BR>\r", "\n", $ta[6]);
            $o->body = json_encode($o->body);
        
             // NEW FOR TAGS
             $tmpa2 = explode(',', $ta[7]);
             $tmpk = '';
             foreach ($tmpa2 as $k){
             $tk = trim($k);
             if (strlen($tk) > 3){
               $tmpk .= "\n" . json_encode($tk) . ",";  
               }
              }
                $tmpk .= "\n" . json_encode("news");
               $o->keywords = $tmpk;
             // END NEW FOR TAGS
          
        
        
             $ia[$o->storyid] = $o;
               } // end if strlen
          }

           echo "<pre>\n";
      
            foreach ($ia as $id => $o){
           echo "\nchecking log for $id\n";
           $ftg = $ld . $id . '.txt';
           $dt = file_get_contents($ftg);
           if (strlen($dt) < 5){
        echo "no $dt entry, creating content for $id\n";
        $cd = $wd . $id;
        mkdir($cd, 0777);
        $tt = $t;
        $tt = str_replace('**story_id**', $o->storyid, $tt);
        $tt = str_replace('**headline**', $o->headline, $tt);
        $tt = str_replace('**abstract**', $o->abst, $tt);
        $tt = str_replace('**auth**', $o->auth, $tt);
        $tt = str_replace('**caption**', $o->caption, $tt);
        $tt = str_replace('**body**', $o->body, $tt);
        
        // NEW FOR TAGS
        $tt = str_replace('**keywords**', $o->keywords, $tt);
        // END NEW FOR TAGS
        echo "</pre><hr>made template : \n\n<textarea>$tt</textarea><pre>\n\n";
        $nfn = $cd . '/' . 'article.json';  
        echo "made template, writing to file : $nfn\n";
        $res = file_put_contents($nfn, $tt);
        $metafn = str_replace('article.json', 'aametadata.json', $nfn);
        echo "writing metadata file : $metafn\n";
        $res = file_put_contents($metafn, $metadata); 
        echo "getting image : $o->img1\n";
        $img = file_get_contents($o->img1);
        if (! $img){
          echo "failed to get img\n";      
        } else {
          $ifn = $cd . '/' . 'header.jpg';
          echo "got img, putting as $ifn\n";
          $res = file_put_contents($ifn, $img);
        }
        $dfn = $ld = '/var/www/ssl/apple2/doneFash/' . $id . '.txt';
        echo "creating done file $dfn \n";
        $dtx = 'completed at ' . date('l jS \of F Y h:i:s A');
        $res = file_put_contents($dfn, $dtx);
          } else {
           echo "found $dt so not creating this content\n";    
           }

           }

          echo "\n</pre>\n";
          ?>

When Script is run this is what I get:

checking log for 1746332
no entry, creating content for 1746332
made template :

     made template, writing to file : 
    /var/www/ssl/apple2/workingFash/1746332/article.json
     writing metadata file : 
      /var/www/ssl/apple2/workingFash/1746332/aametadata.json
       getting image : http://website.com/i.php?k=d6362b35418fc2fc546dfa03ce1bac247
       got img, putting as 
      /var/www/ssl/apple2/workingFash/1746332/header.jpg
      creating done file /var/www/ssl/apple2/doneFash/1746332.txt 

article.json file is saved but completely empty. The file saved in /doneFash/1746332.txt just shows the following: ‘completed at Tuesday 8th of January 2019 10:56:27 AM’

I’ll bet if you rewrite this code with more descriptive variable names and a lot of comments explaining what each section of code does, you’ll find your problem. https://rubberduckdebugging.com/

1 Like

Hi,
I actually had not written this code but another person did it, I am just asked to find out what the issue is. I know most of code is working but except for the part where content being taken from database and it’s not saving into the file, file is being saved but empty and content is not being added into the file. Except for that, the code seems to be working fine.

Never miss an opportunity to refactor. Just rewrite the code yourself in a way that you, future-you, and another person would be able to understand 4 years from now. In the process of doing so you’ll discover the problem you’re currently worried about and probably another problem.

Just do it and don’t complain. Half my answers in this forum involved rewriting the OPs code to be easier to understand. If I can do that for free for other people, you can do that for yourself especially if this is code you’re being paid to work on. I can’t do anything at the moment because the battery in my laptop went bad.

1 Like
Sponsor our Newsletter | Privacy Policy | Terms of Service