hi,
I’m having a problem with a curl script when I use it inside of Modx. Are there any good tutorials on using curl with Modx?
My script works fine outside of Modx but when I call as a snippet it fails to work.
thanks,
Dan
The code bellow works fine on its own called from the same server.
<?php
if (!defined(’MPI_PASSWORD’))
{
define(’MPI_PASSWORD’, ’P@ssw0rd’);
}
function get_xml_children($values, &$i)
{
$children = array();
if ($i > -1 && isset($values[$i][’value’]))
{
$children[’_value_’] = $values[$i][’value’];
}
while (++$i < count($values))
{
$type = $values[$i][’type’];
if ($type === ’cdata’)
{
$children[’_value_’] .= $values[$i][’value’];
}
elseif ($type === ’complete’ || $type === ’open’)
{
$name = $values[$i][’tag’];
$tag = get_xml_tag($values[$i], $values, $i, $type);
$children[$name][] = $tag;
}
elseif ($type === ’close’)
{
break;
}
}
// Collapse single tags?
foreach($children as $key => $value)
{
if (is_array($value) && count($value) === 1)
{
$children[$key] = $value[0];
}
}
return $children;
}
function get_xml_tag($thisvalues, $values, &$i, $type)
{
$tag = array();
if (isset($thisvalues[’attributes’]))
{
$tag[’_attributes_’] = $thisvalues[’attributes’];
}
if ($type === ’complete’)
{
if (isset($thisvalues[’value’]))
{
$tag[’_value_’] = $thisvalues[’value’];
}
}
else
{
$tag = array_merge($tag, get_xml_children($values, $i));
}
return $tag;
}
function get_xml_tree($data)
{
$parser = xml_parser_create();
xml_parser_set_option($parser, XML_OPTION_CASE_FOLDING, false);
xml_parser_set_option($parser, XML_OPTION_SKIP_WHITE, true);
xml_parse_into_struct($parser, $data, $values);
xml_parser_free($parser);
$i = -1;
return get_xml_children($values, $i);
}
function xml_entities($str)
{
return strtr($str, array(
’&’ => ’&’,
’"’ => ’"’,
’\’’ => ’'’,
’<’ => ’<’,
’>’ => ’>’));
}
$SiteSecureBaseURL = $_SERVER[’SCRIPT_URI’];
// Function to construct the XML that is required to query the enrollment status with the MPI.
//Settings
$totalPrice = ’1.23’;
$acquirerBIN=’123456’;
$currencyCode=’826’;
$merchantID=’123456789012345’;
$transactionNarrative=’120’;
$XID=’12345678901234567890’;
function get_enrollment_xml()
{
global $acquirerBIN, $currencyCode, $merchantID, $transactionNarrative, $XID, $totalPrice;
$xml = ’<?xml version="1.0" encoding="UTF-8"?>’;
$xml .= ’<Request>’;
$xml .= ’<Enrollment>’;
$xml .= ’<AcquirerBIN>’.xml_entities($acquirerBIN).’</AcquirerBIN>’;
$xml .= ’<Amount>’.xml_entities($totalPrice).’</Amount>’;
$xml .= ’<CurrencyCode>’.xml_entities($currencyCode).’</CurrencyCode>’;
$xml .= ’<ExpiryDateMonth>’.xml_entities(’12’).’</ExpiryDateMonth>’;
$xml .= ’<ExpiryDateYear>’.xml_entities(’2012’).’</ExpiryDateYear>’;
$xml .= ’<MerchantID>’.xml_entities($merchantID).’</MerchantID>’;
$xml .= ’<PAN>’.xml_entities(’4123450131003312’).’</PAN>’;
$xml .= ’<Password>’.xml_entities(MPI_PASSWORD).’</Password>’;
$xml .= ’<TransactionNarrative>’.xml_entities($transactionNarrative).’</TransactionNarrative>’;
$xml .= ’<XID>’.xml_entities($XID).’</XID>’;
$xml .= ’</Enrollment>’;
$xml .= ’</Request>’;
$curl = curl_init();
//header ("content-type: text/xml");
curl_setopt($curl, CURLOPT_HTTPHEADER, array("content-type: text/xml"));
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $xml);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false); // FIXME This should really be true if local host has appropriate root certificate
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); // FIXME This should really be true if local host has appropriate root certificate
curl_setopt($curl, CURLOPT_URL, ’
https://testmpi.cardeasexml.com’);
$result = curl_exec($curl);
curl_close($curl);
return $result;
}
//session_start();
$enrollment_xml = get_xml_tree(get_enrollment_xml());
// Store the POST and XML response data in the session.
// Associate it with the Merchant Data identifier being sent to the Access Control Server.
// For the purposes of this example the Merchant Data identifier is a random number created in this script.
$md = rand();
$_SESSION[’MD’.$md][’enrollment_xml’] = $enrollment_xml;
// If the card holder is not enrolled processing stops.
// The CardHolderEnrolled should mapped and sent to CardEaseXML with a TransactionStatus of ’None’.
if (!isset($enrollment_xml[’Response’][’Enrollment’][’CardHolderEnrolled’][’_value_’]) ||
$enrollment_xml[’Response’][’Enrollment’][’CardHolderEnrolled’][’_value_’] != ’Y’)
{
echo get_enrollment_xml();
//return $output ;
}
// The card holder is enrolled so they should be redirected to the Access Control Server.
// The Access Control Server requires MD, PaReq and TermUrl fields.
// The MD field can be empty if required.
else
{
//$TransactionStatus = ’yes’;
echo get_enrollment_xml();
//echo $TransactionStatus;
//return $output ;
}
?>