我最近找到了一份新工作。他们需要我使用PHP通过API从ShipStation获取信息。我对PHP相当陌生,甚至对ShipStation还是比较新。我从API文档复制了代码,并尝试添加代码以进行授权。这就是我所拥有的:
<?php
$apiKey = "my_api_key";
$apiSecret = "my_api_secret";
$auth = base64_encode($apiKey . ":" . $apiSecret);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://ssapi.shipstation.com/orders");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_HEADER, FALSE);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
"Authorization: Basic " . $auth
));
$response = curl_exec($ch);
curl_close($ch);
var_dump($response);
?>
它没有给我订单信息,而是返回bool(false)
。
我想我只是没看错我在做什么。任何帮助将不胜感激。谢谢!
答案 0 :(得分:0)
这是在查询“ Shipstation API PHP”查询中出现的,因此我想指向此答案How do I make a request using HTTP basic authentication with PHP curl?,并重申它是基本的身份验证,因此您只需要这样做
curl_setopt($ch, CURLOPT_USERPWD, $apiKey . ":" . $apiSecret);
我还建议您通过安全连接发送它,以免中间人阅读您的API凭据。
答案 1 :(得分:0)
我以这种方式与 CURL 连接:
$url = 'https://ssapi.shipstation.com/orders';
$ShpStnAuth = 'Authorization: basic '.base64_encode('key:pair');
$curl = curl_init();
curl_setopt_array(
$curl, array(
CURLOPT_URL => $url,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => '',
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 0,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => 'GET',
CURLOPT_HTTPHEADER => array(
'Host: ssapi.shipstation.com',
$this->ShpStnAuth,
),
)
);
$response = curl_exec($curl);
curl_close($curl);
print_r($response);