1

Can anyone help me convert this javascript script I wrote below to PHP? Any help would be appreciated:

<script type="text/javascript">
    $.getJSON('inc/data.php',{format: "json"}, function(data) {
        var valC="";
        $.each(data['canales'], function(key, val) {
            var str = key;
            str = str.replace("_","-");
            valC=valC+'<li><a href="'+str+'.php" title="'+key+'"><img class="img-min-menu" src="http://cdn.vaughnsoft.com/vaughnsoft/vaughn/img_profiles/'+val+'_320.jpg" alt=""></a></li>';
        });

        $("#lista").html(valC);
    }); 
</script>

This php file generates a json string from an array:

http://pastebin.com/xmqvii2X

Rasclatt
  • 12,498
  • 3
  • 25
  • 33
Erick Ls
  • 15
  • 4

1 Answers1

0

I think this is what you are trying to do. See my notes, you have to uncomment one of the two options based on your capabilities and requirements:

class getJSON
    {
        protected   $valC;
        protected   $getFile;

        public  function __construct($filename = 'inc/data.php')
            {
                $this->valC     =   "";

                // If you can edit page two, remove lines 65 down on that page and uncomment this:
                // $this->doOpt1($filename);
                // If you are stuck with page two uncomment this:
                // $this->doOpt2($filename);
            }

        private function doOpt1($filename)
            {
                // Include the page
                include_once($filename);
                // Just assign the array
                $this->getFile  =   $lista->canales;
            }

        private function doOpt2($filename)
            {
                // Create a buffer
                ob_start();
                // Include the page which echos the json string
                include($filename);
                // Capture the buffer
                $data   =   ob_get_contents();
                // Flush
                ob_end_clean();
                // Decode the json
                $this->getFile  =   json_decode(trim($data),true);
            }

        public  function createLinks()
            {
                if(empty($this->getFile) || !is_array($this->getFile))
                    return $this->valC;

                foreach($this->getFile as $key => $val) {
                        $this->valC .=  '
                    <li>
                        <a href="'.str_replace("_","-",$key).'.php" title="'.$key.'">
                            <img class="img-min-menu" src="http://cdn.vaughnsoft.com/vaughnsoft/vaughn/img_profiles/'.$val.'_320.jpg" alt="">
                        </a>
                    </li>';
                    }

                return $this->valC;
            }
    }           

$makeLinks  =   new getJSON();
?>
<div id="lista"><?php echo $makeLinks->createLinks(); ?></div>

EDIT

After further discussion, it sounds like you want to scrape from the original. See if this is more what you are trying to do. It requires a cURL library and the DOM Library:

<?php

    class   cURL
        {
            public      $response;
            protected   $sendHeader;

            protected   $PostFields;

            private     $query;

            public  function    __construct($query = '')
                {
                    $this->sendHeader   =   false;
                    $this->query        =   $query;
                    if(!empty($this->query)) {
                            if(!is_array($this->query))
                                $this->response =   $this->Connect($this->query);
                            else
                                $this->encode();
                        }
                }

            public  function SendPost($array = array())
                {
                    $this->PostFields['payload']    =   $array;
                    $this->PostFields['query']      =   http_build_query($array);
                    return $this;
                }

            public  function Connect($_url,$deJSON = true)
                {
                    // Remote Connect
                    $ch         = curl_init();

                    curl_setopt($ch, CURLOPT_URL, $_url);
                    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

                    if(strpos($_url,"https://") !== false) {
                            curl_setopt($ch, CURLOPT_SSL_VERIFYPEER,2);
                            curl_setopt($ch, CURLOPT_SSL_VERIFYHOST,2);
                        }

                    if(!empty($this->PostFields['payload'])) {
                            curl_setopt($ch, CURLOPT_POST, count($this->PostFields['payload']));
                            curl_setopt($ch, CURLOPT_POSTFIELDS, $this->PostFields['query']);
                        }

                    if(!empty($this->sendHeader))
                        curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11) AppleWebKit/601.1.56 (KHTML, like Gecko) Version/9.0 Safari/601.1.56');

                    $decode     =   curl_exec($ch);
                    $_response  =   ($deJSON)? json_decode($decode, true) : $decode;
                    $error      =   curl_error($ch);

                    curl_close($ch);
                    return (empty($error))? $_response: $error;
                }

            public  function emulateBrowser()
                {
                    $this->sendHeader   =   true;
                    return $this;
                }

            public  function encode($_filter = 0)
                {
                    foreach($this->query as $key => $value) {
                            $string[]   =   urlencode($key).'='.urlencode($value);
                        }

                    if($_filter == true)
                        $string =   array_filter($string);

                    return implode("&",$string);
                }
        }

To use:

// Create cURL instance
$curl   =   new cURL();
// Masquerade as a browser and get contents
$val    =   $curl   ->emulateBrowser()
                    ->Connect('http://vaughnlive.tv/browse/espanol?a=mvn%22',false);
// Create a DOM instance
$DOM    =   new DOMDocument();
// Process the returned data
@$DOM->loadHTML($val);
// Fetch the <a> elements
$aArray =   $DOM->getElementsByTagName('a');
// Loop through them and echo the html
foreach ($aArray as $node) {
    echo $DOM->saveHtml($node), PHP_EOL;
}
Rasclatt
  • 12,498
  • 3
  • 25
  • 33
  • See if the above edit works. It will scrape the data from their page. – Rasclatt Oct 25 '15 at 03:20
  • 1
    thank you so much for the help it works, i only have a question it's posible get for example this: [link](http://prntscr.com/8v1h72) i mean all div content not only content.. this is because I want to give css styles and it would be better to get the entire contents of the div. i add a div to foreach: echo '
    '. $DOM->saveHtml($node), PHP_EOL . "
    ";
    but doesn't work: [link](http://prntscr.com/8v1hg4)
    – Erick Ls Oct 25 '15 at 05:54
  • You might be able to get the div by class or something?: http://stackoverflow.com/questions/6366351/getting-dom-elements-by-classname – Rasclatt Oct 26 '15 at 01:15