1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

[Help] post data into another php function?

Discussion in 'PHP & Perl' started by ljt3759, Feb 9, 2012.

Tags:
  1. ljt3759

    ljt3759 Regular Member

    Joined:
    Nov 18, 2009
    Messages:
    228
    Likes Received:
    100
    Occupation:
    Entrepreneur
    Location:
    Lewis-Tisdall.com
    Home Page:
    I'm making a search engine, and I need the words that are inputted in the search form to be posted into a php function for example:

    someone searches harry potter I would like it to be posted into the keywords bit, would that simply mean the search engine form would be

    PHP:
    <form action="search.php" method="post">
    <
    input type="text" name="searchterm" />
    <
    input type="submit" />
    </
    form>
    PHP:
    <?php $Amazon=new Amazon();  $parameters=array( "region"=>"com"
    "AssociateTag"=>'affiliateTag''ResponseGroup'=>'Images',  "Operation"=>"ItemSearch",
     
    "SearchIndex"=>"Books",  "Keywords"=>"harry potter" );  
    $queryUrl=$Amazon->getSignedUrl($parameters);  echo $queryUrl?>

    therefore the
    PHP:
     "keywords"=>"$_POST["searchterm"] 
    Could anyone help me with this? I only know certain bits of PHP and not that much. Any help would be great. :D

    - Lewis
     
    Last edited: Feb 9, 2012
  2. madoctopus

    madoctopus Supreme Member

    Joined:
    Apr 4, 2010
    Messages:
    1,249
    Likes Received:
    3,498
    Occupation:
    Full time IM
    have you even tried? you'd know if you tried. yes i think that's basically what you want, but sanitize the input. NEVER use the values in $_POST, $_GET unsanitized it is a security risk.

    Also it is "keywords"=>$_POST["searchterm"]

    Without the quote before $_POST
     
    • Thanks Thanks x 1
  3. ljt3759

    ljt3759 Regular Member

    Joined:
    Nov 18, 2009
    Messages:
    228
    Likes Received:
    100
    Occupation:
    Entrepreneur
    Location:
    Lewis-Tisdall.com
    Home Page:
    I've tried without luck, I know some bits are right and some aren't, thanks for that.

    I'm trying to get my head round where this bit goes (as in under the same page as the search page or under search.php or a completely new php file?

    PHP:
     <?php $Amazon=new Amazon();  $parameters=array( "region"=>"com"
    "AssociateTag"=>'affiliateTag''ResponseGroup'=>'Images',  "Operation"=>"ItemSearch",
     
    "SearchIndex"=>"Books",   "keywords"=>$_POST["searchterm"]   );  
    $queryUrl=$Amazon->getSignedUrl($parameters);  echo $queryUrl?>  
    I forgot to include that this code goes in somewhere too:

    PHP:
    <?php class Amazon {      // public key     var $publicKey = "xxxxxxxxxxxxxx";     // private key     var $privateKey = "xxxxxxxxxxxxx";     // affiliate tag
         
    var $affiliateTag='affiliateTag';          /**         *Get a signed URL         *@param string $region used to define country      
       *@param array $param used to build url         *@return array $signature returns the signed string and its components         */  
       
    public function generateSignature($param)     {         // url basics         $signature['method']='GET';         $signature['host']='ecs.amazonaws.'.$param['region'];         $signature['uri']='/onca/xml';          // necessary parameters         $param['Service'] = "AWSECommerceService"; 
            
    $param['AWSAccessKeyId'] = $this->publicKey;         $param['Timestamp'] = gmdate("Y-m-d\TH:i:s\Z");         $param['Version'] = '2009-10-01';         ksort($param);         foreach ($param as $key=>$value)         {             $key str_replace("%7E""~"rawurlencode($key));          
       
    $value str_replace("%7E""~"rawurlencode($value));             $queryParamsUrl[] = $key."=".$value;         }   
         
    // glue all the  "params=value"'s with an ampersand         $signature['queryUrl']= implode("&", $queryParamsUrl);          // we'll use this string to make the signature         $StringToSign = $signature['method']."\n".$signature['host']."\n".$signature['uri']."\n".$signature['queryUrl'];  
           // make signature         $signature['string'] = str_replace("%7E", "~",             rawurlencode(                 base64_encode(                     hash_hmac("sha256",$StringToSign,$this->privateKey,True                     )                 )             )         );         return $signature;     }         /**         * Get signed url response         * @param string $region         * @param array $params         * @return string $signedUrl a query url with signature         */     public function getSignedUrl($params)     {         $signature=$this->generateSignature($params);          return $signedUrl= "http://".$signature['host'].$signature['uri'].'?'.$signature['queryUrl'].'&Signature='.$signature['string'];     } } 
    ?>
    I've tried looking into php classes, but im not sure if it goes into a seperate php file or what?

    I think i just need to know which of those two goes where (under what file?)

    Sorry about this, but the help is greatly appreciated.
     
  4. madoctopus

    madoctopus Supreme Member

    Joined:
    Apr 4, 2010
    Messages:
    1,249
    Likes Received:
    3,498
    Occupation:
    Full time IM
    you put second code (the class) in a separate file called for example Amazon.class.php and include it in your actual script (where you have ' = new Amazon()') with:

    require_once 'Amazon.class.php';

    First PHP block of code goes in search.php - exact location depends on when it should be executed - before or after what.
     
  5. ljt3759

    ljt3759 Regular Member

    Joined:
    Nov 18, 2009
    Messages:
    228
    Likes Received:
    100
    Occupation:
    Entrepreneur
    Location:
    Lewis-Tisdall.com
    Home Page:
    Thanks a lot, I've got that all working now. If I profit from this you should have a pm asking for your paypal email :D
     
  6. madoctopus

    madoctopus Supreme Member

    Joined:
    Apr 4, 2010
    Messages:
    1,249
    Likes Received:
    3,498
    Occupation:
    Full time IM
    no need. i'm glad u got it working
     
  7. nixon5

    nixon5 Newbie

    Joined:
    Feb 4, 2012
    Messages:
    13
    Likes Received:
    0
    ljt3759 irc isn't up right now n I totally don't have the 15 posts I need lol so i sent you an email regarding the php and website and things