Log in or register to post comments

How to modify the sample web services PHP code to pass variables to the classes?

March 31, 2018 - 2:22pm #2

Hello all. Non-PHP coder here -- please be gentle... :-)

We're working on modifying the Vuforia Web Services sample PHP code to integrate with our platform. It works perfectly as long as the server access keys, target IDs, etc. are hard-coded as private variables in the php files (as they are in the sample code).

Obviously, we need to dynamically pass different access keys, target IDs, and such to the classes, replacing the hard-coded private variables. Supposedly, the constructor function is the way to do that in PHP, but we have not gotten that to work (nor using global variables).

To get the image target data from the Vuforia web services API, we are starting with a "Vuforia_GetTarget.php" file that reads the necessary server key and target ID variables as form field submissions. Here's that code:



  $ServerAccessKey = $_POST["ServerAccessKey"];   

  $ServerSecretKey = $_POST["ServerSecretKey"];

  $TargetID = $_POST["TargetID"];  


require_once 'GetTarget.php';

$objGetTarget = new GetTarget();


We then want to slightly modify the GetTarget.php file in the sample code to replace the hard-coded private variables with the data from the form submission.

Here's that code. You can see our commented-out attempts to replace the hard-coded data.


require_once 'HTTP/Request2.php';

require_once 'SignatureBuilder.php';

// See the Vuforia Web Services Developer API Specification - https://developer.vuforia.com/resources/dev-guide/retrieving-target-cloud-database

// The GetTarget sample demonstrates how to query a single target by target id.

class GetTarget{


//Server Keys


private $access_key  = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";

private $secret_key  = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";

private $targetId  = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";   


    //public function __construct($ServerAccessKey, $ServerSecretKey, $TargetID) {    



//global $ServerAccessKey;

//global $ServerSecretKey;

//global $TargetID;   


private $url   = "https://vws.vuforia.com";

private $requestPath = "/targets/";// . $targetId;

private $request;

function GetTarget(){  


  $this->requestPath = $this->requestPath . $this->targetId;




private function execGetTarget(){    


  $this->request = new HTTP_Request2();

  $this->request->setMethod( HTTP_Request2::METHOD_GET );



    'ssl_verify_peer' => false



  $this->request->setURL( $this->url . $this->requestPath );


  // Define the Date and Authentication headers




  try {


   $response = $this->request->send();


   if (200 == $response->getStatus()) {

    echo $response->getBody();

   } else {

    echo 'Unexpected HTTP status: ' . $response->getStatus() . ' ' .

      $response->getReasonPhrase(). ' ' . $response->getBody();


  } catch (HTTP_Request2_Exception $e) {

   echo 'Error: ' . $e->getMessage();





private function setHeaders(){

  $sb =  new SignatureBuilder();

  $date = new DateTime("now", new DateTimeZone("GMT"));

  // Define the Date field using the proper GMT format

  $this->request->setHeader('Date', $date->format("D, d M Y H:i:s") . " GMT" );

  // Generate the Auth field value by concatenating the public server access key w/ the private query signature for this request

  $this->request->setHeader("Authorization" , "VWS " . $this->access_key . ":" . $sb->tmsSignature( $this->request , $this->secret_key ));





We'd be most grateful if any real PHP coders could point us in the right direction. Many thanks!

How to modify the sample web services PHP code to pass variables to the classes?

March 31, 2018 - 10:58pm #1

We have resolved this. It is a matter of having the php file read the server keys and target IDs, etc., as form fields. Then using  "public function __construct()" to read the form fields, and finally passing the values as arguments to the various functions in the code.

Log in or register to post comments