drewdan Posted September 29, 2012 Share Posted September 29, 2012 Hi Guys, I am trying to write a script which will automate the creating of accounts for customers on a plesk server. I am having some difficulty though. This script will be used for when I get IPN from Paypal. For the moment, I am just running it to get it to work. It will search tables in a database to get the relevant information and then should create an xml file post it to my server with the relevant details which will create an account, however, when I am trying to create the xml file through the code, and I put the variables into the script, I get errors returned stating their is no data being passed through. Any advice anyone could provide would be very handy! Thanks Andrew <?php include("includes/conn.php"); //get todays date for the payment schedule database date_default_timezone_set(GMT); $n=gmdate("m"); $y= gmdate("Y"); $d=gmdate("d"); $time = mktime(0,0,0,$n,$d,$y); //get ID number of transaction so database can be updated $id="1"; //update the payment schedule and mark this transaction as paid $update_payment_schedule = "update paymentschedule set payment_paid='1' AND payment_paiddate = '$time' where payment_id='$id'"; $update_payment_schedule_res = mysqli_query($conn, $update_payment_schedule) or die(mysqli_error($conn)); //get the customer number from payment schedule $get_customer_number = "select * from paymentschedule where payment_id='$id'"; $get_customer_number_res = mysqli_query($conn, $get_customer_number) or die(mysqli_error($conn)); while($arCustomerNumber=mysqli_fetch_array($get_customer_number_res)) { $customer_number = $arCustomerNumber['customer_id']; } //update the hosting tables $update_hosting = "update hosting set hosting_accountstatus='3' where customer_id='$customer_number'"; $update_hosting_res = mysqli_query($conn, $update_hosting) or die(mysqli_error($conn)); //get_customer_details $get_customer_details = "select * from customer where customer_id='$customer_number'"; $get_customer_details_res = mysqli_query($conn, $get_customer_details) or die(mysqli_error($conn)); while($arCustomerDetails = mysqli_fetch_array($get_customer_details_res)) { $customerfirstname = $arCustomerDetails['customer_firstname']; $customerlastname = $arCustomerDetails['customer_lastname']; $customerfullname = $customer_firstname . " " . $customer_lastname; $customercompanyname = $arCustomerDetails['customer_companyname']; $customerfirstlineaddress = $arCustomerDetails['customer_firstlineaddress']; $customercity = $arCustomerDetails['customer_city']; $customercounty = $arCustomerDetails['customer_county']; $customerpostcode = $arCustomerDetails['customer_postcode']; $customerphone = $arCustomerDetails['customer_phone']; $customeremailaddress = $arCustomerDetails['customer_emailaddress']; } function generatePassword($length=9, $strength=0) { $vowels = 'aeuy'; $consonants = 'bdghjmnpqrstvz'; if ($strength & 1) { $consonants .= 'BDGHJLMNPQRSTVWXZ'; } if ($strength & 2) { $vowels .= "AEUY"; } if ($strength & 4) { $consonants .= '23456789'; } if ($strength & { $consonants .= '@#$%'; } $password = ''; $alt = time() % 2; for ($i = 0; $i < $length; $i++) { if ($alt == 1) { $password .= $consonants[(rand() % strlen($consonants))]; $alt = 0; } else { $password .= $vowels[(rand() % strlen($vowels))]; $alt = 1; } } return $password; } $panel_password = generatePassword(9,4); //--------------------------------------------------------------------------------------------- //script to add new client to the server //--------------------------------------------------------------------------------------------- function addClient() { $xmldoc = new DomDocument('1.0', 'UTF-8'); $xmldoc->formatOutput = true; // <packet> $packet = $xmldoc->createElement('packet'); $packet->setAttribute('version', '1.6.3.0'); $xmldoc->appendChild($packet); // <packet/domain> $client = $xmldoc->createElement('customer'); $packet->appendChild($client); // <packet/domain/get> $add = $xmldoc->createElement('add'); $client->appendChild($add); // <packet/domain/get/filter> $geninfo = $xmldoc->createElement('gen_info'); $add->appendChild($geninfo); $geninfo->appendChild($xmldoc->createElement('cname', $customercompanyname)); $geninfo->appendChild($xmldoc->createElement('pname', $customerfullname)); $geninfo->appendChild($xmldoc->createElement('login', $customeremailaddress)); $geninfo->appendChild($xmldoc->createElement('passwd', $panel_password)); $geninfo->appendChild($xmldoc->createElement('status', '0')); $geninfo->appendChild($xmldoc->createElement('address', $customerfirstlineaddress)); $geninfo->appendChild($xmldoc->createElement('city', $customercity)); $geninfo->appendChild($xmldoc->createElement('pcode', $customerpostcode)); $geninfo->appendChild($xmldoc->createElement('country', 'GB')); return $xmldoc; } //--------------------------------------------------------------------------------------------------------------------------------------- //end of add new client script //-------------------------------------------------------------------------------------------------------------------------------------- function curlInit($host, $login, $password) { $curl = curl_init(); curl_setopt($curl, CURLOPT_URL, "https://{$host}:8443/enterprise/control/agent.php"); curl_setopt($curl, CURLOPT_RETURNTRANSFER, false); curl_setopt($curl, CURLOPT_POST, true); curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false); curl_setopt($curl, CURLOPT_HTTPHEADER, array("HTTP_AUTH_LOGIN: {$login}", "HTTP_AUTH_PASSWD: {$password}", "HTTP_PRETTY_PRINT: TRUE", "Content-Type: text/xml") ); return $curl; } function sendRequest($curl, $packet) { curl_setopt($curl, CURLOPT_POSTFIELDS, $packet); $result = curl_exec($curl); if (curl_errno($curl)) { $errmsg = curl_error($curl); $errcode = curl_errno($curl); curl_close($curl); throw new ApiRequestException($errmsg, $errcode); } curl_close($curl); return $result; } function parseResponse($response_string) { $xml = new SimpleXMLElement($response_string); if (!is_a($xml, 'SimpleXMLElement')) throw new ApiRequestException("Cannot parse server response: {$response_string}"); return $xml; } function checkResponse(SimpleXMLElement $response) { $resultNode = $response->domain->get->result; // check if request was successful if ('error' == (string)$resultNode->status) throw new ApiRequestException("The Panel API returned an error: " . (string)$resultNode->result->errtext); } $host = '*****'; $login = '*****'; $password = '******'; $curl = curlInit($host, $login, $password); try { $response = sendRequest($curl, addClient()->saveXML()); $responseXml = parseResponse($response); checkResponse($responseXml); } catch (ApiRequestException $e) { echo $e; die(); } // Explore the result foreach ($responseXml->xpath('/packet/client/add/result') as $resultNode) { $server_customer_id = (string)$resultNode->id; } $insert_into_domain = "insert into domains values('', '$domainname', '$server_customer_id', 'vrt_host', '82.165.151.28', '$customeremailaddress', '$panelpassword')"; $insert_into_domain_res = mysqli_query($conn, $insert_into_domain) or die(mysqli_error($conn)); ?> Thanks in advance for your help! Quote Link to comment https://forums.phpfreaks.com/topic/268920-variables-are-not-working-in-an-xml-document/ Share on other sites More sharing options...
berridgeab Posted September 30, 2012 Share Posted September 30, 2012 (edited) So the server is responding, however it is actually telling you that you have not submitted the data? Have you run a print_r() on the data that you build before actually sending it to check its being built correctly? Edited September 30, 2012 by berridgeab Quote Link to comment https://forums.phpfreaks.com/topic/268920-variables-are-not-working-in-an-xml-document/#findComment-1381808 Share on other sites More sharing options...
drewdan Posted September 30, 2012 Author Share Posted September 30, 2012 Thanks for your help. I had put an underscore somewhere it shouldnt have been, so the variable it was trying to pass contained no data. Which is why my server wasnt being very happy! As soon as I built the XML up and found the only two variables were actually missing data and not all of them it became nice and simple. Brill! Quote Link to comment https://forums.phpfreaks.com/topic/268920-variables-are-not-working-in-an-xml-document/#findComment-1381814 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.