You are here:  » merging scripts


merging scripts

Submitted by ukdave on Sun, 2009-11-15 21:49 in

Hi David,
I have two snippets of code, the first is the "url signer" which I managed to get working with magic parser. The second is a better caching method than I have previously used, it deletes and replaces cached files that have timed out, eliminating the need to empty the cache manually. Trouble is they work fine on their own but when I try to merge them it all goes wrong. Would you help merge these two snippets of code please. Thanks, Dave.

{code saved}

Submitted by support on Mon, 2009-11-16 10:12

Hi Dave,

What's happening I think is that your cache method is returning the contents of the file rather than the file itself, so all you probably needed to change was $url to "string://".$response in the call to MagicParser_parse(). I've combined them how I think should work below;

{code saved}

Cheers
David.

Submitted by ukdave on Mon, 2009-11-16 17:40

Hi David, thanks for that. Unfortunately I got "Request failed" from the server. I ran the code through zend ide and it came up with:

"Global variable $request was used before it was defined (line 4)"
"Global variable $request was used before it was defined (line 59)"

I wasn't sure what to do so thought it best to ask you.

Thanks again, Dave.

Submitted by support on Mon, 2009-11-16 18:00

Hi Dave,

Ooops - it should be:

  $request = UrlSigner('http://uk.shoppingapis.kelkoo.com', '/V3/productSearch?query=ipod', 'myaffiliateid', 'mykey');
  $response = request_cache($request, $cache_fullpath, $cache_timeout);

(i'd kept the $request variable from the old version)

That should be all it is (although I don't see any use of $request on line 4...)

Cheers,
David.

Submitted by ukdave on Mon, 2009-11-16 19:02

Hi David, I'm not sure that I've followed your guidance correctly because all I am getting is a blank page however a new cache file is being generated every time I refresh the page. If it helps I can email you my Affiliate ID and key. Also, I would like to make a donation via PayPal so if you don't mind would you let me know the email address I should send it to please. Thanks, Dave.

This is what I have so far:

<?php
  
require("MagicParser.php");
  
define('CACHEDIR''./');
  
$request UrlSigner('http://uk.shoppingapis.kelkoo.com''/V3/productSearch?query=ipod''affiliateid''mykey');
  
$cache_filename CACHEDIR.md5($request);
  
$cache_fullpath CACHEDIR.$cache_filename;
  
// Number of seconds until the cache gets stale and is replaced. 172800 = 48hrs
  
$cache_timeout 172800;
  function 
request_cache($url$dest_file$timeout=7200) {
      if(!
file_exists($dest_file) || filemtime($dest_file) < (time()-$timeout)) {
        
$data file_get_contents($url);
        if (
$data === false) return false;
        
$tmpf tempnam('/tmp','YWS');
        
$fp fopen($tmpf,"w");
        
fwrite($fp$data);
        
fclose($fp);
        
rename($tmpf$dest_file);
        } else {
          return 
file_get_contents($dest_file);
        }
      return(
$data);
    }
  function 
UrlSigner($urlDomain$urlPath$partner$key){
    
$URL_sig "hash";
    
$URL_ts "timestamp";
    
$URL_partner "aid";
    
// get the timestamp
    
$time time();
    
// replace " " by "+"
    
$urlPath str_replace(" ""+"$urlPath);
    
// format URL
    
$URLtmp $urlPath "&" $URL_partner "=" $partner "&" $URL_ts "=" $time;
    
// URL needed to create the tokken
    
$s $urlPath "&" $URL_partner "=" $partner "&" $URL_ts "=" $time $key;
    
$tokken base64_encode(pack('H*'md5($s)));
    
$tokken str_replace(array("+""/""="), array(".""_""-"), $tokken);
        
$URLreturn $urlDomain $URLtmp "&" $URL_sig "=" $tokken;
    return 
$URLreturn;
    }
  
$counter=0;
  function 
myRecordHandler($record)
  {
  global 
$counter;
  
//  global $sql;
  
$counter++;
  
$urla $record["PRODUCT"];
  
$urlb $record["OFFER"];
  
$urlc $record["OFFER-TYPE"];
  
$urld $record["OFFER-ID"];
  
$urle $record["OFFER/TITLE"];
  
$urlf $record["OFFER/DESCRIPTION"];
  print 
"$urla";
  print 
"$urlb";
  print 
"$urlc";
  print 
"$urld";
  print 
"$urle";
  print 
"$urlf";
  }
  
$url UrlSigner('http://uk.shoppingapis.kelkoo.com''/V3/productSearch?query=ipod''affiliateid''mykey');
  
$response request_cache($request$cache_fullpath$cache_timeout);
  if (
$response === false) {
      die(
'Request failed');
  }
  
MagicParser_parse("string://".$response,"myRecordHandler","xml|PRODUCTSEARCH/PRODUCTS/PRODUCT/");
?>

Submitted by support on Tue, 2009-11-17 09:35

Hi Dave,

The only thing I can see wrong, and it's not really wrong as supposed to superfluous; is the repeat of the $url = ... line.

What I would do next is add some debug code to print the $response directly instead of handing it to the parser, and then this can be checked against what is expected to be returned...

<?php
  
require("MagicParser.php");
  
define('CACHEDIR''./');
  
$request UrlSigner('http://uk.shoppingapis.kelkoo.com''/V3/productSearch?query=ipod''affiliateid''mykey');
  
$cache_filename CACHEDIR.md5($request);
  
$cache_fullpath CACHEDIR.$cache_filename;
  
// Number of seconds until the cache gets stale and is replaced. 172800 = 48hrs
  
$cache_timeout 172800;
  function 
request_cache($url$dest_file$timeout=7200) {
      if(!
file_exists($dest_file) || filemtime($dest_file) < (time()-$timeout)) {
        
$data file_get_contents($url);
        if (
$data === false) return false;
        
$tmpf tempnam('/tmp','YWS');
        
$fp fopen($tmpf,"w");
        
fwrite($fp$data);
        
fclose($fp);
        
rename($tmpf$dest_file);
        } else {
          return 
file_get_contents($dest_file);
        }
      return(
$data);
    }
  function 
UrlSigner($urlDomain$urlPath$partner$key){
    
$URL_sig "hash";
    
$URL_ts "timestamp";
    
$URL_partner "aid";
    
// get the timestamp
    
$time time();
    
// replace " " by "+"
    
$urlPath str_replace(" ""+"$urlPath);
    
// format URL
    
$URLtmp $urlPath "&" $URL_partner "=" $partner "&" $URL_ts "=" $time;
    
// URL needed to create the tokken
    
$s $urlPath "&" $URL_partner "=" $partner "&" $URL_ts "=" $time $key;
    
$tokken base64_encode(pack('H*'md5($s)));
    
$tokken str_replace(array("+""/""="), array(".""_""-"), $tokken);
        
$URLreturn $urlDomain $URLtmp "&" $URL_sig "=" $tokken;
    return 
$URLreturn;
    }
  
$counter=0;
  function 
myRecordHandler($record)
  {
  global 
$counter;
  
//  global $sql;
  
$counter++;
  
$urla $record["PRODUCT"];
  
$urlb $record["OFFER"];
  
$urlc $record["OFFER-TYPE"];
  
$urld $record["OFFER-ID"];
  
$urle $record["OFFER/TITLE"];
  
$urlf $record["OFFER/DESCRIPTION"];
  print 
"$urla";
  print 
"$urlb";
  print 
"$urlc";
  print 
"$urld";
  print 
"$urle";
  print 
"$urlf";
  }
  
$response request_cache($request$cache_fullpath$cache_timeout);
  if (
$response === false) {
      die(
'Request failed');
  }
  
header("Content-Type: text/plain;");
  print 
$response;
  
// MagicParser_parse("string://".$response,"myRecordHandler","xml|PRODUCTSEARCH/PRODUCTS/PRODUCT/");
?>

Cheers,
David.

Submitted by ukdave on Tue, 2009-11-17 21:55

Hi David, we are getting there!

I made a few changes which seem to have helped but there is still a problem.

1. As soon as I shifted the line "$request = UrlSigner(etc etc);" downward the cache began to work correctly.
2. changed $timeout to $cache_timeout
3. Changed $url to $request

Ok so the cache is now working perfectly. A single cached file is being generated and is being replaced exactly at "$cache_timeout = 100;". To be certain I downloaded the cached copy and modified a line by hand then put it back on the server. After exactly 100 seconds the modified file updated correctly.

In my browser "print $response;" displays the full xml as expected however when I comment it back out and decomment the "MagicParser_parse();" line all I get, in internet explorer, is the following error message, which unfortunately I don't know how to fix:

"XML document must have a top level element. Error processing resource"

This is what I now have:

<?php
  
require("MagicParser.php");
  
define('CACHEDIR''./');
  
$cache_filename CACHEDIR.md5($request);
  
$cache_fullpath CACHEDIR.$cache_filename;
  
// Number of seconds until the cache gets stale and is replaced. 172800 = 48hrs
  
$cache_timeout 100;
       function 
request_cache($request$dest_file$cache_timeout) {
      if(!
file_exists($dest_file) || filemtime($dest_file) < (time()-$cache_timeout)) {
        
$data file_get_contents($request);
        if (
$data === false) return false;
        
$tmpf tempnam('/tmp','YWS');
        
$fp fopen($tmpf,"w");
        
fwrite($fp$data);
        
fclose($fp);
        
rename($tmpf$dest_file);
        } else {
          return 
file_get_contents($dest_file);
        }
      return(
$data);
    }
  function 
UrlSigner($urlDomain$urlPath$partner$key){
    
$URL_sig "hash";
    
$URL_ts "timestamp";
    
$URL_partner "aid";
    
// get the timestamp
    
$time time();
    
// replace " " by "+"
    
$urlPath str_replace(" ""+"$urlPath);
    
// format URL
    
$URLtmp $urlPath "&" $URL_partner "=" $partner "&" $URL_ts "=" $time;
    
// URL needed to create the tokken
    
$s $urlPath "&" $URL_partner "=" $partner "&" $URL_ts "=" $time $key;
    
$tokken base64_encode(pack('H*'md5($s)));
    
$tokken str_replace(array("+""/""="), array(".""_""-"), $tokken);
        
$URLreturn $urlDomain $URLtmp "&" $URL_sig "=" $tokken;
    return 
$URLreturn;
    }
  
$request UrlSigner('http://uk.shoppingapis.kelkoo.com''/V3/productSearch?query=ipod''affiliateid''mykey');
  
$counter=0;
  function 
myRecordHandler($record)
  {
  global 
$counter;
  
//  global $sql;
  
$counter++;
  
$urla $record["PRODUCT"];
  
$urlb $record["OFFER"];
  
$urlc $record["OFFER-TYPE"];
  
$urld $record["OFFER-ID"];
  
$urle $record["OFFER/TITLE"];
  
$urlf $record["OFFER/DESCRIPTION"];
  print 
"$urla";
  print 
"$urlb";
  print 
"$urlc";
  print 
"$urld";
  print 
"$urle";
  print 
"$urlf";
  }
  
$response request_cache($request$cache_fullpath$cache_timeout);
  if (
$response === false) {
      die(
'Request failed');
  }
  
header("Content-Type: text/plain;");
  print 
$response;
  
//  MagicParser_parse("string://".$response,"myRecordHandler","xml|PRODUCTSEARCH/PRODUCTS/PRODUCT/");
?>

Thanks again, Dave.

Submitted by support on Wed, 2009-11-18 11:32

Hi Dave,

Could you perhaps capture the $response (using print $response;) save it to a text file and email it to me and i'll check it out...!

Thanks,
David.