SitePoint Sponsor

User Tag List

Results 1 to 10 of 10
  1. #1
    SitePoint Member IanArcher's Avatar
    Join Date
    Oct 2011
    Posts
    9
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    What is wrong with this PHP code used to convert css to inline?

    Here is the code

    It's an Open type free use code for convert css to inline style codes by capturing

    from your own html code or a url and it outputs an html code when finished.

    Because i need to send webpages in an e-mail.

    I am brand new to PHP so i wouldn't know where to start to use this code on my

    own separate from the page url below.

    It's the source of this page

    http://premailer.dialect.ca

    Can someone shine some light on what do with the the below code to get the

    result i am seeking? Thanks.


    Code:
    <?php
    /**
     * Premailer API PHP class
     * Premailer is a library/service for making HTML more palatable for various 
    
    inept email clients, in particular GMail
     * Primary function is to convert style tags into equivalent inline styles so styling 
    
    can survive <head> tag removal
     * Premailer is owned by Dialect Communications group
     * @link http://premailer.dialect.ca/api
     * @author Marcus Bointon <marcus@synchromedia.co.uk>
     */
     
    class Premailer {
    	/**
    	 * The Premailer API URL
    	 */
    	const ENDPOINT = 'http://premailer.dialect.ca/api/0.1/documents';
    
    	/**
    	 * Central static method for submitting either an HTML string or a 
    
    URL, optionally retrieving converted versions
    	 * @static
    	 * @throws Exception
    	 * @param string $html Raw HTML source
    	 * @param string $url URL of the source file
    	 * @param bool $fetchresult Whether to also fetch the converted 
    
    output
    	 * @param string $adaptor Which document handler to use (hpricot 
    
    (default) or nokigiri)
    	 * @param string $base_url Base URL for converting relative links
    	 * @param int $line_length Length of lines in the plain text version 
    
    (default 65)
    	 * @param string $link_query_string Query string appended to links
    	 * @param bool $preserve_styles Whether to preserve any link 
    
    rel=stylesheet and style elements
    	 * @param bool $remove_ids Remove IDs from the HTML document?
    	 * @param bool $remove_classes Remove classes from the HTML 
    
    document?
    	 * @param bool $remove_comments Remove comments from the 
    
    HTML document?
    	 * @return array Either a single strclass object containing the decoded 
    
    JSON response, or a 3-element array containing result, html and plain parts if 
    
    $fetchresult is set
    	 */
    	protected static function convert($html = '', $url = 
    
    'http://test2.chamberhome.com/viewPage.php?ID=Newsletter_1', $fetchresult = 
    
    true, $adaptor = 'hpricot', $base_url = '', $line_length = 65, $link_query_string 
    
    = '', $preserve_styles = true, $remove_ids = false, $remove_classes = false, 
    
    $remove_comments = false) {
    		$params = array();
    		if (!empty($html)) {
    			$params['html'] = $html;
    		} elseif (!empty($url)) {
    			$params['url'] = $url;
    		} else {
    			throw new Exception('Must supply an html or url 
    
    value');
    		}
    		if ($adaptor == 'hpricot' or $adaptor == 'nokigiri') {
    			$params['adaptor'] = $adaptor;
    		}
    		if (!empty($base_url)) {
    			$params['base_url'] = $base_url;
    		}
    		$params['line_length'] = (integer)$line_length;
    		if (!empty($link_query_string)) {
    			$params['link_query_string'] = 
    
    $link_query_string;
    		}
    		$params['preserve_styles'] = ($preserve_styles?'true':'false');
    		$params['remove_ids'] = ($remove_ids?'true':'false');
    		$params['$remove_classes'] = 
    
    ($remove_classes?'true':'false');
    		$params['$remove_comments'] = 
    
    ($remove_comments?'true':'false');
    		$options = array(
    			'timeout' => 15,
    			'connecttimeout' => 15,
    			'useragent' => 'PHP Premailer',
    			'ssl' => array('verifypeer' => false, 'verifyhost' => 
    
    false)
    		);
    		$h = new HttpRequest(self::ENDPOINT, 
    
    HttpRequest::METH_POST, $options);
    		$h->addPostFields($params);
    		try {
    			$response = $h->send();
    			$result = json_decode($response->getBody());
    			$code = $response->getResponseCode();
    			if ($code != 201) {
    				switch ($code) {
    					case 400:
    						throw new 
    
    Exception('Content missing', 400);
    						break;
    					case 403:
    						throw new 
    
    Exception('Access forbidden', 403);
    						break;
    					case 500:
    					default:
    						throw new 
    
    Exception('Error', $code);
    				}
    			}
    			$return = array('result' => $result);
    			if ($fetchresult)() {
    				//Get HTML and plain versions in 
    
    parallel
    				http_persistent_handles_clean();
    				$pool = new HttpRequestPool;
    				$pool->attach(new HttpRequest
    
    ($result->documents->html, HttpRequest::METH_GET, $options));
    				$pool->attach(new HttpRequest
    
    ($result->documents->txt, HttpRequest::METH_GET, $options));
    				$pool->send();
    				foreach($pool as $request) {
    					if ($request->getUrl() == 
    
    $result->documents->html) {
    						$return['html'] = 
    
    $request->getResponseBody();
    					} elseif ($request->getUrl() 
    
    == $result->documents->txt) {
    						$return['plain'] = 
    
    $request->getResponseBody();
    					}
    				}
    				return $return;
    			}
    			return $result;
    		} catch (HttpException $e) {
    			var_dump($h, $e->getMessage());
    			return false;
    		}
    	}
    
    	/**
    	 * Central static method for submitting either an HTML string or a 
    
    URL, optionally retrieving converted versions
    	 * @static
    	 * @throws Exception
    	 * @param string $html Raw HTML source
    	 * @param bool $fetchresult Whether to also fetch the converted 
    
    output
    	 * @param string $adaptor Which document handler to use (hpricot 
    
    (default) or nokigiri)
    	 * @param string $base_url Base URL for converting relative links
    	 * @param int $line_length Length of lines in the plain text version 
    
    (default 65)
    	 * @param string $link_query_string Query string appended to links
    	 * @param bool $preserve_styles Whether to preserve any link 
    
    rel=stylesheet and style elements
    	 * @param bool $remove_ids Remove IDs from the HTML document?
    	 * @param bool $remove_classes Remove classes from the HTML 
    
    document?
    	 * @param bool $remove_comments Remove comments from the 
    
    HTML document?
    	 * @return array Either a single element array containing the 'result' 
    
    object, or three elements containing result, html and plain if $fetchresult is set
    	 */
    	public static function html($html, $fetchresult = true, $adaptor = 
    
    'hpricot', $base_url = '', $line_length = 65, $link_query_string = '', 
    
    $preserve_styles = true, $remove_ids = false, $remove_classes = false, 
    
    $remove_comments = false) {
    		return self::convert($html, '', $fetchresult, $adaptor, 
    
    $base_url, $line_length, $link_query_string, $preserve_styles, $remove_ids, 
    
    $remove_classes, $remove_comments);
    	}
    
    	/**
    	 * Central static method for submitting either an HTML string or a 
    
    URL, optionally retrieving converted versions
    	 * @static
    	 * @throws Exception
    	 * @param string $url URL of the source file
    	 * @param bool $fetchresult Whether to also fetch the converted 
    
    output
    	 * @param string $adaptor Which document handler to use (hpricot 
    
    (default) or nokigiri)
    	 * @param string $base_url Base URL for converting relative links
    	 * @param int $line_length Length of lines in the plain text version 
    
    (default 65)
    	 * @param string $link_query_string Query string appended to links
    	 * @param bool $preserve_styles Whether to preserve any link 
    
    rel=stylesheet and style elements
    	 * @param bool $remove_ids Remove IDs from the HTML document?
    	 * @param bool $remove_classes Remove classes from the HTML 
    
    document?
    	 * @param bool $remove_comments Remove comments from the 
    
    HTML document?
    	 * @return array Either a single element array containing the 'result' 
    
    object, or three elements containing result, html and plain if $fetchresult is set
    	 */
    	public static function url($url, $fetchresult = true, $adaptor = 'hpricot', 
    
    $base_url = '', $line_length = 65, $link_query_string = '', $preserve_styles = 
    
    true, $remove_ids = false, $remove_classes = false, $remove_comments = 
    
    false) {
    		return self::convert('', $url, $fetchresult, $adaptor, 
    
    $base_url, $line_length, $link_query_string, $preserve_styles, $remove_ids, 
    
    $remove_classes, $remove_comments);
    	}
    }
    
    /*
    Simplest usage:
    $pre = Premailer::html($var_with_some_html_in);
    $html = $pre['html'];
    $plain = $pre['plain'];
    //Similarly for URLs:
    $pre = Premailer::url($url);
    */
    ?>

  2. #2
    Avid Logophile silver trophy
    ParkinT's Avatar
    Join Date
    May 2006
    Location
    Central Florida
    Posts
    2,282
    Mentioned
    180 Post(s)
    Tagged
    4 Thread(s)
    You need to call this file on a PHP server (almost any hosting service available on the planet).
    Sample usage is at the bottom of the file. So, the simplest way to use this is:
    • remove the comments from that block of code at the bottom (delete the /* at the beginning and the */ at the end)
    • move the last line [$pre = Premailer::url($url)] up to the top of the block of code from which you removed the comments (but just below the other similar line)
    • replace '$url' with the URL of a page you want to modify
    • Call the page (http://yourhosteddomain/whateverthefilenameis.php

    The variables ($html and $plain) will contain the modified text for you

    If you add this code at the end of the same file, the output will be displayed

    Code:
    echo $html;
    Don't be yourself. Be someone a little nicer. -Mignon McLaughlin, journalist and author (1913-1983)


    Literally, the best app for readers.
    Make Your P@ssw0rd Secure
    Leveraging SubDomains

  3. #3
    SitePoint Member IanArcher's Avatar
    Join Date
    Oct 2011
    Posts
    9
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks for the help!

    Here's my code now

    Code:
    <?php
    /**
    * Premailer API PHP class
    * Premailer is a library/service for making HTML more palatable for various inept email clients, in particular GMail
    * Primary function is to convert style tags into equivalent inline styles so styling can survive <head> tag removal
    * Premailer is owned by Dialect Communications group
    * @link http://premailer.dialect.ca/api
    * @author Marcus Bointon <marcus@synchromedia.co.uk>
    */
     
    class Premailer {
    /**
    * The Premailer API URL
    */
    const ENDPOINT = 'http://premailer.dialect.ca/api/0.1/documents';
    
    /**
    * Central static method for submitting either an HTML string or a URL, optionally retrieving converted versions
    * @static
    * @throws Exception
    * @param string $html Raw HTML source
    * @param string $url URL of the source file
    * @param bool $fetchresult Whether to also fetch the converted output
    * @param string $adaptor Which document handler to use (hpricot (default) or nokigiri)
    * @param string $base_url Base URL for converting relative links
    * @param int $line_length Length of lines in the plain text version (default 65)
    * @param string $link_query_string Query string appended to links
    * @param bool $preserve_styles Whether to preserve any link rel=stylesheet and style elements
    * @param bool $remove_ids Remove IDs from the HTML document?
    * @param bool $remove_classes Remove classes from the HTML document?
    * @param bool $remove_comments Remove comments from the HTML document?
    * @return array Either a single strclass object containing the decoded JSON response, or a 3-element array containing result, html and plain parts if $fetchresult is set
    */
    
    
    protected static function convert($html = '', $url = "test2.chamberhome.com/viewPage.php?ID=Newsletter_1", $fetchresult = true, $adaptor = 'hpricot', $base_url = '', $line_length = 65, $link_query_string = '', $preserve_styles = true, $remove_ids = false, $remove_classes = false, $remove_comments = false) {
    $params = array();
    if (!empty($html)) {
    $params['html'] = $html;
    } elseif (!empty($url)) {
    $params['url'] = $url;
    } else {
    throw new Exception('Must supply an html or url value');
    }
    if ($adaptor == 'hpricot' or $adaptor == 'nokigiri') {
    $params['adaptor'] = $adaptor;
    }
    if (!empty($base_url)) {
    $params['base_url'] = $base_url;
    }
    $params['line_length'] = (integer)$line_length;
    if (!empty($link_query_string)) {
    $params['link_query_string'] = $link_query_string;
    }
    $params['preserve_styles'] = ($preserve_styles?'true':'false');
    $params['remove_ids'] = ($remove_ids?'true':'false');
    $params['$remove_classes'] = ($remove_classes?'true':'false');
    $params['$remove_comments'] = ($remove_comments?'true':'false');
    $options = array(
    'timeout' => 15,
    'connecttimeout' => 15,
    'useragent' => 'PHP Premailer',
    'ssl' => array('verifypeer' => false, 'verifyhost' => false)
    );
    $h = new HttpRequest(self::ENDPOINT, HttpRequest::METH_POST, $options);
    $h->addPostFields($params);
    try {
    $response = $h->send();
    $result = json_decode($response->getBody());
    $code = $response->getResponseCode();
    if ($code != 201) {
    switch ($code) {
    case 400:
    throw new Exception('Content missing', 400);
    break;
    case 403:
    throw new Exception('Access forbidden', 403);
    break;
    case 500:
    default:
    throw new Exception('Error', $code);
    }
    }
    $return = array('result' => $result);
    if ($fetchresult) {
    //Get HTML and plain versions in parallel
    http_persistent_handles_clean();
    $pool = new HttpRequestPool;
    $pool->attach(new HttpRequest($result->documents->html, HttpRequest::METH_GET, $options));
    $pool->attach(new HttpRequest($result->documents->txt, HttpRequest::METH_GET, $options));
    $pool->send();
    foreach($pool as $request) {
    if ($request->getUrl() == $result->documents->html) {
    $return['html'] = $request->getResponseBody();
    } elseif ($request->getUrl() == $result->documents->txt) {
    $return['plain'] = $request->getResponseBody();
    }
    }
    return $return;
    }
    return $result;
    } catch (HttpException $e) {
    var_dump($h, $e->getMessage());
    return false;
    }
    }
    
    
    //Simplest usage:
    $pre = file_get_contents(http://test2.chamberhome.com/viewPage.php?ID=Newsletter_1);
    $html = $pre['html'];
    $plain = $pre['plain'];
    
    echo $html;
    
    
    
    
    ?>
    I constantly get errors about parsing for example, with that current code it get the error:
    "Parse error: syntax error, unexpected T_VARIABLE, expecting T_FUNCTION in C:\xampp\htdocs\mydiv\Premailer.php on line 110"

    Line 110 is
    Code:
    $pre = file_get_contents(http://test2.chamberhome.com/viewPage.php?ID=Newsletter_1);

  4. #4
    dooby dooby doo silver trophybronze trophy
    spikeZ's Avatar
    Join Date
    Aug 2004
    Location
    Manchester UK
    Posts
    13,788
    Mentioned
    151 Post(s)
    Tagged
    3 Thread(s)
    You need some quotes around the url

    $pre = file_get_contents('http://test2.chamberhome.com/viewPage.php?ID=Newsletter_1');
    Mike Swiffin - Community Team Advisor
    Only a woman can read between the lines of a one word answer.....

  5. #5
    SitePoint Member IanArcher's Avatar
    Join Date
    Oct 2011
    Posts
    9
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks you, but i am still receiving the same error.

  6. #6
    dooby dooby doo silver trophybronze trophy
    spikeZ's Avatar
    Join Date
    Aug 2004
    Location
    Manchester UK
    Posts
    13,788
    Mentioned
    151 Post(s)
    Tagged
    3 Thread(s)
    also missing a } just above that line (to match the opening { in the class declaration line)
    Mike Swiffin - Community Team Advisor
    Only a woman can read between the lines of a one word answer.....

  7. #7
    SitePoint Member IanArcher's Avatar
    Join Date
    Oct 2011
    Posts
    9
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks for the help. I don't get errors anymore, but when i load the page now i just get a:

    <

    so i added a code to write to a file called echo.htm, and when i open the echo.htm it looks fine but i look at the source code of it and it's still depending on the CSS files from the server instead of inline. i even went offline refreshed the page and it looked completely different, that's how i knew.

    Sorry to bother so much, but please tell me what must i do next or what i'm doing wrong? Thanks

    Code:
    <?php
    /**
    * Premailer API PHP class
    * Premailer is a library/service for making HTML more palatable for various inept email clients, in particular GMail
    * Primary function is to convert style tags into equivalent inline styles so styling can survive <head> tag removal
    * Premailer is owned by Dialect Communications group
    * @link http://premailer.dialect.ca/api
    * @author Marcus Bointon <marcus@synchromedia.co.uk>
    */
     
    class Premailer {
    /**
    * The Premailer API URL
    */
    const ENDPOINT = 'http://premailer.dialect.ca/api/0.1/documents';
    
    /**
    * Central static method for submitting either an HTML string or a URL, optionally retrieving converted versions
    * @static
    * @throws Exception
    * @param string $html Raw HTML source
    * @param string $url URL of the source file
    * @param bool $fetchresult Whether to also fetch the converted output
    * @param string $adaptor Which document handler to use (hpricot (default) or nokigiri)
    * @param string $base_url Base URL for converting relative links
    * @param int $line_length Length of lines in the plain text version (default 65)
    * @param string $link_query_string Query string appended to links
    * @param bool $preserve_styles Whether to preserve any link rel=stylesheet and style elements
    * @param bool $remove_ids Remove IDs from the HTML document?
    * @param bool $remove_classes Remove classes from the HTML document?
    * @param bool $remove_comments Remove comments from the HTML document?
    * @return array Either a single strclass object containing the decoded JSON response, or a 3-element array containing result, html and plain parts if $fetchresult is set
    */
    
    
    protected static function convert($html = '', $url = "test2.chamberhome.com/viewPage.php?ID=Newsletter_1", $fetchresult = true, $adaptor = 'hpricot', $base_url = '', $line_length = 65, $link_query_string = '', $preserve_styles = true, $remove_ids = false, $remove_classes = false, $remove_comments = false) {
    $params = array();
    if (!empty($html)) {
    $params['html'] = $html;
    } elseif (!empty($url)) {
    $params['url'] = $url;
    } else {
    throw new Exception('Must supply an html or url value');
    }
    if ($adaptor == 'hpricot' or $adaptor == 'nokigiri') {
    $params['adaptor'] = $adaptor;
    }
    if (!empty($base_url)) {
    $params['base_url'] = $base_url;
    }
    $params['line_length'] = (integer)$line_length;
    if (!empty($link_query_string)) {
    $params['link_query_string'] = $link_query_string;
    }
    $params['preserve_styles'] = ($preserve_styles?'true':'false');
    $params['remove_ids'] = ($remove_ids?'true':'false');
    $params['$remove_classes'] = ($remove_classes?'true':'false');
    $params['$remove_comments'] = ($remove_comments?'true':'false');
    $options = array(
    'timeout' => 15,
    'connecttimeout' => 15,
    'useragent' => 'PHP Premailer',
    'ssl' => array('verifypeer' => false, 'verifyhost' => false)
    );
    $h = new HttpRequest(self::ENDPOINT, HttpRequest::METH_POST, $options);
    $h->addPostFields($params);
    try {
    $response = $h->send();
    $result = json_decode($response->getBody());
    $code = $response->getResponseCode();
    if ($code != 201) {
    switch ($code) {
    case 400:
    throw new Exception('Content missing', 400);
    break;
    case 403:
    throw new Exception('Access forbidden', 403);
    break;
    case 500:
    default:
    throw new Exception('Error', $code);
    }
    }
    $return = array('result' => $result);
    if ($fetchresult) {
    //Get HTML and plain versions in parallel
    http_persistent_handles_clean();
    $pool = new HttpRequestPool;
    $pool->attach(new HttpRequest($result->documents->html, HttpRequest::METH_GET, $options));
    $pool->attach(new HttpRequest($result->documents->txt, HttpRequest::METH_GET, $options));
    $pool->send();
    foreach($pool as $request) {
    if ($request->getUrl() == $result->documents->html) {
    $return['html'] = $request->getResponseBody();
    } elseif ($request->getUrl() == $result->documents->txt) {
    $return['plain'] = $request->getResponseBody();
    }
    }
    return $return;
    }
    return $result;
    } catch (HttpException $e) {
    var_dump($h, $e->getMessage());
    return false;
    }
    }
    }
    
    //Simplest usage:
    $pre = file_get_contents('http://test2.chamberhome.com/viewPage.php?ID=Newsletter_1');
    $html = $pre['html'];
    $plain = $pre['plain'];
    
    // Write to file
    $f = fopen('echo.htm', 'w');
    fwrite($f, $pre);
    fclose($f);
    
    echo $html;
    
    
    
    
    ?>

  8. #8
    dooby dooby doo silver trophybronze trophy
    spikeZ's Avatar
    Join Date
    Aug 2004
    Location
    Manchester UK
    Posts
    13,788
    Mentioned
    151 Post(s)
    Tagged
    3 Thread(s)
    I cant see where you are actually calling the class......
    Mike Swiffin - Community Team Advisor
    Only a woman can read between the lines of a one word answer.....

  9. #9
    SitePoint Member IanArcher's Avatar
    Join Date
    Oct 2011
    Posts
    9
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Do you think i would need this code?

    Or in the simplest usage section do something like $params because that looks like what activates the conversion. That's really what i'm getting after, activating the conversion.
    Code:
    	/**
    	 * Central static method for submitting either an HTML string or a URL, optionally retrieving converted versions
    	 * @static
    	 * @throws Exception
    	 * @param string $html Raw HTML source
    	 * @param bool $fetchresult Whether to also fetch the converted output
    	 * @param string $adaptor Which document handler to use (hpricot (default) or nokigiri)
    	 * @param string $base_url Base URL for converting relative links
    	 * @param int $line_length Length of lines in the plain text version (default 65)
    	 * @param string $link_query_string Query string appended to links
    	 * @param bool $preserve_styles Whether to preserve any link rel=stylesheet and style elements
    	 * @param bool $remove_ids Remove IDs from the HTML document?
    	 * @param bool $remove_classes Remove classes from the HTML document?
    	 * @param bool $remove_comments Remove comments from the HTML document?
    	 * @return array Either a single element array containing the 'result' object, or three elements containing result, html and plain if $fetchresult is set
    	 */
    	public static function html($html, $fetchresult = true, $adaptor = 'hpricot', $base_url = '', $line_length = 65, $link_query_string = '', $preserve_styles = true, $remove_ids = false, $remove_classes = false, $remove_comments = false) {
    		return self::convert($html, '', $fetchresult, $adaptor, $base_url, $line_length, $link_query_string, $preserve_styles, $remove_ids, $remove_classes, $remove_comments);
    	}
    
    	/**
    	 * Central static method for submitting either an HTML string or a URL, optionally retrieving converted versions
    	 * @static
    	 * @throws Exception
    	 * @param string $url URL of the source file
    	 * @param bool $fetchresult Whether to also fetch the converted output
    	 * @param string $adaptor Which document handler to use (hpricot (default) or nokigiri)
    	 * @param string $base_url Base URL for converting relative links
    	 * @param int $line_length Length of lines in the plain text version (default 65)
    	 * @param string $link_query_string Query string appended to links
    	 * @param bool $preserve_styles Whether to preserve any link rel=stylesheet and style elements
    	 * @param bool $remove_ids Remove IDs from the HTML document?
    	 * @param bool $remove_classes Remove classes from the HTML document?
    	 * @param bool $remove_comments Remove comments from the HTML document?
    	 * @return array Either a single element array containing the 'result' object, or three elements containing result, html and plain if $fetchresult is set
    	 */
    	public static function url($url, $fetchresult = true, $adaptor = 'hpricot', $base_url = '', $line_length = 65, $link_query_string = '', $preserve_styles = true, $remove_ids = false, $remove_classes = false, $remove_comments = false) {
    		return self::convert('', $url, $fetchresult, $adaptor, $base_url, $line_length, $link_query_string, $preserve_styles, $remove_ids, $remove_classes, $remove_comments);
    	}
    }

  10. #10
    SitePoint Member IanArcher's Avatar
    Join Date
    Oct 2011
    Posts
    9
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by spikeZ View Post
    I cant see where you are actually calling the class......
    you're right i wasn't calling the class at all, that's why it didn't work. Can you write me something for that please?


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •