Jump to content

Custom hook to pass product custom fields via HTTP POST as XML


Recommended Posts

Hey guys,

 

I've been stumped the past few days trying to figure out how to retrieve an order's custom field values and then HTTP POST them as XML to our 3rd party API.

 

Since WHMCS doesn't include product custom fields as variables in it's hooks, I had to resort to querying MYSQL.

 

I was able to retrieve the values, but I can't pass them in XML.

 

Would appreciate any help!

 

Thanks

 

Tom

Link to comment
Share on other sites

Thanks for the reply.

 

The problem is, I can't seem to post my XML and get a response.

 

Here's my full code:

 

<?php

function client_afterorder_hook( $vars ) {

$orderid = $vars['orderid'];
$ordernumber = $vars['ordernumber'];

$sql    = "SELECT tblcustomfieldsvalues.value
		   FROM tblcustomfieldsvalues
		   INNER JOIN tblhosting
		   ON tblcustomfieldsvalues.relid=tblhosting.id
		   WHERE tblhosting.orderid=$orderid";

$result = mysql_query( $sql );

$data = array();
while ( $row = mysql_fetch_array( $result )) {

	$data[] = $row;

}

$xml = '<?xml version="1.0" encoding="UTF-8"?>
			<body>
				<node>' . $data[0]['value'] . '</node>
				<node>' . $data[1]['value'] . '</node>
				<node>' . $data[2]['value'] . '</node>
				<node>' . $data[3]['value'] . '</node>
				<node>' . $data[4]['value'] . '</node>
				<node>' . $data[5]['value'] . '</node>
				<node>' . $data[6]['value'] . '</node>
				<node>' . $data[7]['value'] . '</node>
			</body>';

$url = 'http://myurl.com';

$options = array( 
	'HEADER' => array( 'Content-Type' => 'text/xml' )
	);

$response = curlCall( $url, $xml, $options );

}

add_hook( "ShoppingCartCheckoutCompletePage", 3, "client_afterorder_hook" );

?>

 

If you just echo $data then you will get an array of the custom fields' values, even if the customer orders multiple items.

 

My problem is, I cant seem to get the request to go through, and I can't get a response.

 

BONUS QUESTION: How would I send multiple curl posts, 1 for each product ordered (if there's multiple)?

 

Thanks!

Edited by tbsweet52
Link to comment
Share on other sites

Hello,

 

i have made modifications to your code, please apply and give it a try, the response should be printed to your admin Activity Log (for debug of-course) and your data should be sent also, from the remote file you need to parse $_POST['data'] as a response.

 

<?php


function client_afterorder_hook( $vars ) {


   $orderid = $vars['orderid'];
   $ordernumber = $vars['ordernumber'];


   $sql    = "SELECT `tblcustomfieldsvalues`.`value`
              FROM `tblcustomfieldsvalues`
              INNER JOIN `tblhosting`
              ON `tblcustomfieldsvalues`.`relid`=`tblhosting`.`id`
              WHERE `tblhosting`.`orderid`={$orderid}";


   $result = mysql_query($sql);
   if (mysql_error()!==''){
       localAPI("logactivity", array("description" => mysql_error()), "admin"); // Print into Activity Log if Error
   }


   $data = array();
   while ( $row = mysql_fetch_array( $result )) {


       $data[] = $row;


   }


   $xml = '<?xml version="1.0" encoding="UTF-8"?>
               <body>
                   <node>' . $data[0]['value'] . '</node>
                   <node>' . $data[1]['value'] . '</node>
                   <node>' . $data[2]['value'] . '</node>
                   <node>' . $data[3]['value'] . '</node>
                   <node>' . $data[4]['value'] . '</node>
                   <node>' . $data[5]['value'] . '</node>
                   <node>' . $data[6]['value'] . '</node>
                   <node>' . $data[7]['value'] . '</node>
               </body>';


   localAPI("logactivity", array("description" => $xml), "admin"); // Print Results to Activity Log


   $url = 'http://myurl.com';


   $options = array(
       'HEADER' => "Content-Type: text/xml; charset=utf-8"
   );


   $response = curlCall( $url, array("data" => $xml), $options );


}


add_hook( "ShoppingCartCheckoutCompletePage", 3, "client_afterorder_hook" );


?>

Link to comment
Share on other sites

Hey Thanks for the reply.

 

I tried that code and it did work a little bit better. The debugging is going into the Activity Log, but I'm still seeing zero response from the XML POST.

 

I keep getting a blank line in the Activity Log right after the $xml debug, which Im assuming is the curlCall?

 

I feel like there is one little thing missing and I'm stumped!

Link to comment
Share on other sites

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.   Paste as plain text instead

  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

By using this site, you agree to our Terms of Use & Guidelines and understand your posts will initially be pre-moderated