Short url script problem

Discussion in 'PHP' started by Ruriko, Jul 2, 2010.

  1. #1
    This is a wordpress plugin that converts all urls in a post into sm00sh urls. Now I want to edit it and change to a different service so I replaced the api url and it doesn't work!

    <?php
    /*
    Program Name: sm00sh Processor Library
    Program URI: http://smsh.me/action/api
    Description: A lib to be used by external plugins in order to converts all URLs in a text into shorten URLs using the sm00sh service at http://smsh.me.
    Author: Lopo Lencastre de Almeida <dev@ipublicis.com>, Copyright 2009  
    Version: 1.0 
    Author URI: http://www.ipublicis.com
    License: GNU LGPL v3 or later
    */
    
    /**
    Class sm00sher
    main class section.
    */
    class smsh {
    
    	/**
    	smsh_parse
    	this function search the content for http links and then convert them to sm00sh'ed ones.
    	
    	@param string $content the original content
    	@return string $content the modified content
    	*/
    	public function smsh_parse($content)
    	{
    		$urls = $this->_smsh_all_links($content);
    		foreach ($urls as $url) {
    			$content = str_ireplace($url['bigurl'], $url['smsh'], $content);
    		}
    		return $content;
    	}
    
    	/**
    	_smsh_all_links
    	get all content's URLs using native PHP DOM and sends them for processing.
    
    	@param string $content the content to process
    	@return array $urls the array containing the original and the sm00s'ed URLs
    	*/
    	protected function _smsh_all_links($content) 
    	{
    
    		$urls = array();
    
    		$dom = new DOMDocument();
    		$dom->preserveWhiteSpace = FALSE;
    		@$dom->loadHTML($content);
    
    		$atags = $dom->getElementsByTagName('a');
    		foreach ($atags as $atag) {
    			$the_url = $atag->getAttribute('href');
    			$new_url = $this->_smsh_process($the_url);
    			$urls[] = array( 'bigurl' => $the_url, 'smsh' => $new_url );
    		}
    	
    		return($urls);
    	}
    
    	/**
    	_smsh_process
    	this function sends the original URL to sm00sh and returns a shorten new one. Requires cURL and SimpleXML.
    
    	@param string $url the source url
    	@return string $surl the shorten url or, on error, the original unmodified one
    	*/
    	protected function _smsh_process($url)
    	{
    		$surl = $url;
    
    		if($this->_valid_url($url))  // URLs *MUST* be valid ones 
    		{
    			$sm00sher = '[COLOR="Red"]http://spiralurl.x10.mx/api.php?url=[/COLOR]'.$url;
    	
    			$curl_handle=curl_init();
    			curl_setopt($curl_handle,CURLOPT_URL,$sm00sher);
    			curl_setopt($curl_handle,CURLOPT_CONNECTTIMEOUT,2);
    			curl_setopt($curl_handle,CURLOPT_RETURNTRANSFER,1);
    			$buffer = curl_exec($curl_handle);
    			curl_close($curl_handle);
    
    			if (!empty($buffer))
    			{
    				$xml = @simplexml_load_string($buffer);
    				if (isset($xml)) 
    				{
    					$title = @trim($xml->title);
    					$body  = @trim($xml->body);
    					if( $title == 'HTTP/1.0 200 OK' && !empty($body) ) 
    					{
    						$surl = $body;
    					}
    				}
    			}
    		}
    
    		return $surl;
    	}
    
    	/**
    	_valid_url
    	check if a URL string is valid or not
    	
    	@param string $url the source url
    	@return bollean $is_url true of is valid or false if it isn't
    	*/
    	protected function _valid_url($url)
    	{
    		return ( ! preg_match('/^(http|https|ftp|sftp):\/\/([A-Z0-9][A-Z0-9_-]*(?:\.[A-Z0-9][A-Z0-9_-]*)+):?(\d+)?\/?/i', $url)) ? FALSE : TRUE;
    	}
    
    } // EOF
    ?>
    
    Code (markup):

     
    Ruriko, Jul 2, 2010 IP
  2. c_programmer

    c_programmer Peon

    Messages:
    92
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #2
    You need to provide us with more information, we can't be expected to go though a 113 line script and find an otherwise unspecified problem. What specifically is not working and what error message do you get? Also, how do you know this script is even able to switch API urls?
     
    c_programmer, Jul 2, 2010 IP
  3. krsix

    krsix Peon

    Messages:
    435
    Likes Received:
    11
    Best Answers:
    0
    Trophy Points:
    0
    #3
    randomly throwing in a completely different service is NOT GOING TO WORK.
     
    krsix, Jul 2, 2010 IP
  4. danx10

    danx10 Peon

    Messages:
    1,179
    Likes Received:
    44
    Best Answers:
    2
    Trophy Points:
    0
    #4
    You don't need soo much code, you can achieve what you want with 3 lines of code.

    <?php
    $url_to_short = "http://digitalpoint.com";
    $shortened_url = file_get_contents("http://spiralurl.x10.mx/api?url=".$url_to_short);
    
    echo $shortened_url;
    ?>
    PHP:
     
    danx10, Jul 5, 2010 IP
  5. krsix

    krsix Peon

    Messages:
    435
    Likes Received:
    11
    Best Answers:
    0
    Trophy Points:
    0
    #5
    oh, and try this
    
    <?php
    class smsh {
    	public function smsh_parse($content)
    	{
    		$urls = $this->_smsh_all_links($content);
    		foreach ($urls as $url) {
    			$content = str_ireplace($url['bigurl'], $url['smsh'], $content);
    		}
    		return $content;
    	}
    	protected function _smsh_all_links($content) 
    	{
    
    		$urls = array();
    
    		$dom = new DOMDocument();
    		$dom->preserveWhiteSpace = FALSE;
    		@$dom->loadHTML($content);
    
    		$atags = $dom->getElementsByTagName('a');
    		foreach ($atags as $atag) {
    			$the_url = $atag->getAttribute('href');
    			$new_url = $this->_smsh_process($the_url);
    			$urls[] = array( 'bigurl' => $the_url, 'smsh' => $new_url );
    		}
    	
    		return($urls);
    	}
    	protected function _smsh_process($url)
    	{
    		$surl = $url;
    
    		if($this->_valid_url($url))
    		{
    			$sm00sher = 'http://spiralurl.x10.mx/api.php?url='.$url;
    	
    			$curl_handle=curl_init();
    			curl_setopt($curl_handle,CURLOPT_URL,$sm00sher);
    			curl_setopt($curl_handle,CURLOPT_CONNECTTIMEOUT,2);
    			curl_setopt($curl_handle,CURLOPT_RETURNTRANSFER,1);
    			$buffer = curl_exec($curl_handle);
    			curl_close($curl_handle);
    
    			if (!empty($buffer))
    			{
    				$surl = $buffer;
    			}
    		}
    
    		return $surl;
    	}
    	protected function _valid_url($url)
    	{
    		return ( ! preg_match('/^(http|https|ftp|sftp):\/\/([A-Z0-9][A-Z0-9_-]*(?:\.[A-Z0-9][A-Z0-9_-]*)+):?(\d+)?\/?/i', $url)) ? FALSE : TRUE;
    	}
    
    } // EOF
    ?>
    PHP:
     
    krsix, Jul 5, 2010 IP