I'm trying to make a call to an API supposed to return me a XML to create a Label for an order.
I've got this documentation :
Type of request:
GET
URL PROD:
https://api.bpost.be/services/shm/{accountID}/orders/{OrderReference}/labels/{size}
Headers for PDF labels:
Authorization: Basic AccountID:pass-phrase (base64)
Content-Type: application/vnd.bpost.shm-labelRequest-v3+XML
Accept: application/vnd.bpost.shm-label-pdf-v3+XML
It's working fine when I'm trying the parameters in Rest Console, but I'm fairly new with PHP so I have trouble implementing it.
My code so far :
<?php
header('Content-Type: application/vnd.bpost.shm-labelRequest-v3+XML');
header('Accept: application/vnd.bpost.shm-label-pdf-v3+XML');
header('Authorization: Basic MTE4MDI1OjEyMzQ=');
function createLabel(){
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, 'https://api.bpost.be/services/shm/ID/orders/1634/labels/A6');
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
$resp = curl_exec($curl);
curl_close($curl);
return $resp;
}
createLabel();
?>
Anybody got an idea about this ?
EDIT : I've been going with file_get_contents again :
$context = stream_context_create(array(
'http' => array(
'header' => ["Authorization: Basic " . base64_encode("$username:$password"),
"Accept: application/vnd.bpost.shm-label-pdf-v3+XML",
"Content-Type: application/vnd.bpost.shm-labelRequest-v3+XML"]
)
));
$res = file_get_contents('https://api.bpost.be/services/shm/118025/orders/1638/labels/A6', false, $context);
$xml = new SimpleXMLElement($res);
var_dump($res);
var_dump($xml);
Result of the 2 var_dump :
string(319) "" object(SimpleXMLElement)#1 (0) { }
So i guess I retrieve some informations, but how can I extract it ? This API call is supposed to give me a PDF ..
您没有打印任何内容,因此它可能会返回 xml,而您只是看不到结果.. 尝试 var_dump( createLabel());
The line with headers are the RESPONSE headers to your own request:
header('Content-Type: application/vnd.bpost.shm-labelRequest-v3+XML');
header('Accept: application/vnd.bpost.shm-label-pdf-v3+XML');
header('Authorization: Basic MTE4MDI1OjEyMzQ=');
So those headers are coming back with your request; you need to add them to the curl request:
curl_setopt($ch,CURLOPT_HTTPHEADER,array('HeaderName: HeaderValue','HeaderName2: HeaderValue2'));
Encountered the same problem, maybe it's no longer relevant to you but I solved it with the following code.
$username = 'accountid';
$password = 'passphrase';
$context = stream_context_create(array(
'http' => array(
'header' => ["Authorization: Basic " . base64_encode("$username:$password"),
"Content-Type: application/vnd.bpost.shm-labelRequest-v3+XML",
"Accept: application/vnd.bpost.shm-label-pdf-v3+XML"]
)
));
$res = file_get_contents('https://api.bpost.be/services/shm/'.$username.'/orders/'.$order.'/labels/A6', false, $context);
$xml = simplexml_load_string($res);
foreach($xml->label as $item) {
header('Content-type: application/pdf');
header('Content-Disposition: attachment; filename="bpost_'.$order.'.pdf"');
echo base64_decode((string)$item->bytes);
}
It's important to know that each label only can be printed once by this call. So the second time the results will be empty. I suggest storing the results the first time.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.