Help me with XML and PHP

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    Help me with XML and PHP

    It has been 3 days since I trying to learn about PHP.
    I sleep for 3 hours only a day, I can't sleep till I realizing it.
    I absolutely don't know PHP but I want to realized this code.

    hopefully there someone who can help me so I can sleep tight tonight.
    this is my big problem:

    Code:
    <?php
    $request_url = "http://search.4shared.com/network/searchXml.jsp?q='.urlencode($termstring).'&searchExtention=mp3&sortType=1&sortOrder=1&searchmode=3&start=1";
    $ch = curl_init();
    $timeout = 5;
    curl_setopt($ch, CURLOPT_URL, $request_url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
    curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows; U; Windows NT 5.1; de; rv:1.9.2.3) Gecko/20100401 Firefox/3.6.3 (.NET CLR 3.5.30729) (Prevx 3.0.5)');
    $data = curl_exec($ch);
    curl_close($ch);
    
    $xml = SimpleXMLElement($data);
    $name = $xml->getElementsByTagName('name')->item(0)->nodeValue;
    $date = $xml->getElementsByTagName('upload-date')->item(0)->nodeValue;
    $altr = $xml->getElementsByTagName('url')->item(0)->nodeValue;
    $donlod = $xml->getElementsByTagName('flash-preview-url')->item(0)->nodeValue;
    $ukrn = $xml->getElementsByTagName('size')->item(0)->nodeValue;
    $oleh = $xml->getElementsByTagName('user')->item(0)->nodeValue;
    ?>
    I want to extract the 4shared xml search result into php.
    there is no result yet for it.
    I try to implement code for showing result using foreach, but still error.

    I hope you can help me.

    Thanks!

    #2
    PHP Code:
    <?php
    $termstring 
    'muz';
    $request_url 'http://search.4shared.com/network/searchXml.jsp?q='.urlencode($termstring).'&searchExtention=mp3&sortType=1&sortOrder=1&searchmode=3&start=1';
    $ch curl_init();
    $timeout 5;
    curl_setopt($chCURLOPT_URL$request_url);
    curl_setopt($chCURLOPT_RETURNTRANSFER1);
    curl_setopt($chCURLOPT_CONNECTTIMEOUT$timeout);
    curl_setopt($chCURLOPT_USERAGENT'Mozilla/5.0 (Windows; U; Windows NT 5.1; de; rv:1.9.2.3) Gecko/20100401 Firefox/3.6.3 (.NET CLR 3.5.30729) (Prevx 3.0.5)');
    $data curl_exec($ch);
    curl_close($ch);

    $xml simplexml_load_string($data);
    $json json_encode($xml);
    $arr json_decode($json,true);
    #print_r($arr);
    foreach($arr['result-files']['file'] AS $file)
    {
        echo 
    '<a href="'.$file['flash-preview-url'].'">'.$file['name'].'</a> '.$file['size'].'<br/>';

    }

    ?>
    Advertise your mobile site for FREE with AdTwirl

    Comment


      #3
      coding-talk.com rocks!

      Trying to implement it.
      Thank you very much admin.
      I'll post again when getting error :D
      I hope you can answer my stupid question

      thank you very much.
      coding-talk.com rocks!

      Added after 14 minutes:

      you rocks admin!
      I implement it and get it works!
      Words isn't enough to say thanks and happy that I feel now.

      this is the code that I've implemented in my search.php file.
      I'm using wordpress btw.

      PHP Code:
      <?php
      $termstring 
      $s;
      $request_url 'http://search.4shared.com/network/searchXml.jsp?q='.urlencode($termstring).'&searchExtention=mp3&sortType=1&sortOrder=1&searchmode=3&start=1';
      $ch curl_init();
      $timeout 5;
      curl_setopt($chCURLOPT_URL$request_url);
      curl_setopt($chCURLOPT_RETURNTRANSFER1);
      curl_setopt($chCURLOPT_CONNECTTIMEOUT$timeout);
      curl_setopt($chCURLOPT_USERAGENT'Mozilla/5.0 (Windows; U; Windows NT 5.1; de; rv:1.9.2.3) Gecko/20100401 Firefox/3.6.3 (.NET CLR 3.5.30729) (Prevx 3.0.5)');
      $data curl_exec($ch);
      curl_close($ch);

      $xml simplexml_load_string($data);
      $json json_encode($xml);
      $arr json_decode($json,true);
      #print_r($arr);
      ?>

      <?php if ($s!='') {
      foreach(
      $arr['result-files']['file'] AS $file) {
          echo 
      '<a href="'.$file['flash-preview-url'].'">'.$file['name'].'</a> '.$file['size'].'<br/>';
      }
      }
      ?>
      Thanks admin!
      You inspiring me.
      I'll learn more things so I can smart like you.

      Next thing must I learn:
      adding pagination
      rename the preview when user download the file.
      steal other site player :D, clone it and implement it.

      Thanks admin
      You rocks!
      Last edited by Aan Damar; 29.04.11, 16:51.

      Comment


        #4
        This is my first code that works with admin help.
        I love it and want develop it.
        since I'm totally new and has no coding background, I hope admin and all friends in Coding Talk can help me develop it
        I plan to adding pagination and player.
        I cancel plan to rename the file automatically. It seems server must download the file before it can be renamed.

        After implement it, I adding some function to remove not useful characters including _ and mp3.
        It seems runs good.
        But, I found other problem.
        It getting error if there is no result found.

        I'm trying to fix it.
        I'm looking at 4shared xml file and find the different that is total files = 0
        Code:
        <total-files>0</total-files>
        and this is what I've typed:

        PHP Code:
        <?php if ($s!='') {
        echo 
        '<table width="100%">';
        echo 
        '<tr><th width="60%">Name</th><th>Size</th><th>Download</th></tr>';
        $counter $arr['search-result']['total-files'];
        $file $arr['result-files']['file'];
        if (
        $counter ) {
        echo 
        '<tr><td>Sorry, there is no result</td></tr>';
        }
        else {
        foreach(
        $file AS $files) {
        echo 
        '<tr>';
        echo 
        '<td>'.cleaning(delete_mp3($files['name'])).'</td>';
        echo 
        '<td>'.$files['size'].'</td>';
        echo 
        '<td><div class="download"><a onclick="window.open (''.$files['flash-preview-url'].'')" href="#" rel="nofollow" href="">Download</a></div></td>';
        echo 
        '</tr>';
        }
        }
        echo 
        '</table>';
        }
        ?>
        Unfortunately, I still get error when no result found.
        admin and all friends in Coding Talk, can you help me please
        where is the error and how I can fix it.

        thank you very much admin and all friends in Coding talk

        ps:
        I'm very sorry for my very bad english

        Comment


          #5
          can you post what type of error you get?
          Advertise your mobile site for FREE with AdTwirl

          Comment


            #6
            it show message like this sir:
            Warning: Invalid argument supplied for foreach() in /home/xxxxxxx/public_html/wp-content/themes/mp3/search.php on line 56

            do you need the login sir?
            thanks

            Comment


              #7
              $file is empty making foreach invalid

              Comment


                #8
                Originally posted by something else View Post
                $file is empty making foreach invalid
                thanks sir.
                It very useful, but I still can't implement it.

                this 2 kinds code I try to implement, but still see the error

                first code I try using empty $file. Still error :D
                PHP Code:
                <?php
                $file 
                $arr['result-files']['file'];
                if (
                $s!='') {
                echo 
                '<table width="100%">';
                echo 
                '<tr><th width="60%">Name</th><th>Size</th><th>Download</th></tr>';
                foreach(
                $file AS $files) {
                echo 
                '<tr>';
                echo 
                '<td>'.cleaning(delete_mp3($files['name'])).'</td>';
                echo 
                '<td>'.$files['size'].'</td>';
                echo 
                '<td><div class="download"><a href="'get_settings('home').'/dl?='.base64_encode($files['flash-preview-url']).'" rel="nofollow" href="">Download</a></div></td>';
                echo 
                '</tr>';
                }
                if (empty(
                $file)) {
                echo 
                '<tr><td>Zero results</td></tr>';
                }
                echo 
                '</table>';
                }
                ?>
                Second code
                PHP Code:
                <?php
                $file 
                $arr['result-files']['file'];
                if (
                $s!='') {
                echo 
                '<table width="100%">';
                echo 
                '<tr><th width="60%">Name</th><th>Size</th><th>Download</th></tr>';
                if (
                $file == 0) {
                echo 
                '<tr><td>zero results..</td></tr>';
                }
                else {
                foreach(
                $file AS $files) {
                echo 
                '<tr>';
                echo 
                '<td>'.cleaning(delete_mp3($files['name'])).'</td>';
                echo 
                '<td>'.$files['size'].'</td>';
                echo 
                '<td><div class="download"><a href="'get_settings('home').'/dl?='.base64_encode($files['flash-preview-url']).'" rel="nofollow" href="">Download</a></div></td>';
                echo 
                '</tr>';
                }
                }
                echo 
                '</table>';
                }
                ?>
                How I can inform it when there is no result found?
                I see total-files is 0 when there is no result found.
                Code:
                http://search.4shared.com/network/searchXml.jsp?q=bon+jovi&searchExtention=mp3&sortType=1&sortOrder=1&searchmode=3&start=1
                result
                Code:
                <!--<?xml version="1.0" encoding="UTF-8"?>-->
                <search-result>
                <query>bon jovi</query>
                <total-files>0</total-files>
                <page-number>1</page-number>
                <pages-total>0</pages-total>
                <start>1</start>
                <files-per-page>10</files-per-page>
                <files-approx-count>0</files-approx-count>
                </search-result>
                May I use this parameter to inform the script if there is no result?
                or I must using other parameter?

                I'm really confused, but it make me addicted lol

                Comment


                  #9
                  i think you need to change this line:
                  if ($file == 0) {
                  to something like:
                  if (count($file) == 0) {

                  Comment


                    #10
                    Success!!!!!

                    thank you very much sir something else.
                    you're the another Rocks people!

                    thank you very much!

                    Added after 7 minutes:

                    next chapter, adding pagination
                    always need helps from you all of friends here ;)

                    thanks Coding Talk
                    Last edited by Aan Damar; 01.05.11, 17:19.

                    Comment


                      #11
                      Hai Damar, how to adding pagination ?? plz..help me...thanks

                      Comment


                        #12
                        i dnt believe in pampering yung coders like my brethrens Gum and something else, so i'll tell u something else... GO and READ. Stop cheaply coupling up a 4shared grabber.

                        Comment


                          #13
                          lol quick money

                          Comment

                          Working...
                          X