使用Shippo和PHP生成运输标签

时间:2015-05-24 15:29:05

标签: php shipping fedex shippo

除了生成标签外,我几乎完美地工作了。

我有这个代码来生成费率,效果很好:

//Wait for rates to be generated
$attempts = 0;
while (($shipment["object_status"] == "QUEUED" || $shipment["object_status"] == "WAITING") && $attempts < 10)
{
    $shipment = Shippo_Shipment::retrieve($shipment["object_id"]);
    $attempts +=1;
}

//Get all rates for shipment.
$rates = Shippo_Shipment::get_shipping_rates(array('id'=> $shipment["object_id"]));

$json = json_decode($rates, true);
foreach ($json["results"] as $key)
{
    $amount = $key["amount"];
    $servicelevel = $key["servicelevel_name"];
    $objid = $key["object_id"];
}

当我浏览每个结果时,我将它们分配给不同服务级别的变量,并允许用户选择他们想要使用的运输方式。我将$objid传递到下一页,使用以下代码生成标签:

//Write the object_id to a variable
$var = $shiparray[1];
$transaction = Shippo_Transaction::create(array('rate'=>$var));
echo $transaction["object_status"] ."<br>";
// Wait for carrier to create shipping label
$attempts = 0;
while (($transaction["object_status"] == "QUEUED" || $transaction["object_status"] == "WAITING") && $attempts < 10)
{
    $transaction = Shippo_Transaction::retrieve($transaction["object_id"]);
    $attempts += 1;
}
echo $transaction["object_status"] ."<br>";
// Retrieve label url and tracking number or error message
if ($transaction["object_status"] == "SUCCESS")
{
    echo($transaction["label_url"]);
    echo("\n");
    echo($transaction["tracking_number"]);
}
else
{
    echo( $transaction["messages"] );
}

但这只会产生错误。我传递错误的值来制作标签吗?我应该使用为装运而不是费率生成的值吗?

1 个答案:

答案 0 :(得分:0)

这是来自Shippo的Simon。您发布的链接实际上是费率响应,而不是交易(它是许多费率的数组,因此是长度)。

我已经快速检查了您的帐户,并且对于您最近的交易尝试,出现了错误消息&#34;由于Shippo帐户没有,因此无法购买费率有效的结算设置。&#34;。这是因为您的Shippo用户帐户没有任何信用卡信息,但您正在尝试在生产中购买标签。

您可以在此处输入有效的信用卡https://goshippo.com/user/billing/。一旦您的信用卡被保存,请求应该正常工作!

如果您有任何其他问题,请告诉我,随时乐意为您提供帮助!