We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 32407
    • 35 Posts
    Hi,

    I created a PHP page that I want to incorporate in modx either by a snippit or including it somehow. This will only go on one page of my site.

    Here is what the page looks like http://garagedoorspittsburgh.com/directory.php

    here is the source code:

    <?php

    class localRSS
    {
    // -------------------------------------------------------------------
    // Public properties
    // -------------------------------------------------------------------
    var $default_cp=’UTF-8’;
    var $CDATA=’nochange’;
    var $cp=’’;
    var $items_limit=10;
    var $stripHTML=False;
    // -------------------------------------------------------------------
    // Private variables
    // -------------------------------------------------------------------
    var $localtags=array(’name’,’address’,’city’,’state’,’zipcode’,’phone’);
    // -------------------------------------------------------------------
    // Parse RSS file and returns associative array.
    // -------------------------------------------------------------------
    function Get ($rss_url)
    {
    //if cache enabled
    if ($this->cache_dir!=’’)
    {
    $cache_file=$this->cache_dir.’/rsscache_’.md5($rss_url);
    $timedif=@(time()-filemtime($cache_file));
    if ($timedif<$this->cache_time)
    {
    // cached file is fresh enough, return cached array
    $result = unserialize(join(’’, file($cache_file)));
    // set ’cached’ to 1 only if cached file is correct
    if ($result) $result[’cached’] = 1;
    }
    else
    {
    // cached file is too old, create new
    $result = $this->Parse($rss_url);
    $serialized = serialize($result);
    if ($f = @fopen($cache_file, ’w’)) {
    fwrite ($f, $serialized, strlen($serialized));
    fclose($f);
    }
    if ($result) $result[’cached’] = 0;
    }
    }
    else
    {
    $result = $this->Parse($rss_url);
    if ($result) $result[’cached’] = 0;
    }
    // return result
    return $result;
    }
    // -------------------------------------------------------------------
    // Modification of preg_match(); return trimed field with index 1
    // from ’classic’ preg_match() array output
    // -------------------------------------------------------------------
    function my_preg_match ($pattern, $subject)
    {
    // start regullar expression
    preg_match($pattern, $subject, $out);
    //print_r ($out);
    // if there is some result... process it and return it
    if(isset($out[1]))
    {
    // Process CDATA (if present)
    if ($this->CDATA == ’content’)
    { // Get CDATA content (without CDATA tag)
    $out[1] = strtr($out[1], array(’<![CDATA[’=>’’, ’]]>’=>’’));
    }
    elseif ($this->CDATA == ’strip’)
    { // Strip CDATA
    $out[1] = strtr($out[1], array(’<![CDATA[’=>’’, ’]]>’=>’’));
    }

    // If code page is set convert character encoding to required
    if ($this->cp != ’’)
    //$out[1] = $this->MyConvertEncoding($this->rsscp, $this->cp, $out[1]);
    $out[1] = iconv($this->rsscp, $this->cp.’//TRANSLIT’, $out[1]);
    // Return result
    //echo "Yes";
    return trim($out[1]);
    }
    else
    {
    //echo "No";
    // if there is NO result, return empty string
    return ’’;
    }
    }
    // -------------------------------------------------------------------
    // Replace HTML entities &something; by real characters
    // -------------------------------------------------------------------
    function unhtmlentities ($string)
    {
    // Get HTML entities table
    $trans_tbl = get_html_translation_table (HTML_ENTITIES, ENT_QUOTES);
    // Flip keys<==>values
    $trans_tbl = array_flip ($trans_tbl);
    // Add support for &apos; entity (missing in HTML_ENTITIES)
    $trans_tbl += array(’&apos;’ => "’");
    // Replace entities by values
    return strtr ($string, $trans_tbl);
    }
    // -------------------------------------------------------------------
    // Parse() is private method used by Get() to load and parse RSS file.
    // Don’t use Parse() in your scripts - use Get($rss_file) instead.
    // -------------------------------------------------------------------
    function Parse ($rss_url)
    {
    // Open and load RSS file
    if ($f = @fopen($rss_url, ’r’))
    {
    $rss_content = ’’;
    while (!feof($f))
    {
    $rss_content .= fgets($f, 4096);
    }
    fclose($f);
    // Parse ITEMS
    preg_match_all("’<local(| .*?)>(.*?)</local>’si", $rss_content, $locals);
    $rss_locals = $locals[2];
    $i = 0;
    $result[’locals’] = array(); // create array even if there are no items
    foreach($rss_locals as $rss_local)
    {
    // If number of items is lower then limit: Parse one item
    if ($i < $this->items_limit || $this->items_limit==0)
    {
    foreach($this->localtags as $localtag)
    {
    $temp=$this->my_preg_match("’<$localtag.*?>(.*?)</$localtag>’si",$rss_local);
    if ($temp != ’’) $result[’locals’][$i][$localtag] = $temp; // Set only if not empty
    }
    $i++;
    }
    }
    $result[’items_count’]=$i;
    $i=0;
    preg_match_all("’<extended(| .*?)>(.*?)</extended>’si",$rss_content,$datas);
    $rss_datas=$datas[2];
    foreach($rss_datas as $rss_data)
    {
    // If number of items is lower then limit: Parse one item
    if ($i < $this->items_limit || $this->items_limit==0)
    {
    $temp=$this->my_preg_match("’<data key=\"weburl\" value=\"(.*?)\"/>’si",$rss_data);
    if ($temp != ’’)
    {
    $result[’locals’][$i][url] = $temp; // Set only if not empty
    continue;
    }
    $temp=$this->my_preg_match("’<data key=\"Acxiom_url\" value=\"(.*?)\"/>’si",$rss_data);
    if ($temp != ’’)
    {
    $result[’locals’][$i][url] = $temp; // Set only if not empty
    continue;
    }
    $temp=$this->my_preg_match("’<data key=\"CitySearch_url\" value=\"(.*?)\"/>’si",$rss_data);
    if ($temp != ’’)
    {
    $result[’locals’][$i][url] = $temp; // Set only if not empty
    continue;
    }
    $temp=$this->my_preg_match("’<data key=\"CitySearch_displayurl\" value=\"(.*?)\"/>’si",$rss_data);
    if ($temp != ’’)
    {
    $result[’locals’][$i][url] = $temp; // Set only if not empty
    continue;
    }
    }
    $i++;
    }
    return $result;
    }
    else
    {
    return False;
    }
    }
    }


    $rss = new localRSS;

    // Set cache dir and cache time limit (5 seconds)
    // (don’t forget to chmod cahce dir to 777 to allow writing)
    $rss->cache_dir = ’./temp’;
    $rss->cache_time = 1200;
    global $rss;
    if ($rs = $rss->Get("http://ldn.local.com/lds/search.jsp?appid=F02EE02A-5998-4C03-AF3C-E780EB82C8BC&keyword=garage+door&location=pittsburgh%2Cpa&ip=98.194.8.21&referrer=www.temp.com&maxresults=10&rp=http%3A%2F%2Fwww.temp.com%3A"))
    {
    echo "<table class=\"listingTable\" align=\"center\" cellpadding=\"0\" cellspacing=\"0\">\n<tr>\n<td class=\"listingHeader\" colspan=\"2\">Pittsburgh Garage Door Directory</td>\n</tr>\n";
    foreach ($rs[’locals’] as $local)
    {
    echo "<tr>\n<td class=\"listing\">\n";
    echo "<b>$local[name]<b>
    ";
    if ($local[address]!="") echo "$local[address] ";
    echo "$local[city], ";
    echo "$local[state] ";
    echo "$local[zipcode]";
    echo "<td>\n<td class=\"listingR\">";
    echo "<b>";
    echo substr($local[phone],1,3);
    echo "-";
    echo substr($local[phone],3,3);
    echo "-";
    echo substr($local[phone],6,4);
    echo "</b>
    ";
    if ($local[url]!="")
    {
    echo "<div class=\"visitWebsite\"><ul><li><a href=\"";
    echo "$local[url]";
    echo "\" target=\"_blank\" class=\"btn-visit-website\"></a";
    }
    }
    }
    ?>

    When I try adding it to modx using a snippet and then putting it in the content I get a 500 error code. I am on a windows server.
      • 6228
      • 249 Posts
      Although I’m not sure exactly what is causing the server error, I would strongly recommend refactoring the code to take advantage of php’s DOMDocument extension which is made for this sort of thing (and MUCH more efficient than regular expression DOM parsing). The sample link you provided took several seconds to display, which is probably an indication of a script that is working a bit harder than it should.

      That said, 500 errors on Windows can be caused by filesystem errors, resource limitations (your script might have run out of memory), and permission problems, to name a few. Try doing some ’process of elimination’ testing on your snippet code to isolate the faulty code.

      Mike
        lo9on.com

        MODx Evolution/Revolution | Remote Desktop Training | Development