简体   繁体   English

在 PHP 中使用 XML POST 调用 API

[英]POST Call to an API with an XML in PHP

I'm trying to make a call to an API supposed to return me a XML to create a Label for an order.我正在尝试调用一个 API,该 API 应该返回一个 XML 来为订单创建一个标签。

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.当我在 Rest Console 中尝试参数时它工作正常,但我对 PHP 相当陌生,所以我在实现它时遇到了麻烦。

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 :编辑:我再次使用 file_get_contents :

$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 : 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 ..这个 API 调用应该给我一个 PDF ..

您没有打印任何内容,因此它可能会返回 xml,而您只是看不到结果.. 尝试 var_dump( createLabel());

The line with headers are the RESPONSE headers to your own request:带有标题的行是您自己的请求的 RESPONSE 标题:

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 请求中:

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.我建议第一次存储结果。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM