gpt4 book ai didi

perl - USPS HTTP 邮寄请求

转载 作者:可可西里 更新时间:2023-11-01 17:17:56 28 4
gpt4 key购买 nike

这是我在尝试运行以下 Perl 代码时收到的错误消息。任何想法是什么造成的?它似乎不喜欢 API=Verify,但它是我能找到的返回 9 位邮政编码的 API 的唯一名称。

80040B1AAPI 授权失败。不是一个有效的 API 名称协议(protocol).USPSCOM::DoAuth

# Perl subroutine for POST Request
#########
sub FindTracking() {
$saddress="60 Passional Way";
$scity="Burnsville";
$szip="27690";
$sstate="NC";
print "start of tracking...<br>";

$queryString = qq~
<?xml version="1.0"?>
https://secure.shippingapis.com/ShippingAPI.dll?API=Verify&XML=
<AddressValidateRequest USERID="xxxxxxx">
<Revision>1</Revision>
<Address ID="0">
<Address1></Address1>
<Address2>$saddress</Address2>
<City>$scity</City>
<State>$sstate</State>
<Zip5>$szip</Zip5>
<Zip4></Zip4>
</Address>
</AddressValidateRequest>
<?xml version="1.0"?>
<AddressValidateResponse><Address ID="0">
<Address2></Address2><City></City><State></State><Zip5></Zip5>
<Zip4></Zip4></Address></AddressValidateResponse>
<Error>
<Number></Number>
<Source></Source>
<Description></Description>
<HelpFile></HelpFile>
<HelpContext></HelpContext>
</Error>
~;

# Instantiate the user agent and set our agent string
$userAgent = new LWP::UserAgent;
$userAgent->agent( 'USPS' );
$queryString =~ s/ /\%20/ig;
$request = new HTTP::Request( 'POST',
'https://secure.shippingapis.com/ShippingAPI.dll' );

# Set the content type
$request->content_type( 'text/xml' );

# Set the query string
$request->content( $queryString );

# Make the request
$response = $userAgent->request( $request );
print $response->content();
# Check the status of the request
if ( $response->is_success ) {
$content = $$response{ "_content" };
$TrackingNumber = "";
($success) = $content =~ /\<AddressValidateRequest\>(.*)\
<\/AddressValidateRequest\>/;
print "<br><br>==>some $content<br>";
if ($success eq "Success") {
($TrackingNumber)= $content =~ /\<Zip4\>(.*)\<\/Zip4\>/;
print "here with $TrackingNumber<br>";
}

}
else {
print "<br><br>here with resp=$response<br>req=$request <br>cont
$content";
}
}

最佳答案

首先,您有一个无效的查询字符串。将您的操作与 USPS WebTools example 进行比较并删除他们不显示的内容。确保您正确设置了您的 USERID(而不是使用“xxxxxxx”);环境变量在那里很方便。

或者,您可以尝试使用 Business::USPS::WebTools来自 GitHub 的模块。它实现了邮政编码查找。

关于perl - USPS HTTP 邮寄请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53463740/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com