繁体   English   中英

在 PHP 中使用 XML POST 调用 API

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

我正在尝试调用一个 API,该 API 应该返回一个 XML 来为订单创建一个标签。

我有这个文件:

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

当我在 Rest Console 中尝试参数时它工作正常,但我对 PHP 相当陌生,所以我在实现它时遇到了麻烦。

到目前为止我的代码:

    <?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();

?>

有人对此有想法吗?

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

2 var_dump 的结果:

string(319) "" object(SimpleXMLElement)#1 (0) { } 

所以我想我检索了一些信息,但是如何提取它? 这个 API 调用应该给我一个 PDF ..

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

带有标题的行是您自己的请求的 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=');

所以这些标头会随着您的请求返回; 您需要将它们添加到 curl 请求中:

curl_setopt($ch,CURLOPT_HTTPHEADER,array('HeaderName: HeaderValue','HeaderName2: HeaderValue2'));

遇到同样的问题,也许它不再与您相关,但我用以下代码解决了它。

$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);
}

重要的是要知道每个标签只能通过此调用打印一次。 所以第二次结果将是空的。 我建议第一次存储结果。

暂无
暂无

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

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