Tip: Click lines to highlight, hold ctrl/cmd to multi-select

soundcloud.php debug (15-Jan @ 22:32)

desbest.myopenid.com

Syntax Highlighted Code

  1. <?php
  2. /**
  3.  * API Wrapper for SoundCloud written in PHP with support for authication using OAuth.
  4.  *
  5.  * @author Anton Lindqvist <anton@qvister.se>
  6.  * @version 1.1
  7.  * @link http://github.com/mptre/php-soundcloud/
  8.  */
  9.         // desbest edit. uncomment the lines below
  10.         //if the api fails to generate a token
  11.         //one line at a time. working DoWnWaRdS
  12.         //echo "$consumer_key<br> $consumer_secret,<br>  ";
  13. //        echo "$oauth_token <br> $oauth_token_secret";
  14.         //exit();
  15.         //
  16.        
  17. class Soundcloud {
  18.     const VERSION = '1.1';
  19.     const URL_API = 'http://api.soundcloud.com/';
  20.     const URL_OAUTH = 'http://api.soundcloud.com/oauth/';
  21.  
  22.     function __construct($consumer_key, $consumer_secret, $oauth_token = NULL, $oauth_token_secret = NULL) {
  23.         $this->sha1_method = new OAuthSignatureMethod_HMAC_SHA1() or die ("could not sha1 method");
  24.         $this->consumer = new OAuthConsumer($consumer_key, $consumer_secret)or die ("could not make a consumer");
  25.  
  26.         if (!empty($oauth_token) && !empty($oauth_token_secret)) {
  27.             $this->token = new OAuthConsumer($oauth_token, $oauth_token_secret);
  28.         } else {
  29.             echo "<span style=\"background-color: yellow;\">This yellow section is coming from soundcloud.php
  30.            <br>A token could not be generated.
  31.            <br>$oauth_token and $oauth_token_secret
  32.            </span><hr>";
  33.             $this->token = NULL;
  34.         }
  35.     }
  36.  
  37.     function get_authorize_url($token) {
  38.         if (is_array($token)) {
  39.             $token = $token['oauth_token'];
  40.         }
  41.  
  42.         return $this->_get_url('authorize') . sprintf('?oauth_token=%s', $token);
  43.     }
  44.  
  45.     function get_request_token($oauth_callback) {
  46.         $request = $this->request(
  47.             $this->_get_url('request'),
  48.             'POST',
  49.             array('oauth_callback' => $oauth_callback)
  50.         );
  51.         $token = $this->_parse_response($request);
  52.  
  53.         $this->token = new OAuthConsumer(
  54.             $token['oauth_token'],
  55.             $token['oauth_token_secret']
  56.         );
  57.  
  58.         return $token;
  59.     }
  60.  
  61.     function get_access_token($token) {
  62.         $response = $this->request(
  63.             $this->_get_url('access'),
  64.             'POST',
  65.             array('oauth_verifier' => $token)
  66.         );
  67.         $token = $this->_parse_response($response);
  68.         $this->token = new OAuthConsumer(
  69.             $token['oauth_token'],
  70.             $token['oauth_token_secret']
  71.         );
  72.  
  73.         return $token;
  74.     }
  75.  
  76.     function request($resource, $method = 'GET', $args = array(), $headers = NULL) {
  77.         if (!preg_match('/http:\/\//', $resource)) {
  78.             $url = self::URL_API . $resource;
  79.         } else {
  80.             $url = $resource;
  81.         }
  82.  
  83.         if (stristr($headers['Content-Type'], 'multipart/form-data')) {
  84.             $body = FALSE;
  85.         } elseif (stristr($headers['Content-Type'], 'application/xml')) {
  86.             $body = FALSE;
  87.         } else {
  88.             $body = TRUE;
  89.         }
  90.  
  91.         $request = OAuthRequest::from_consumer_and_token(
  92.             $this->consumer,
  93.             $this->token,
  94.             $method,
  95.             $url,
  96.             ($body === TRUE) ? $args : NULL
  97.         );
  98.         $request->sign_request($this->sha1_method, $this->consumer, $this->token);
  99.  
  100.         return $this->_curl(
  101.             $request->get_normalized_http_url(),
  102.             $request,
  103.             $args,
  104.             $headers
  105.         );
  106.     }
  107.  
  108.     private function _build_header($headers) {
  109.         $h = array();
  110.  
  111.         if (count($headers) > 0) {
  112.             foreach ($headers as $key => $val) {
  113.                 $h[] = $key . ': ' . $val;
  114.             }
  115.  
  116.             return $h;
  117.         } else {
  118.             return $headers;
  119.         }
  120.     }
  121.  
  122.     private function _curl($url, $request, $post_data = NULL, $headers = NULL) {
  123.         $ch = curl_init();
  124.         $mime = (stristr($headers['Content-Type'], 'multipart/form-data')) ? TRUE : FALSE;
  125.         $headers['User-Agent'] = (isset($headers['User-Agent']))
  126.             ? $headers['User-Agent']
  127.             : 'PHP SoundCloud/' . self::VERSION;
  128.         $headers = (is_array($headers)) ? $this->_build_header($headers) : array();
  129.         $options = array(
  130.             CURLOPT_URL => $url,
  131.             CURLOPT_HEADER => FALSE,
  132.             CURLOPT_RETURNTRANSFER => TRUE
  133.         );
  134.  
  135.         if (in_array($request->get_normalized_http_method(), array('DELETE', 'PUT'))) {
  136.             $options[CURLOPT_CUSTOMREQUEST] = $request->get_normalized_http_method();
  137.             $options[CURLOPT_POSTFIELDS] = '';
  138.         }
  139.  
  140.         if (is_array($post_data) && count($post_data) > 0 || $mime === TRUE) {
  141.             $options[CURLOPT_POSTFIELDS] = (is_array($post_data) && count($post_data) == 1)
  142.                 ? ((isset($post_data[0])) ? $post_data[0] : $post_data)
  143.                 : $post_data;
  144.             $options[CURLOPT_POST] = TRUE;
  145.         }
  146.  
  147.         $headers[] = $request->to_header();
  148.         $options[CURLOPT_HTTPHEADER] = $headers;
  149.  
  150.         curl_setopt_array($ch, $options);
  151.  
  152.         $response = curl_exec($ch);
  153.         $this->http_status = curl_getinfo($ch, CURLINFO_HTTP_CODE);
  154.  
  155.         curl_close($ch);
  156.  
  157.         return $response;
  158.     }
  159.  
  160.     private function _get_url($type) {
  161.         switch ($type) {
  162.             case 'access':
  163.                 $method = 'access_token';
  164.                 break;
  165.             case 'authorize':
  166.                 $method = 'authorize';
  167.                 break;
  168.             case 'request':
  169.                 $method = 'request_token';
  170.                 break;
  171.         }
  172.  
  173.         return self::URL_OAUTH . $method;
  174.     }
  175.  
  176.     private function _parse_response($response) {
  177.         $return = array();
  178.         $response = explode('&', $response);
  179.  
  180.         foreach ($response as $r) {
  181.             if (strstr($r, '=')) {
  182.                 list($key, $val) = explode('=', $r);
  183.  
  184.                 if (!empty($key) && !empty($val)) {
  185.                     $return[urldecode($key)] = urldecode($val);
  186.                 }
  187.             }
  188.         }
  189.  
  190.         return (count($return) > 0) ? $return : FALSE;
  191.     }
  192. }

Plain Code

<?php
/**
 * API Wrapper for SoundCloud written in PHP with support for authication using OAuth.
 *
 * @author Anton Lindqvist <anton@qvister.se>
 * @version 1.1
 * @link http://github.com/mptre/php-soundcloud/
 */
        // desbest edit. uncomment the lines below 
        //if the api fails to generate a token
        //one line at a time. working DoWnWaRdS
        //echo "$consumer_key<br> $consumer_secret,<br>  "; 
//        echo "$oauth_token <br> $oauth_token_secret"; 
        //exit();
        //
        
class Soundcloud {
    const VERSION = '1.1';
    const URL_API = 'http://api.soundcloud.com/';
    const URL_OAUTH = 'http://api.soundcloud.com/oauth/';

    function __construct($consumer_key, $consumer_secret, $oauth_token = NULL, $oauth_token_secret = NULL) {
        $this->sha1_method = new OAuthSignatureMethod_HMAC_SHA1() or die ("could not sha1 method");
        $this->consumer = new OAuthConsumer($consumer_key, $consumer_secret)or die ("could not make a consumer");

        if (!empty($oauth_token) && !empty($oauth_token_secret)) {
            $this->token = new OAuthConsumer($oauth_token, $oauth_token_secret);
        } else {
            echo "<span style=\"background-color: yellow;\">This yellow section is coming from soundcloud.php
            <br>A token could not be generated.
            <br>$oauth_token and $oauth_token_secret
            </span><hr>";
            $this->token = NULL;
        }
    }

    function get_authorize_url($token) {
        if (is_array($token)) {
            $token = $token['oauth_token'];
        }

        return $this->_get_url('authorize') . sprintf('?oauth_token=%s', $token);
    }

    function get_request_token($oauth_callback) {
        $request = $this->request(
            $this->_get_url('request'),
            'POST',
            array('oauth_callback' => $oauth_callback)
        );
        $token = $this->_parse_response($request);

        $this->token = new OAuthConsumer(
            $token['oauth_token'],
            $token['oauth_token_secret']
        );

        return $token;
    }

    function get_access_token($token) {
        $response = $this->request(
            $this->_get_url('access'),
            'POST',
            array('oauth_verifier' => $token)
        );
        $token = $this->_parse_response($response);
        $this->token = new OAuthConsumer(
            $token['oauth_token'],
            $token['oauth_token_secret']
        );

        return $token;
    }

    function request($resource, $method = 'GET', $args = array(), $headers = NULL) {
        if (!preg_match('/http:\/\//', $resource)) {
            $url = self::URL_API . $resource;
        } else {
            $url = $resource;
        }

        if (stristr($headers['Content-Type'], 'multipart/form-data')) {
            $body = FALSE;
        } elseif (stristr($headers['Content-Type'], 'application/xml')) {
            $body = FALSE;
        } else {
            $body = TRUE;
        }

        $request = OAuthRequest::from_consumer_and_token(
            $this->consumer,
            $this->token,
            $method,
            $url,
            ($body === TRUE) ? $args : NULL
        );
        $request->sign_request($this->sha1_method, $this->consumer, $this->token);

        return $this->_curl(
            $request->get_normalized_http_url(),
            $request,
            $args,
            $headers
        );
    }

    private function _build_header($headers) {
        $h = array();

        if (count($headers) > 0) {
            foreach ($headers as $key => $val) {
                $h[] = $key . ': ' . $val;
            }

            return $h;
        } else {
            return $headers;
        }
    }

    private function _curl($url, $request, $post_data = NULL, $headers = NULL) {
        $ch = curl_init();
        $mime = (stristr($headers['Content-Type'], 'multipart/form-data')) ? TRUE : FALSE;
        $headers['User-Agent'] = (isset($headers['User-Agent']))
            ? $headers['User-Agent']
            : 'PHP SoundCloud/' . self::VERSION;
        $headers = (is_array($headers)) ? $this->_build_header($headers) : array();
        $options = array(
            CURLOPT_URL => $url,
            CURLOPT_HEADER => FALSE,
            CURLOPT_RETURNTRANSFER => TRUE
        );

        if (in_array($request->get_normalized_http_method(), array('DELETE', 'PUT'))) {
            $options[CURLOPT_CUSTOMREQUEST] = $request->get_normalized_http_method();
            $options[CURLOPT_POSTFIELDS] = '';
        }

        if (is_array($post_data) && count($post_data) > 0 || $mime === TRUE) {
            $options[CURLOPT_POSTFIELDS] = (is_array($post_data) && count($post_data) == 1)
                ? ((isset($post_data[0])) ? $post_data[0] : $post_data)
                : $post_data;
            $options[CURLOPT_POST] = TRUE;
        }

        $headers[] = $request->to_header();
        $options[CURLOPT_HTTPHEADER] = $headers;

        curl_setopt_array($ch, $options);

        $response = curl_exec($ch);
        $this->http_status = curl_getinfo($ch, CURLINFO_HTTP_CODE);

        curl_close($ch);

        return $response;
    }

    private function _get_url($type) {
        switch ($type) {
            case 'access':
                $method = 'access_token';
                break;
            case 'authorize':
                $method = 'authorize';
                break;
            case 'request':
                $method = 'request_token';
                break;
        }

        return self::URL_OAUTH . $method;
    }

    private function _parse_response($response) {
        $return = array();
        $response = explode('&', $response);

        foreach ($response as $r) {
            if (strstr($r, '=')) {
                list($key, $val) = explode('=', $r);

                if (!empty($key) && !empty($val)) {
                    $return[urldecode($key)] = urldecode($val);
                }
            }
        }

        return (count($return) > 0) ? $return : FALSE;
    }
}

Permalink: http://codedumper.com/soundcloudphp-debug