Using Volusion API to Import Order Data - php

I've been trying to use the Volusion API to import order data with no luck and support through Volusion is limited. I tend to lean towards a PHP solution, and their web site has this example for using the API for Import with php...
<?php
// Create the Xml to POST to the Webservice
$Xml_to_Send = "<?xml version=\"1.0\" encoding=\"utf-8\" ?>";
$Xml_to_Send .= "<Volusion_API>";
$Xml_to_Send .= "<!--";
$Xml_to_Send .= " xml input file for \"\"";
$Xml_to_Send .= "-->";
$Xml_to_Send .= "</Volusion_API>";
// Create the Header
$url = "https://www.xxxxxcomusa.com/net/WebService.aspx?Login=xxxxx#xxxxcomusa.com &EncryptedPassword=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx&Import=Insert";
$header = "POST".$url." HTTP/1.0 \r\n";
$header .= "MIME-Version: 1.0 \r\n";
$header .= "Content-type: text/xml; charset=utf-8 \r\n";
$header .= "Content-length: ".strlen($post_string)." \r\n";
$header .= "Content-transfer-encoding: text \r\n";
$header .= "Request-number: 1 \r\n";
$header .= "Document-type: Request \r\n";
$header .= "Interface-Version: Test 1.4 \r\n";
$header .= "Connection: close \r\n\r\n";
$header .= $Xml_to_Send;
// Post and Return Xml
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 4);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $header);
$data = curl_exec($ch);
// Check for Errors
if (curl_errno($ch)){
print curl_error($ch);
} else {
curl_close($ch);
}
// Display the Xml Returned on the Browser
echo $data;
?>
I've edited this accordingly in order to get an order updated (used "Import=Update") but there is no 'data' output on the resulting page and the record does not get updated. I am able to get data returned from the api using a similar url so I know my encrypted password is correct. I think I also determined that the line that reads...
$header = "POST".$url." HTTP/1.0 \r\n";
should read ...
$header = "POST ".$url." HTTP/1.0 \r\n";
with a space after POST but I'm still not able to successfully update a record. I was hoping someone might have had success and can share more specifics on how you were able to import an update using the Volusion API (and hopefully PHP).

Instead of trying with CUSTOMREQUEST you can use below one. It sends your headers appropriately.
First make the header as below:
$header = array(
"MIME-Version: 1.0",
"Content-type: text/xml; charset=utf-8",
"Content-transfer-encoding: text",
"Request-number: 1",
"Document-type: Request",
"Interface-Version: Test 1.4"
);
After that make your post request as below with the above header.
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $Xml_to_Send);
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);

Here is the code I ended up with but as I mention above it appears that Import=Update does not work with Volusion Order data, but similar code should work when trying to use the Volusion API and PHP to import. Thanks again for your help Sabuj!
<?php
$file = file_get_contents('mydir/singleorder.txt', true);
// Create the Xml to POST to the Webservice
$Xml_to_Send = "<?xml version=\"1.0\" encoding=\"utf-8\" ?>";
$Xml_to_Send .= "<Volusion_API>";
// $Xml_to_Send .= "<!--";
$Xml_to_Send .= $file;
// $Xml_to_Send .= "\"\"";
// $Xml_to_Send .= "-->";
$Xml_to_Send .= "</Volusion_API>";
$url = "https://www.xxxxxusa.com/net/WebService.aspx?Login=xxxx#xxxxxxxcom.com&EncryptedPassword=0000000000000000000000000000xxxxxxxxxx&Import=Update";
// Create the Header
$header = array(
"MIME-Version: 1.0",
"Content-type: text/xml; charset=utf-8",
"Content-transfer-encoding: text",
"Request-number: 1",
"Document-type: Request",
"Interface-Version: Test 1.4"
);
// Post and Return Xml
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $Xml_to_Send);
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
$data = curl_exec($ch);
// Check for Errors
if (curl_errno($ch)){
print curl_error($ch);
} else {
curl_close($ch);
}
// Display the Xml Returned on the Browser
echo $data;
?>

First you should echo your file.
Because I was having the same issue, the remaining code is correct but may be your file path is wrong and doesn't get by PHP so thats why the records is not inserted.
When I changed the directory path is like this..
$file = file_get_contents('singleorder.txt', true);
It will be always in the same directory.
It started work fine. So try this for solution.

Related

echo json_encode response is not working with mail function

I have used curl for generated the report and send mail to user.
here is my code
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, count($post));
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($post));
$ret = curl_exec($ch);
$ret = json_decode($ret, true);
curl_close($ch);
print_r($ret);
here i am using my mail function and return json encode response but nothing get in response.
if i comment send_report_to_user function then i am able to get response
$to="test#gmail.com";
$name="Test";
$report_links='MYREPORT LINKS';
send_report_to_user($to,$name,$report_links);
echo json_encode(array('status'=>'success','message'=>"Report has been generated successfully. Please check your mail to view reports."));
die;
function send_report_to_user($to,$name,$report_links)
{
$mailheaders .= "MIME-Version: 1.0\r\n";
$mailheaders .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
$mailheaders .= "From: Mcs <do_not_reply#mcs.com>";
$subject = "Your Report is Ready";
$body_message='<p>Your Report is now ready to be viewed here:</p>
<p>Click Here to view report</p>
<p> </p>';
mail($to,$subject,$body_message,$mailheaders);
return true;
}
I think you have issue with your send_report_to_user function. You are using variable $mailheaders but have not defined it. Try defining the variable before using it and it may work for you.
Please try with below code
function send_report_to_user($to,$name,$report_links)
{
$mailheaders = '';
$mailheaders .= "MIME-Version: 1.0\r\n";
$mailheaders .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
$mailheaders .= "From: Mcs <do_not_reply#mcs.com>";
$subject = "Your Report is Ready";
$body_message='<p>Your Report is now ready to be viewed here:</p>
<p>Click Here to view report</p>
<p> </p>';
mail($to,$subject,$body_message,$mailheaders);
return true;
}

sending insert request to volusion API does not work and return 1

It return just "success1". here is my code and xml file. Please anyone help me why the product is not imported to volusion. When i run this code it just return 1. i don't know what is the problem and product is not inserted.
<Import>
<Products>
<ProductID>360</ProductID>
<productcode>10035</productcode>
<vendor_partno>035</vendor_partno>
<productname>Asus Video Card ATI Doulat</productname>
<listprice>6.95</listprice>
<productprice>2</productprice>
<vendor_price>3.83</vendor_price>
<stockstatus>100</stockstatus>
<upc_code>99999</upc_code>
<categoryids>107,134</categoryids>
<productdescriptionshort />
<productdescription />
<productweight>0.00</productweight>
<freeshippingitem />
<minqty />
<maxqty />
</Products>
</Import>
My Api request code
// Create the Xml to POST to the Webservice
$Xml_to_Send = "<?xml version=\"1.0\" encoding=\"utf-8\" ?>";
$Xml_to_Send .= "<Volusion_API>";
// $Xml_to_Send .= "<!--";
$Xml_to_Send .= $file;
// $Xml_to_Send .= "\"\"";
// $Xml_to_Send .= "-->";
$Xml_to_Send .= "</Volusion_API>";
$url = "http://tebkq.mvlce.servertrust.com/net/WebService.aspxLogin=mylogin&EncryptedPassword=mypassword&Import=Insert";
// Create the Header
$header = array(
"MIME-Version: 1.0",
"Content-type: text/xml; charset=utf-8",
"Content-transfer-encoding: text",
"Request-number: 1",
"Document-type: Request",
"Interface-Version: Test 1.4"
);
// Post and Return Xml
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $Xml_to_Send);
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
$data = curl_exec($ch);
//echo $data;
// Check for Errors
if (curl_errno($ch)){
echo "error";
print curl_error($ch);
} else {
echo "success";
curl_close($ch);
}
// Display the Xml Returned on the Browser
echo $data;
?>
Remove <Import></Import> and it should work.
In your xml, try replacing the import node with xmldata:
<?xml version="1.0" encoding="utf-8" ?>
<xmldata>
<Products>
<ProductID>360</ProductID>
.....
</Products>
</xmldata>
This is what I use for a header when sending inserts or updates
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type:application/x-www-form-urlencoded; charset=utf-8", "Content-Action:Volusion_API"));

How to Add/Import/Insert new products to Volusion using Volusion API in PHP with cURL

Could you help me to understand how to add new products to Volusion using the Volusion API in PHP with something like cURL?
Any useful links will be appreciated.
Make XML file in the format like this with name "myXML.txt".
<Import>
<Products>
<ProductID>360</ProductID>
<productcode>10035</productcode>
<vendor_partno>035</vendor_partno>
<productname>Asus Video Card ATI Doulat</productname>
<listprice>6.95</listprice>
<productprice>2</productprice>
<vendor_price>3.83</vendor_price>
<stockstatus>100</stockstatus>
<upc_code>99999</upc_code>
<categoryids>107,134</categoryids>
<productdescriptionshort />
<productdescription />
<productweight>0.00</productweight>
<freeshippingitem />
<minqty />
<maxqty />
</Products>
</Import>
Then copy and paste this in your editor
<?php
$file = file_get_contents('myXML.txt', true);
// Create the Xml to POST to the Webservice
$Xml_to_Send = "<?xml version=\"1.0\" encoding=\"utf-8\" ?>";
$Xml_to_Send .= "<Volusion_API>";
// $Xml_to_Send .= "<!--";
$Xml_to_Send .= $file;
// $Xml_to_Send .= "\"\"";
// $Xml_to_Send .= "-->";
$Xml_to_Send .= "</Volusion_API>";
$url = "https://www.xxxxxusa.com/net/WebService.aspx?Login=xxxx#xxxxxxxcom.com&EncryptedPassword=0000000000000000000000000000xxxxxxxxxx&Import=Update";
// Create the Header
$header = array(
"MIME-Version: 1.0",
"Content-type: text/xml; charset=utf-8",
"Content-transfer-encoding: text",
"Request-number: 1",
"Document-type: Request",
"Interface-Version: Test 1.4"
);
// Post and Return Xml
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $Xml_to_Send);
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
$data = curl_exec($ch);
// Check for Errors
if (curl_errno($ch)){
print curl_error($ch);
} else {
curl_close($ch);
}
// Display the Xml Returned on the Browser
echo $data;
?>
Remember this use your own API credential. Try this hope you got the answer.

How to parse curl response when header is returned

So what I am trying to do, is parse every "node" in the response for lack of better words. It should echo the "tag" followed by a ": " then the value. What I have tried is in the "Code" section. What gets returned from the current code is in the "Response" section. I'm trying to learn from others code and this is where the "Display headers / body / response for debugging and certification purposes" code comes from. I'm just trying to modify it to echo the parsed rawResponse when it clearly does not.
Response:
headers:
body:
rawResponse:AC700000005332001VI4788250000028291sBn551664C1A088C3242DF9CD5C92D52A8FA9EDD532D10100 N097857Approved00N013731
OrderID: sBn5
TxRefNum: 51664C1A088C3242DF9CD5C92D52A8FA9EDD532D
RespCode: 00
AVSRespCode:
CVV2RespCode: N
AuthCode: 097857
Code:
<?php
$xml_data = "<Request><NewOrder>";
$xml_data .= "<IndustryType>EC</IndustryType>";
$xml_data .= "<MessageType>AC</MessageType>";
$xml_data .= "<BIN>000002</BIN>";
$xml_data .= "<AccountNum>".$varCC."</AccountNum>";
$xml_data .= "<Exp>".$varExpiry."</Exp>";
$xml_data .= "<CardSecVal>".$varCVV."</CardSecVal>";
$xml_data .= "<OrderID>sBn5</OrderID>";
$xml_data .= "<Amount>".$varAmt."</Amount>";
$xml_data .= "</NewOrder></Request>";
$URL = "https://orbitalvar1.paymentech.net";
// Build header as array for cURL option
$header = "POST /AUTHORIZE HTTP/1.0\r\n";
$header.= "MIME-Version: 1.0\r\n";
$header.= "Content-type: application/PTI46\r\n";
$header.= "Content-length: ".strlen($xml_data)."\r\n";
$header.= "Content-transfer-encoding: text\r\n";
$header.= "Request-number: 1\r\n";
$header.= "Document-type: Request\r\n";
$header.= "Interface-Version: Test 1.4\r\n";
$header.= "Connection: close \r\n\r\n";
$header.= $xml_data;
// Define cURL options, then connect to server while saving response
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$URL);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_FRESH_CONNECT, TRUE);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $header);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$rawResponse = curl_exec($ch);
if (curl_errno($ch)) {
print curl_error($ch);
} else {
curl_close($ch);
}
/////////////////////////////////////////////////////////////////////////////
// Display headers / body / response for debugging and certification purposes
/////////////////////////////////////////////////////////////////////////////
echo "headers:<BR>\n";
foreach($header as $value) echo " $value<BR>\n";
echo "<BR>body:<BR>\n $body<BR><BR><HR>\n";
echo "<BR>rawResponse:<BR>\n $rawResponse<BR><BR>\n";
// Parse rawResponse XML to response array, then only display important values
$parser = xml_parser_create('UTF-8');
xml_parse_into_struct($parser, $rawResponse, $response);
//var_dump($response);
foreach($response as $value){
switch($value["tag"]){
case "ORDERID": echo "OrderID: ".$value["value"]."<BR>\n"; break;
case "RESPCODE": echo "RespCode: ".$value["value"]."<BR>\n"; break;
case "AVSRESPCODE": echo "AVSRespCode: ".$value["value"]."<BR>\n"; break;
case "CVV2RESPCODE": echo "CVV2RespCode: ".$value["value"]."<BR>\n"; break;
case "TXREFNUM": echo "TxRefNum: ".$value["value"]."<BR>\n"; break;
case "AUTHCODE": echo "AuthCode: ".$value["value"]."<BR>\n"; break;
}
}unset($value);
?>
You neeed separate http headers from http body. It is a "\r\n\r\n" snting. So you can find it like this:
$p = strpos($rawResponse, "\r\n\r\n");
if( $p !== false ) {
$rawHeades = substr($rawResponse, 0, $p);
$rawBody = substr($rawResponse, $p + 4);
}
and you need some thing after print curl_error($ch); - in this case there isn't any true data in $rawResponse

php - how to be able to ajax post and return from this script?

I'm quite stuck in how to edit this script be able to take a request from jquery and return to me the value the script returns? Any help is most gratefully appreciated?
I can use it to pass an xml file and url from the filesystem and get the return I expect but would really like to be able to use it as a proxy to a wsdl service.
[edit] Due to environmental changes I'm wanting to use the below script to pass back to me the xml return from a wsdl service that I'm passing xml. It works fine when I do so from the command line - I'm just unsure how to be able to get the script to accept a post and then return the xml return from the service call.
I'm using the script as follows from the command line:
php file.php theurl <test.xml
^^ Which returns to me the xml I would like to then pass back to the frontend
<?php
ini_set('display_errors', "1");
$url= $argv[1];
echo "url\n$url\n";
preg_match("/https?:\/\/([^\/]*)(.*)/", $url, $matches);
$host=$matches[1];
$request=$matches[2];
$mxml=fread(STDIN,65536);
$yt =curl_init();
$header = "POST $request HTTP/1.0\r\n";
$header .= "Host: $host\r\n";
$header .= "SoapAction:";
$header .= "Content-Type: text/xml\r\n";
$header .= "Content-Length: ".strlen($mxml)."\r\n";
$header .= "Content-Transfer-Encoding: text\r\n";
$header .= "Connection-Close: close\r\n\r\n";
echo "header\n$header\n";
$header .= $mxml;
curl_setopt($yt, CURLOPT_SSL_VERIFYPEER,0);
curl_setopt($yt, CURLOPT_URL, $url);
curl_setopt($yt, CURLOPT_CUSTOMREQUEST, $header);
curl_setopt($yt, CURLOPT_RETURNTRANSFER, true);
$rxml=curl_exec($yt);
echo "sent\n$mxml\n";
echo "received\n$rxml\n";
echo curl_error($yt);
?>
Sussed it. Sorry forgot to post answer. Hopefully help someone else:
<?php
$url = 'http://myURL';
$xmlpost = file_get_contents('php://input');
$header = "POST $url HTTP/1.0\r\n";
$header .= "Host: myHOST\r\n";
$header .= "SoapAction: ''";
$ch = curl_init();
//Set the URL
curl_setopt($ch, CURLOPT_URL, $url);
//Enable POST data
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $xmlpost);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $header);
//curl_exec automatically writes the data returned
$response = curl_exec($ch);
curl_close($ch);
echo $response;
?>
$.post('path/to/php/', function(data)
{
$('body').append('<div id="outputs"></div>');
$('#outputs').html($(data).find('name_of_xml_node').text());
}, 'xml');
maybe.. can't guarantee this one, without at the least maybe some sample data.. as well as this would be more example to work with rather than actually based off what your getting for xml data..
Also you want your PHP to output the XML only no extra strings or anything else like that, that can break the formatting of XML

Categories