Jump to content

Sending and Receive XML requests via Remote URL


monkeytooth

Recommended Posts

Alright, im fried, and I think my options are limited as far as searching. More so as I don't know where to start with this concept per say.

What I need to do is send a preformed/predefined XML request to a remote URL, the remote URL will the take the information provided, and form a new XML request to send back.

 

Putting the XML together to send I can do, getting it to read the XML that comes back I think I can do if I can get it to send. However the connection, the bridge that brings the data back and forth is eluding me at the moment. I don't know if I should use fopen, curl, or what to do what I need.

 

What I am working with is the USPS API's trying to build Delivery Confirmation/Shipping Labels. API info can be found here: http://www.usps.com/webtools/htm/Delivery-Confirmation1.htm

 

Has anyone here ever worked with the USPS API's? I could use all the help I can get on this one. But as of the moment mostly with connecting and sending of the XML data they want. Maybe something to help gather the xml data they send back.

 

Thanks in advance for any and all help.

Ok, I finally found something that I can gauge an base off of and work with, and have been working with. However I am still stuck..

 

The Class:

<?php

class usps {

	public $account = 'xxxxxxxxxxxxx';
	public $url = 'https://secure.shippingapis.com/ShippingAPITest.dll';
	public $person, $company, $address1, $address2, $city, $state, $zip;
	public $ship_name, $ship_company, $ship_address1, $ship_address2, $ship_city, $ship_state, $ship_zip;
	public $to_name, $to_company, $to_address1, $to_address2, $to_city, $to_state, $to_zip;

function toXML() {
	$xml = ' <DelivConfirmCertifyV3.0Request USERID="' . $this->account . '">';
	$xml .= '<Option>1</Option>';
	$xml .= '<ImageParameters></ImageParameters>';
	$xml .= '<FromName>' . $this->person . '</FromName>';
	$xml .= '<FromAddress1>' . $this->address1 . '</FromAddress1>';
	$xml .= '<FromAddress2>' . $this->address2 . '</FromAddress2>';
	$xml .= '<FromCity>' . $this->city . '</FromCity>';
	$xml .= '<FromState>' . $this->state . '</FromState>';
	$xml .= '<FromZip5>' . $this->zip . '</FromZip5>';
	$xml .= '<FromZip4></FromZip4>';
	$xml .= '<ToName>' . $this->to_name . '</ToName>';
	$xml .= '<ToAddress1>' . $this->to_address1 . '</ToAddress1>';
	$xml .= '<ToAddress2>' . $this->to_address2 . '</ToAddress2>';
	$xml .= '<ToCity>' . $this->to_city . '</ToCity>';
	$xml .= '<ToState>' . $this->to_state . '</ToState>';
	$xml .= '<ToZip5>' . $this->to_zip . '</ToZip5>';
	$xml .= '<ToZip4></ToZip4>';
	$xml .= '<WeightInOunces>60</WeightInOunces>';
	$xml .= '<ServiceType>Media Mail</ServiceType>';

	$xml .= '<ImageType>PDF</ImageType>';
	$xml .= '<LabelDate></LabelDate>';


	/*if ($this->ship_address2 <> ''){
		//shipping address
		$xml .= '<Address ID="2">';
		$xml .= '<Address1>' . $this->ship_address1 . '</Address1>';
		$xml .= '<Address2>' . $this->ship_address2 . '</Address2>';
		$xml .= '<City>' . $this->ship_city . '</City>';
		$xml .= '<State>' . $this->ship_state . '</State>';
		$xml .= '<Zip5>' . $this->ship_zip . '</Zip5>';
		$xml .= '<Zip4></Zip4></Address>';
		}*/

	$xml .= '</DeliveryConfirmationV3.0Request>';

     return $xml;
     }

function submit_request() {

	$ch = curl_init($this->url);
	curl_setopt($ch, CURLOPT_POST, 1);
	curl_setopt($ch, CURLOPT_POSTFIELDS, "API=DeliveryConfirmationV3&XML=" . $this->toXML());
	curl_setopt($ch, CURLOPT_TIMEOUT, 60);
	curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
	//curl_setopt($ch, CURLOPT_SSL_VERIFYPEER,false);

	$result = curl_exec($ch);
	$error = curl_error($ch);

	if(empty($error)) {
		return $result;
	}else{
		die(curl_error($ch));
	}
}
}

?>

 

The PHP Page:

<?php

require("usps2.php");
	///public $person, $company, $address1, $address2, $city, $state, $zip;
	///public $ship_name, $ship_company, $ship_address1, $ship_address2, $ship_city, $ship_state, $ship_zip;
	///public $to_name, $to_company, $to_address1, $to_address2, $to_city, $to_state, $to_zip;

$uspsRequest = new usps(); //class instantiation
$uspsRequest->person = 'Christopher Hacia';
$uspsRequest->address1 = 'Apt 2';
$uspsRequest->address2 = '39 Concord St';
$uspsRequest->city = 'New Britain';
$uspsRequest->state = 'CT';
$uspsRequest->zip = '06053';

$uspsRequest->to_name = 'Jacinta Handley';
$uspsRequest->to_address1 = 'Apt 3';
$uspsRequest->to_address2 = '39 Concord St';
$uspsRequest->to_city = 'New Britain';
$uspsRequest->to_state = 'CT';
$uspsRequest->to_zip = '06053';
/*
//optional second address
$uspsRequest->ship_address1 = '';
$uspsRequest->ship_address2 = '';
$uspsRequest->ship_city = '';
$uspsRequest->ship_state = '';
$uspsRequest->ship_zip = '';
*/

$result = $uspsRequest->submit_request();

if (!empty($result)){
	$xml = new SimpleXMLElement($result);
}else{
	die;
}


if(isset($xml->Address[0]->Error)) { echo ' Error Address 1';}
if(isset($xml->Address[1]->Error)) { echo ' Error Address 2';}
header ("content-type: text/xml");
echo $xml->Address[0]->Address2 . ' ' . $xml->Address[0]->Address1 ; //line 49
echo '<br />';
echo $xml->Address[0]->City. ' ' . $xml->Address[0]->State  . ' ' . $xml->Address[0]->Zip5; //line 51
//echo '<br />';
//echo $xml->Address[1]->Address2 . ' ' . $xml->Address[1]->Address1 ;
//echo '<br />';
//echo $xml->Address[1]->City. ' ' . $xml->Address[1]->State  . ' ' . $xml->Address[1]->Zip5;

?>

 

The Errors:

Notice:  Trying to get property of non-object in /home/.../usps1.php on line 49

Notice:  Trying to get property of non-object in /home/.../usps1.php on line 51

noted above.. which is which

 

I can't figure out the error of my ways..

 

I am attempting to build a Shipping Label Class based on the APIs provided in this document http://www.usps.com/webtools/htm/Delivery-Confirmation1.htm

 

Now that I have more to work with, is anyone able to help me pin point my problem(s)?

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.