Jump to content

External Links in CronJob


Greenaj04

Recommended Posts

Ok,

So I have a php script which I want to call using a cron job... It all works correctally when I call it from a browser, and I have updated the local paths to include the full url.

 

All the local side of things works correctally/ fine. The issue is that during part of the script I am pulling an image from an external source (specifically Apple). I'm then using ImageMagik to convert it to a png and save the file locally. Again this all works outside of using a cron job, but dosen't when I am using a cron job...

 

It's either an issue with the fact I'm using an external link, or an issue with utilizing ImageMagik...

 

If it's a ImageMagik issue then I'll ask on their forums - but I first wanted to check there shouldn't be any issues with using external links in a php triggered by cron? Like it dosent try find the file at /usr/www/users/<external link here> or anything dumb like that?

 

Cheers,

- Adam G

Link to comment
Share on other sites

Ok, heres the portion of relevant code.

I've narrowed it down to not being an external linking problem... It's something to do with ImageMagik I believe, as it's pulling the icons, it's just not pulling the screenshots (which have varying formats which I need ImageMagik to deal with, where as the icons are all pngs).

 

Essentially it pulls an xml from apple to get the "screenshot" link. For refrence here is an example apple/ iTunes ID: 343200656

 

So if you go to http://ax.itunes.apple.com/WebObjects/MZStoreServices.woa/wa/wsLookup?id=343200656 then apple list the

 

 

$url = "http://ax.itunes.apple.com/WebObjects/MZStoreServices.woa/wa/wsLookup?id=" . $iTunesID;
$handle = fopen($url, "r");
$iTunesData = fread($handle, 99999);
$price = get_string_between($iTunesData, '"price":', ', "sellerUrl"');
$iconLink = get_string_between($iTunesData, '"artworkUrl60":"', '", "artworkUrl100":');
$ssLink = get_string_between($iTunesData, '"screenshotUrls":[', ',');
if(strlen($ssLink) < 10){
	$ssLink2 = get_string_between($iTunesData, '"ipadScreenshotUrls":[', ',');
		if (strpos($ssLink2, 'jpg') !== false) {
		$format = "jpg";	
	}
	if (strpos($ssLink2, 'png') !== false) {
		$format = "png";
	}
	if (strpos($ssLink2, 'tif') !== false) {
		$format = "tif";
	}		
	$actualSSLink2 = get_string_between($ssLink2, 'http', $format);
	$screenshotLink = 'http' . $actualSSLink2 . "." . $format;
//	echo "<br>iPadURL <br>";
//	echo "Screenshot Base is : ". $ssLink2 . " ";
//	echo "Screenshot Final is : ". $screenshotLink . " ";
}
else{
	if (strpos($ssLink, 'jpg') !== false) {
		$format = "jpg";	
	}
	if (strpos($ssLink, 'png') !== false) {
		$format = "png";
	}
	if (strpos($ssLink, 'tif') !== false) {
		$format = "tif";
	}		
	$actualSSLink = get_string_between($ssLink, 'http', $format);
	$screenshotLink = 'http' . $actualSSLink . $format;

}   

if($price == "0.00")
{
	//echo "gen icon at" . $location . " Using Image: " . $iconLink;
	generateImage($iconLink, "free", $location);
	$length=strlen($screenshotLink);
	if($length > 10)
	{
		//echo "gen screenshot at" . $SLoc . " Using Image: " . $screenshotLink;
		generateScreenShot($screenshotLink, $SLoc);
	}
}

 

So it sorts out the format, returns the link and passes it into the generateScreenShot function which is simply:

 

 

function generateScreenShot($imageLink, $location){

    $p = new phMagick($imageLink,$location);
$p->convert();
}

 

With this include at the top of the whole script:

 

    include "phmagick.php";

 

Then phmagik.php is a plugin script which interfaces with a PEARL script what came pre-installed with my host. (It's quite long, but I've tried also changing the path refrences in the plugin php too)...

 

 

The full phmagik.php code is:

<?php
/*
    +--------------------------------------------------------------------------------------------+
    |   DISCLAIMER - LEGAL NOTICE -                                                              |
    +--------------------------------------------------------------------------------------------+
    |                                                                                            |
    |  This program is free for non comercial use, see the license terms available at            |
    |  http://www.francodacosta.com/licencing/ for more information                              |
    |                                                                                            |
    |  This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; |
    |  without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
    |                                                                                            |
    |  USE IT AT YOUR OWN RISK                                                                   |
    |                                                                                            |
    |                                                                                            |
    +--------------------------------------------------------------------------------------------+

*/
/**
* phMagick - Image manipulation with Image Magick
*
* @version    0.4.1
* @author     Nuno Costa - sven@francodacosta.com
* @copyright  Copyright (c) 2007
* @license    LGPL
* @link       http://www.francodacosta.com/phmagick
* @since      2008-03-13
*/
class phmagick{
    private $availableMethods = array();
    private $loadedPlugins = array();

    private $escapeChars = null ;

    private $history = array();
    private $originalFile = '';
    private $source = '';
    private $destination = '';
    private $imageMagickPath = '';
    private $imageQuality = 80 ;

    public $debug = false;
    private $log = array();

    function __construct($sourceFile='', $destinationFile=''){
        $this->originalFile = $sourceFile;

        $this->source = $sourceFile ;
        $this->destination = $destinationFile;

        if(is_null($this->escapeChars) ){
            $this->escapeChars = !( strtolower ( substr( php_uname('s'), 0, 3))  == "win" ) ;
        }

        $this->loadPlugins();
    }


    public function getLog(){
        return $this->log;
    }
    public function getBinary($binName){
        return $this->getImageMagickPath()  . $binName ;
    }

    //-----------------
     function setSource ($path){
        $this->source = str_replace(' ','\ ',$path) ;
        return $this ;
    }

     function getSource (){
        return $this->source ;
    }

    //-----------------
     function setDestination ($path){
        $path = str_replace(' ','\ ',$path) ;
        $this->destination = $path ;
        return $this;
    }

     function getDestination (){
        if( ($this->destination == '')){
            $source = $this->getSource() ;
            $ext = end (explode('.', $source)) ;
            $this->destinationFile = dirname($source) . '/' . md5(microtime()) . '.' . $ext;
        }
        return $this->destination ;
    }

    //-----------------

    function setImageMagickPath ($path){
        if($path != '')
            if ( strpos($path, '/') < strlen($path))
                $path .= '/';
        $this->imageMagickPath = str_replace(' ','\ ',$path) ;
    }

     function getImageMagickPath (){
        return $this->imageMagickPath;
    }
    //-----------------
     function setImageQuality($value){
        $this->imageQuality = intval($value);
        return $this;
    }

     function getImageQuality(){
        return $this->imageQuality;
    }

    //-----------------

    function getHistory( $type = Null ){
        switch ($type){

            case phMagickHistory::returnCsv:
                return explode(',', array_unique($this->history));
                break;

            default:
            case phMagickHistory::returnArray :
                return array_unique($this->history) ;
                break;

        }
    }

    public function setHistory($path){
        $this->history[] = $path ;
        return $this;
    }

    public function clearHistory(){
        unset ($this->history);
        $this->history = array();
    }


    public function requirePlugin($name, $version=null){

        if(key_exists($name, $this->loadedPlugins)) {
            if(! is_null($version)) {
               if( property_exists($this->loadedPlugins[$name], 'version') ){
                    if($this->loadedPlugins[$name]->version > $version)
                       return true;

                    if($this->debug) throw new phMagickException ('Plugin "'.$name.'" version ='.$this->loadedPlugins[$name]->version . ' required >= ' . $version);
               }
            }
            return true ;
        }

        if($this->debug) throw new phMagickException ('Plugin "'.$name.'" not found!');
        return false;
    }

    //-----------------

    private function loadPlugins(){
        $base = dirname(__FILE__) . '/plugins';
        $plugins = glob($base . '/*.php');
        foreach($plugins as $plugin){
            include_once $plugin ;
            $name = basename($plugin, '.php');
            $className = 'phMagick_'.$name ;
            $obj = new $className();
            $this->loadedPlugins[$name] = $obj ;
            foreach (get_class_methods($obj) as $method )
                 $this->availableMethods[$method] = $name ;
        }
    }


    public function execute($cmd){

        $ret = null ;
        $out = array();

        if($this->escapeChars) {
            $cmd= str_replace    ('(','\(',$cmd);
            $cmd= str_replace    (')','\)',$cmd);
        }
        exec( $cmd .' 2>&1', $out, $ret);

        if($ret != 0)
            if($this->debug) trigger_error (new phMagickException ('Error executing "'. $cmd.'" <br>return code: '. $ret .' <br>command output :"'. implode("<br>", $out).'"' ), E_USER_NOTICE );

        $this->log[] = array(
            'cmd' => $cmd
            ,'return' => $ret
            ,'output' => $out
        );

        return $ret ;
    }

    public function __call($method, $args){
        if(! key_exists($method, $this->availableMethods))
           throw new Exception ('Call to undefined method : ' . $method);

           array_unshift($args, $this);
           $ret = call_user_func_array(array($this->loadedPlugins[$this->availableMethods[$method]], $method), $args);

           if($ret === false)
              throw new Exception ('Error executing method "' . $method ."'");

           return $ret ;
    }
}

class phMagickHistory{
    const returnArray = 0 ;
    const returnCsv   = 1 ;

    private function __construct(){}
}


class phMagickException extends Exception {

    function __construct($message, $code=1){
        //parent::__construct('', $code);
        $this->message($message);
    }
    function message($message){
        echo '<br><b>phMagick</b>: ' . $message ;
    }
}


class phMagickGravity{
    const None      = 'None' ;
    const Center    = 'Center' ;
    const East      = 'East' ;
    const Forget    = 'Forget' ;
    const NorthEast = 'NorthEast' ;
    const North     = 'North' ;
    const NorthWest = 'NorthWest' ;
    const SouthEast = 'SouthEast' ;
    const South     = 'South' ;
    const SouthWest = 'SouthWest' ;
    const West      = 'West' ;

    private function __construct(){}
}


class phMagickTextObjectDefaults{
    public static $fontSize ='12';
    public static $font = false;

    public static $color = '#000';
    public static $background = false;

    public static $gravity = phMagickGravity::Center; //ignored in fromString()
    public $Text = '';

    private function __construct(){}
}


class phMagickTextObject {
    protected $fontSize;
    protected $font;

    protected $color;
    protected $background;

    protected $pGravity; //ignored in fromString()
    protected $pText = '';

    public function __construct(){
        $this->fontSize   = phMagickTextObjectDefaults::$fontSize;
        $this->font       = phMagickTextObjectDefaults::$font;
        $this->color      = phMagickTextObjectDefaults::$color ;
        $this->background = phMagickTextObjectDefaults::$background;
        $this->pGravity   = phMagickTextObjectDefaults::$gravity;
    }

    function defaultFontSize($value){
        phMagickTextObjectDefaults::$fontSize = $value;
    }

    function defaultFont($value){
        phMagickTextObjectDefaults::$font = $value;
    }

    function defaultColor($value){
        phMagickTextObjectDefaults::$color = $value;
    }

    function defaultBackground($value){
        phMagickTextObjectDefaults::$background = $value;
    }

    function defaultGravity($value){
        phMagickTextObjectDefaults::$gravity = $value;
    }



    function fontSize($i){
        $this->fontSize = $i ;
        return $this;
    }

    function font($i){
        $this->font = $i ;
        return $this;
    }

    function color($i){
        $this->color = $i ;
        return $this;
    }

    function background($i){
        $this->background = $i ;
        return $this;
    }

    function __get($var){
        return $this->$var ;
    }

    function gravity( $gravity){
        $this->pGravity = $gravity;
        return $this ;
    }

    function text( $text){
        $this->pText = $text;
        return $this ;
    }
}

?>

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.