首页 > 解决方案 > 未使用 PHP/Curl 从 Deutsche Bahn API 获得响应

问题描述

我正在使用 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';
   }
   ?>

标签: phpapicurl

解决方案


推荐阅读