无法使用PHP / Curl从Deutsche Bahn API获得响应

时间:2019-01-22 14:15:14

标签: php api curl

我正在使用Deutsche Bahn Api获取欧洲的火车记录,但API返回错误:

  

文档已移动https://www.bahn.de/p/view/home/kontakt/error500.shtml。        我不懂德语,所以我感到困惑和困惑。        这是我尝试的代码。你能说明我做错了吗?

<?php
 $t=time();
 $sig = md5('d06c00525957425b5719bba2c7a2c5a6'.$t);
 $xml = simplexml_load_string('<?xml version="1.0" encoding="UTF-8"?>
 <AngebotssucheRequest xmlns="http://www.bahn.de/webservices/dritte/angebotssuche/request" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  xmlns:db="http://www.bahn.de/webservices/dritte/datatypes/db">
 <Header us="prabal" l="EN"  t='.$t.' sig='.$sig.'/>
 <RequestData d="8079732" s="8079706">
 <DateTimeH d="822312689">
 </DateTimeH>
 </RequestData>
 </AngebotssucheRequest>');


 $header = array(
     "Content-type: text/xml;charset=\"utf-8\"",
     "Accept: text/xml",
     "Cache-Control: no-cache",
     "Pragma: no-cache",
     "SOAPAction: \"run\"",
     "Content-length: ".strlen($xml),
 );

 $soap_do = curl_init();
   curl_setopt($soap_do, CURLOPT_URL, "https://fahrkartendritte.bahn.de/webservices/fahrkarten/v1" );
   curl_setopt($soap_do, CURLOPT_RETURNTRANSFER, true );
   curl_setopt($soap_do, CURLOPT_POST,           true );
   curl_setopt($soap_do, CURLOPT_POSTFIELDS,     $xml);
   curl_setopt($soap_do, CURLOPT_HTTPHEADER,     $header);

 if(curl_exec($soap_do) === false) {
  $err = 'Curl error: ' . curl_error($soap_do);
  curl_close($soap_do);
  print $err;
   } else {
     $response = curl_exec($soap_do); 

    curl_close($soap_do);
    $response1 = str_replace("<soap:Body>","",$response);
        $response2 = str_replace("</soap:Body>","",$response1);
        print_r($response);
       print 'Operation completed without any errors';
   }
   ?>

0 个答案:

没有答案