Skip to content

Commit c88c314

Browse files
committed
added usps label
1 parent 80e90d8 commit c88c314

File tree

3 files changed

+255
-4
lines changed

3 files changed

+255
-4
lines changed

USPSBase.php

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,9 @@
1010
* @author Vincent Gabriel
1111
*/
1212
class USPSBase {
13-
const LIVE_API_URL = 'http://production.shippingapis.com/ShippingAPI.dll';
14-
const TEST_API_URL = 'http://testing.shippingapis.com/ShippingAPITest.dll';
13+
const LIVE_API_URL = 'https://secure.shippingapis.com/ShippingAPI.dll';
14+
const TEST_API_URL = 'https://secure.shippingapis.com/ShippingAPITest.dll';
15+
1516
/**
1617
* @var string - the usps username provided by the usps website
1718
*/
@@ -67,7 +68,8 @@ class USPSBase {
6768
'CityStateLookup' => 'CityStateLookupRequest',
6869
'TrackV2' => 'TrackFieldRequest',
6970
'FirstClassMail' => 'FirstClassMailRequest',
70-
'SDCGetLocations' => 'SDCGetLocationsRequest'
71+
'SDCGetLocations' => 'SDCGetLocationsRequest',
72+
'ExpressMailLabel' => 'ExpressMailLabelRequest',
7173
);
7274
/**
7375
* Default options for curl.
@@ -77,7 +79,7 @@ class USPSBase {
7779
CURLOPT_RETURNTRANSFER => true,
7880
CURLOPT_TIMEOUT => 60,
7981
CURLOPT_FRESH_CONNECT => 1,
80-
CURLOPT_PORT => 80,
82+
CURLOPT_PORT => 443,
8183
CURLOPT_USERAGENT => 'usps-php',
8284
CURLOPT_FOLLOWLOCATION => true,
8385
CURLOPT_RETURNTRANSFER => true,

USPSPriorityLabel.php

Lines changed: 209 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,209 @@
1+
<?php
2+
/**
3+
* Load required classes
4+
*/
5+
require_once('USPSBase.php');
6+
7+
/**
8+
*/
9+
class USPSPriorityLabel extends USPSBase {
10+
/**
11+
* @var string - the api version used for this type of call
12+
*/
13+
protected $apiVersion = 'ExpressMailLabel';
14+
/**
15+
* @var array - route added so far.
16+
*/
17+
protected $fields = array();
18+
/**
19+
* Perform the API call.
20+
* @return string
21+
*/
22+
public function createLabel() {
23+
// Add missing required
24+
$this->addMissingRequired();
25+
26+
// Sort them
27+
// Hack by the only way this will work properly
28+
// since usps wants the tags to be in
29+
// a certain order
30+
ksort($this->fields, SORT_NUMERIC);
31+
32+
// remove the \d. from the key
33+
foreach($this->fields as $key => $value) {
34+
$newKey = str_replace('.', '', $key);
35+
$newKey = preg_replace('/\d+\:/', '', $newKey);
36+
unset($this->fields[$key]);
37+
$this->fields[$newKey] = $value;
38+
}
39+
40+
return $this->doRequest();
41+
}
42+
43+
/**
44+
* Return the USPS confirmation/tracking number if we have one
45+
* @return string|bool
46+
*/
47+
public function getConfirmationNumber() {
48+
$response = $this->getArrayResponse();
49+
// Check to make sure we have it
50+
if(isset($response['ExpressMailLabelResponse'])) {
51+
if(isset($response['ExpressMailLabelResponse']['EMConfirmationNumber'])) {
52+
return $response['ExpressMailLabelResponse']['EMConfirmationNumber'];
53+
}
54+
}
55+
56+
return false;
57+
}
58+
59+
/**
60+
* Return the USPS label as a base64 encoded string
61+
* @return string|bool
62+
*/
63+
public function getLabelContents() {
64+
$response = $this->getArrayResponse();
65+
// Check to make sure we have it
66+
if(isset($response['ExpressMailLabelResponse'])) {
67+
if(isset($response['ExpressMailLabelResponse']['EMLabel'])) {
68+
return $response['ExpressMailLabelResponse']['EMLabel'];
69+
}
70+
}
71+
72+
return false;
73+
}
74+
75+
/**
76+
* returns array of all fields added
77+
* @return array
78+
*/
79+
public function getPostFields() {
80+
return $this->fields;
81+
}
82+
83+
/**
84+
* Set the from address
85+
* @param string $firstName
86+
* @param string $lastName
87+
* @param string $company
88+
* @param string $address
89+
* @param string $city
90+
* @param string $state
91+
* @param string $zip
92+
* @param string $address2
93+
* @param string $zip4
94+
* @param string $phone
95+
* @return object
96+
*/
97+
public function setFromAddress($firstName, $lastName, $company, $address, $city, $state, $zip, $address2=null, $zip4=null, $phone=null) {
98+
$this->setField(5, 'FromFirstName', $firstName);
99+
$this->setField(6, 'FromLastName', $lastName);
100+
$this->setField(7, 'FromFirm', $company);
101+
$this->setField(8, 'FromAddress1', $address2);
102+
$this->setField(9, 'FromAddress2', $address);
103+
$this->setField(10, 'FromCity', $city);
104+
$this->setField(11, 'FromState', $state);
105+
$this->setField(12, 'FromZip5', $zip);
106+
$this->setField(13, 'FromZip4', $zip4);
107+
$this->setField(14, 'FromPhone', $phone);
108+
109+
return $this;
110+
}
111+
112+
/**
113+
* Set the to address
114+
* @param string $firstName
115+
* @param string $lastName
116+
* @param string $company
117+
* @param string $address
118+
* @param string $city
119+
* @param string $state
120+
* @param string $zip
121+
* @param string $address2
122+
* @param string $zip4
123+
* @param string $phone
124+
* @return object
125+
*/
126+
public function setToAddress($firstName, $lastName, $company, $address, $city, $state, $zip, $address2=null, $zip4=null, $phone=null) {
127+
$this->setField(15, 'ToFirstName', $firstName);
128+
$this->setField(16, 'ToLastName', $lastName);
129+
$this->setField(17, 'ToFirm', $company);
130+
$this->setField(18, 'ToAddress1', $address2);
131+
$this->setField(19, 'ToAddress2', $address);
132+
$this->setField(20, 'ToCity', $city);
133+
$this->setField(21, 'ToState', $state);
134+
$this->setField(22, 'ToZip5', $zip);
135+
$this->setField(23, 'ToZip4', $zip4);
136+
$this->setField(24, 'ToPhone', $phone);
137+
138+
return $this;
139+
}
140+
141+
/**
142+
* Set package weight in ounces
143+
*
144+
*/
145+
public function setWeight($weight) {
146+
$this->setField(25, 'WeightInOunces', $weight);
147+
return $this;
148+
}
149+
150+
/**
151+
* Set any other requried string make sure you set the correct position as well
152+
* as the position of the items matters
153+
* @param int $position
154+
* @param string $key
155+
* @param string $value
156+
* @return object
157+
*/
158+
public function setField($position, $key, $value) {
159+
$this->fields[$position . ':' . $key] = $value;
160+
return $this;
161+
}
162+
163+
/**
164+
* Add missing required elements
165+
* @return void
166+
*/
167+
protected function addMissingRequired() {
168+
$required = array(
169+
'1:Option' => '',
170+
'1.1:Revision' => '2',
171+
'2:EMCAAccount' => '',
172+
'3:EMCAPassword' => '',
173+
'4:ImageParameters' => '',
174+
'26:FlatRate' => '',
175+
'27:SundayHolidayDelivery' => '',
176+
'28:StandardizeAddress' => '',
177+
'29:WaiverOfSignature' => '',
178+
'30:NoHoliday' => '',
179+
'31:NoWeekend' => '',
180+
'32:SeparateReceiptPage' => '',
181+
'33:POZipCode' => '',
182+
'34:FacilityType' => 'DDU',
183+
'35:ImageType' => 'PDF',
184+
'36:LabelDate' => '',
185+
'37:CustomerRefNo' => '',
186+
'38:SenderName' => '',
187+
'39:SenderEMail' => '',
188+
'40:RecipientName' => '',
189+
'41:RecipientEMail' => '',
190+
'42:HoldForManifest' => '',
191+
'43:CommercialPrice' => 'false',
192+
'44:InsuredAmount' => '',
193+
'45:Container' => '',
194+
'46:Size' => 'REGULAR',
195+
'47:Width' => '',
196+
'48:Length' => '',
197+
'49:Height' => '',
198+
'50:Girth' => '',
199+
);
200+
foreach($required as $item => $value) {
201+
$explode = explode(':', $item);
202+
203+
if(!isset($this->fields[$explode[1]])) {
204+
$this->setField($explode[0], $explode[1], $value);
205+
}
206+
}
207+
}
208+
209+
}

demos/priority_label.php

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<?php
2+
3+
// Load the class
4+
require_once('../USPSPriorityLabel.php');
5+
// Initiate and set the username provided from usps
6+
$label = new USPSPriorityLabel('457LANDM2215');
7+
8+
// During test mode this seems not to always work as expected
9+
$label->setTestMode(true);
10+
11+
$label->setFromAddress('Erik', 'Richard', '', '5161 Lankershim Blvd', 'North Hollywood', 'CA', '91601', '# 204', '', '8882721214');
12+
$label->setToAddress('Vincent', 'Gabriel', '', '5440 Tujunga Ave', 'North Hollywood', 'CA', '91601', '707');
13+
$label->setWeight(1);
14+
15+
// Perform the request and return result
16+
$label->createLabel();
17+
18+
//print_r($label->getArrayResponse());
19+
//print_r($label->getPostData());
20+
//var_dump($label->isError());
21+
22+
// See if it was successful
23+
if($label->isSuccess()) {
24+
//echo 'Done';
25+
//echo "\n Confirmation:" . $label->getConfirmationNumber();
26+
27+
$label = $label->getLabelContents();
28+
if($label) {
29+
$contents = base64_decode($label);
30+
header('Content-type: application/pdf');
31+
header('Content-Disposition: inline; filename="label.pdf"');
32+
header('Content-Transfer-Encoding: binary');
33+
header('Content-Length: ' . strlen($contents));
34+
echo $contents;
35+
exit;
36+
}
37+
38+
} else {
39+
echo 'Error: ' . $label->getErrorMessage();
40+
}

0 commit comments

Comments
 (0)