DHL Tracking Api and PHP

前端 未结 4 1557
北恋
北恋 2021-02-09 06:03

I\'m currently working on a project, where i have to get the status of a packet (sent with DHL). I read about the DHL API, which return an XML, but somehow there are no good exa

4条回答
  •  感情败类
    2021-02-09 06:22

    Quick and dirty without any third party lib and using official API:

    ' );
    $shipmentids = array(
        '00340434161094015902' // in sandbox only special numbers are allowed
    );
    
    
    $opts = array(
        'http' => array(
            'method' => "GET",
            'header' => "Authorization: Basic " . base64_encode( "$username:$password" )
        )
    );
    
    $context = stream_context_create( $opts );
    
    
    foreach ( $shipmentids as $shipmentid ) {
        $payload->attributes()->{'piece-code'} = $shipmentid;
        $response                              = file_get_contents( $endpoint . '?' . http_build_query( array( 'xml' => $payload->saveXML() ) ), false, $context );
        $responseXml                           = simplexml_load_string( $response );
        $status                                = null;
    
        // get last state
        foreach ( $responseXml->data->data->data as $event ) {
            $status = $event->attributes()->{'event-short-status'};
        }
    
        echo "Shipment " . $shipmentid . " is in state: " . $status . "\n";
    }
    

提交回复
热议问题