You are here:  » external search on different server


external search on different server

Submitted by wilkins on Tue, 2012-07-10 21:04 in

hi david

Is there anyway of using the externalsearch on a different server to the PT?

Brent

Submitted by support on Wed, 2012-07-11 08:01

Hi Brent,

Provided that the remote server has URL wrappers enabled to that it can use file_get_contents against a URL then you can use searchExternal.php like this:

<?php
  $url 
"http://www.example.com/searchExternal.php?q=Search+Terms";
  print 
file_get_contents($url);
?>

Where www.example.com is the website running Price Tapestry, and "Search+Terms" is the required query with any spaces replaced with "+" as per standard URL encoding...

If URL Wrappers are not enabled CURL may be installed which is an alternative method to access remote URLs, the equivalent code to the above being:

<?php
  $url 
"http://www.example.com/searchExternal.php?q=Search+Terms";
  
$ch curl_init($url);
  
curl_setopt($chCURLOPT_HEADER0);
  
curl_setopt($chCURLOPT_RETURNTRANSFER1);
  
$html curl_exec($ch);
  
curl_close($ch);
  print 
$html;
?>

Hope this helps!

Cheers,
David.
--
PriceTapestry.com

Submitted by Rocket32 on Mon, 2020-08-03 10:26

Can product search be done externally on social sites such as tumblr or wordpress.com? If possible can some sort of code be used to place a product search bar like the google search bar or a product grid from a product external keyword call on others sites?

Submitted by support on Mon, 2020-08-03 10:45

Hi,

If you are able to embed JavaScript called from a remote server it would be possible have the content generated by the external scripts return as JavaScript using document.write(). To test this, create a new file in the top level of your Price Tapestry installation called jsExternal.php containing:

<?php
  header("Content-Type: application/javascript");
?>
document.write("Testing, Testing 1234");

And then on your social / remote site; if you can add:

<script src='https://www.example.com/jsExternal.php'></script>

...and you see "Testing, Testing 1234" appear in its place then it should work - let me know if you wanted to give it a go; however note that the content wouldn't actually appear as part of the page or ordinarily be visible to search engines (although search engines are getting quite clever with rendering JavaScript to see how pages ultimately appear to humans...)

Cheers,
David.
--
PriceTapestry.com

Submitted by Rocket32 on Tue, 2020-08-04 02:36

The java script works perfect on a weebly site. How to get this to show a search bar & product grid with niche products on the grid? Also if I want, content that search engines read, I would have to get html code of the live site.

Submitted by support on Tue, 2020-08-04 08:07

Hi,

Here's a complete version of jsExternal.php to return the HTML for a search box and the default Featured Products:

<?php
  require("includes/common.php");
  $config_baseHREF = "http".(isset($_SERVER["HTTPS"])&&$_SERVER["HTTPS"]?"s":"")."://".$_SERVER["HTTP_HOST"].$config_baseHREF;
  $sql = "SELECT * FROM `".$config_databaseTablePrefix."featured` ORDER BY sequence";
  if (database_querySelect($sql,$rows))
  {
    $sqlNames = array();
    $sqlCase = "CASE normalised_name";
    foreach($rows as $featured)
    {
      $featured["name"] = tapestry_normalise($featured["name"]);
      $sqlNames[] = "'".$featured["name"]."'";
      $sqlCase .= " WHEN '".database_safe($featured["name"])."' THEN ".$featured["sequence"];
    }
    $sqlCase .= " END AS sequence";
    $sqlIn = implode(",",$sqlNames);
    $sql = "SELECT * , MIN( price ) AS minPrice, MAX( price ) AS maxPrice, COUNT( id ) AS numMerchants, ".$sqlCase." FROM `".$config_databaseTablePrefix."products` WHERE normalised_name IN (".$sqlIn.") GROUP BY normalised_name ORDER BY sequence";
    database_querySelect($sql,$rows);
    $featured["products"] = $rows;
    foreach($featured["products"] as $k => $product)
    {
      $featured["products"][$k]["productHREF"] = tapestry_productHREF($product);
      $featured["products"][$k]["reviewHREF"] = tapestry_reviewHREF($product);
    }
  }
  $html = "<p><form action='".$config_baseHREF."search.php'><input type='text' name='q' /><input type='submit' value='Search' /></form></p>";
  ob_start();
  if (isset($featured))
  {
    ?>
    <style type='text/css'>
    .pt_featured {
    }
    .pt_featured_each {
      text-align: center;
      margin-bottom: 40px !important;
    }
    .pt_featured_each div {
      margin-bottom: 0px;
    }
    @media only screen and (max-width: 50em) {
      .pt_featured {
      }
      .pt_featured_each {
        display: inline-block;
        width: 100%;
      }
    }
    @media only screen and (min-width: 50.063em) {
      .pt_featured {
      }
      .pt_featured_each {
        display: inline-block;
        width: 25%;
      }
    }
    .pt_img_thumb {
      height: 100px;
    }
    </style>
    <div class='pt_featured'>
      <?php foreach($featured["products"] as $k=> $product): ?><div class='pt_featured_each'>
        <div>
          <a href='<?php print $product["productHREF"]; ?>'><?php print $product["name"]; ?></a>
        </div>
        <?php if ($product["image_url"]): ?>
          <div>
            <a href='<?php print $product["productHREF"]; ?>'><img class='pt_img_thumb' alt='<?php print translate("Image of"); ?> <?php print htmlspecialchars($product["name"],ENT_QUOTES,$config_charset); ?>' src='<?php print htmlspecialchars($product["image_url"],ENT_QUOTES,$config_charset); ?>' /></a>
          </div>
        <?php endif; ?>
        <div class='pt_s'>
          <span class='pt_i'><?php print ($product["numMerchants"]>1?translate("from")."&nbsp;":""); ?></span>
          <span class='pt_b'><?php print tapestry_price($product["minPrice"]); ?></span>
        </div>
        <?php if ($config_useInteraction): ?>
          <div class='pt_s'>
            <?php if ($product["reviews"]): ?>
              <?php print tapestry_stars($product["rating"],"s"); ?>&nbsp;<a href='<?php print $product["reviewHREF"]; ?>'><?php print $product["reviews"]." ".translate("Reviews"); ?></a>
            <?php else: ?>
              <a href='<?php print $product["reviewHREF"]; ?>'><?php print translate("Review This Product"); ?></a>
            <?php endif; ?>
          </div>
        <?php endif; ?>
        <div>
          <a href='<?php print $product["productHREF"]; ?>'><button class='pt_secondary pt_radius'><?php print ($product["numMerchants"]>1?translate("Compare")." ".$product["numMerchants"]." ".translate("Prices"):translate("More Information")); ?></button></a>
        </div>
      </div><?php endforeach; ?>
     </div>
     <?php
  }
  $html .= ob_get_contents();
  ob_end_clean();
  $html64 = base64_encode($html);
  header("Content-Type: application/javascript");
?>
document.write(atob("<?php print $html64?>"));

The CSS and Featured Products HTML are taken from the Classic template which is standalone responsive layout, and the CSS is namespaced using "pt_" so this shouldn't interfere with anything else on the page. The links, form and button styles will inherit what ever style those elements have in your theme in the social site however you could override these (keeping the namespace pt_) in the CSS section of the above code if you wished.

In terms of capturing the HTML to enter into an HTML section off-site, simply edit the above code and where you have this line just before the JavaScript output begins;

  $html64 = base64_encode($html);

...REPLACE with:

  header("Content-Type: text/plain");print $html;exit();
  $html64 = base64_encode($html);

Then browse directly to jsExternal.php on your Price Tapestry installation and it will just display the raw HTML for you to copy...

Hope this helps!

Cheers,
David.
--
PriceTapestry.com

Submitted by Rocket32 on Wed, 2020-08-05 06:13

Hey David. Every thing works perfect for the java script and the html option. How do i possibly change the featured out to a different selection of products? Maybe a different product sets by name, search, selection or similar. Possibly similar to how featured is or other. I'm asking in regards to a way of sending a different groups of products to a different sites.

Thanks for all the support.
You are Awesome!

Submitted by support on Wed, 2020-08-05 06:48

Hi,

Thank you for your kind words!

Here is an alternative version using the code from the Featured Product Sections mod...

<?php
  require("includes/common.php");
  $config_baseHREF = "http".(isset($_SERVER["HTTPS"])&&$_SERVER["HTTPS"]?"s":"")."://".$_SERVER["HTTP_HOST"].$config_baseHREF;
  $section = (isset($_GET["section"])?$_GET["section"]:exit());
  $sql = "SELECT * FROM `".$config_databaseTablePrefix."featured` WHERE name LIKE '".database_safe($section)."/%' ORDER BY sequence";
  if (database_querySelect($sql,$rows))
  {
    $sqlNames = array();
    $sqlCase = "CASE normalised_name";
    foreach($rows as $featured)
    {
      $featured["name"] = tapestry_normalise(str_replace($section."/","",$featured["name"]));
      $sqlNames[] = "'".$featured["name"]."'";
      $sqlCase .= " WHEN '".database_safe($featured["name"])."' THEN ".$featured["sequence"];
    }
    $sqlCase .= " END AS sequence";
    $sqlIn = implode(",",$sqlNames);
    $sql = "SELECT * , MIN( price ) AS minPrice, MAX( price ) AS maxPrice, COUNT( id ) AS numMerchants, ".$sqlCase." FROM `".$config_databaseTablePrefix."products` WHERE normalised_name IN (".$sqlIn.") GROUP BY normalised_name ORDER BY sequence";
    database_querySelect($sql,$rows);
    $featured["products"] = $rows;
    foreach($featured["products"] as $k => $product)
    {
      $featured["products"][$k]["productHREF"] = tapestry_productHREF($product);
      $featured["products"][$k]["reviewHREF"] = tapestry_reviewHREF($product);
    }
  }
  $html = "<p><form action='".$config_baseHREF."search.php'><input type='text' name='q' /><input type='submit' value='Search' /></form></p>";
  ob_start();
  if (isset($featured))
  {
    ?>
    <style type='text/css'>
    .pt_featured {
    }
    .pt_featured_each {
      text-align: center;
      margin-bottom: 40px !important;
    }
    .pt_featured_each div {
      margin-bottom: 0px;
    }
    @media only screen and (max-width: 50em) {
      .pt_featured {
      }
      .pt_featured_each {
        display: inline-block;
        width: 100%;
      }
    }
    @media only screen and (min-width: 50.063em) {
      .pt_featured {
      }
      .pt_featured_each {
        display: inline-block;
        width: 25%;
      }
    }
    .pt_img_thumb {
      height: 100px;
    }
    </style>
    <div class='pt_featured'>
      <?php foreach($featured["products"] as $k=> $product): ?><div class='pt_featured_each'>
        <div>
          <a href='<?php print $product["productHREF"]; ?>'><?php print $product["name"]; ?></a>
        </div>
        <?php if ($product["image_url"]): ?>
          <div>
            <a href='<?php print $product["productHREF"]; ?>'><img class='pt_img_thumb' alt='<?php print translate("Image of"); ?> <?php print htmlspecialchars($product["name"],ENT_QUOTES,$config_charset); ?>' src='<?php print htmlspecialchars($product["image_url"],ENT_QUOTES,$config_charset); ?>' /></a>
          </div>
        <?php endif; ?>
        <div class='pt_s'>
          <span class='pt_i'><?php print ($product["numMerchants"]>1?translate("from")."&nbsp;":""); ?></span>
          <span class='pt_b'><?php print tapestry_price($product["minPrice"]); ?></span>
        </div>
        <?php if ($config_useInteraction): ?>
          <div class='pt_s'>
            <?php if ($product["reviews"]): ?>
              <?php print tapestry_stars($product["rating"],"s"); ?>&nbsp;<a href='<?php print $product["reviewHREF"]; ?>'><?php print $product["reviews"]." ".translate("Reviews"); ?></a>
            <?php else: ?>
              <a href='<?php print $product["reviewHREF"]; ?>'><?php print translate("Review This Product"); ?></a>
            <?php endif; ?>
          </div>
        <?php endif; ?>
        <div>
          <a href='<?php print $product["productHREF"]; ?>'><button class='pt_secondary pt_radius'><?php print ($product["numMerchants"]>1?translate("Compare")." ".$product["numMerchants"]." ".translate("Prices"):translate("More Information")); ?></button></a>
        </div>
      </div><?php endforeach; ?>
     </div>
     <?php
  }
  $html .= ob_get_contents();
  ob_end_clean();
  $html64 = base64_encode($html);
  header("Content-Type: application/javascript");
?>
document.write(atob("<?php print $html64?>"));

With this version, include the section prefix in the request e.g.

<script src='https://www.example.com/jsExternal.php?section=site1'></script>

...which in this case, would display the Featured Products configured as;

site1/Red Widget
site1/Green Widget
site1/Blue Widget

Cheers,
David.
--
PriceTapestry.com

Submitted by Rocket32 on Thu, 2020-08-06 06:53

How would I modify the following featured code from Ramos Template in the code below? It is on a file. Featured.php is empty, but this is code found for content on homepage.

{code saved}

Submitted by support on Thu, 2020-08-06 10:47

Hi,

You could add a special section name "popular" to use the popular products selection SQL instead of selecting from the Featured Products table. To try this, in the last version of jsExternal.php from above where you have the following code at line 5:

   $sql = "SELECT * FROM `".$config_databaseTablePrefix."featured` WHERE name LIKE '".database_safe($section)."/%' ORDER BY sequence";

...REPLACE with:

   if ($section=="popular")
   {
     $sql2 = "SELECT CONCAT('popular/',name) AS name FROM `".$config_databaseTablePrefix."products` GROUP BY name ORDER BY views DESC LIMIT 3";
   }
   else
   {
     $sql = "SELECT * FROM `".$config_databaseTablePrefix."featured` WHERE name LIKE '".database_safe($section)."/%' ORDER BY sequence";
   }

And then use;

<script src='https://www.example.com/jsExternal.php?section=popular'></script>

Hope this helps!

Cheers,
David.
--
PriceTapestry.com