paul9519 Posted April 8, 2012 Share Posted April 8, 2012 i am currently building a small client to take xml from a post http request and convert it to json i have run into a problem here is the code , it receives everything properly and it can print the xml; but i cant convert it to json or access any elements of the xml, its being really weird <?php $c = curl_init(); curl_setopt($c, CURLOPT_URL, 'https://api.octranspo1.com/v1.1/GetNextTripsForStop'); curl_setopt($c, CURLOPT_POST, true); curl_setopt($c, CURLOPT_POSTFIELDS, 'appID=00000&apiKey=000000000&stopNo=4316&routeNo=98'); curl_setopt($c, CURLOPT_RETURNTRANSFER, 1); curl_setopt($c, CURLOPT_FOLLOWLOCATION, true ); $response = curl_exec($c); $xml = simplexml_load_string($response) $json = json_encode($xml); echo $xml->asXML(); // this prints the whole xml echo $json; // this just prints {} curl_close( $c ); ?> and here is what it is returning <?xml version="1.0" encoding="utf-8"?> <soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"> <soap:Body> <GetNextTripsForStopResponse xmlns="http://octranspo.com"> <GetNextTripsForStopResult> <StopNo xmlns="http://tempuri.org/">4316</StopNo> <StopLabel xmlns="http://tempuri.org/">BLOHM 1579</StopLabel> <Error xmlns="http://tempuri.org/" /> <Route xmlns="http://tempuri.org/"> <RouteDirection> <RouteNo>98</RouteNo> <RouteLabel>Tunney's Pasture</RouteLabel> <Direction>Northbound</Direction> <Error /> <RequestProcessingTime>20120406171014</RequestProcessingTime> <Trips> <Trip> <TripDestination>Tunney's Pasture</TripDestination> <TripStartTime>17:24</TripStartTime> <AdjustedScheduleTime>18</AdjustedScheduleTime> <AdjustmentAge>4.18</AdjustmentAge> <LastTripOfSchedule>false</LastTripOfSchedule> <BusType>6E - 60</BusType> <Latitude>45.374141</Latitude> <Longitude>-75.596715</Longitude> <GPSSpeed>59.9</GPSSpeed> </Trip> <Trip> <TripDestination>Tunney's Pasture</TripDestination> <TripStartTime>17:39</TripStartTime> <AdjustedScheduleTime>33</AdjustedScheduleTime> <AdjustmentAge>-1</AdjustmentAge> <LastTripOfSchedule>false</LastTripOfSchedule> <BusType>6E - 60</BusType> <Latitude /><Longitude /> <GPSSpeed /> </Trip> <Trip> <TripDestination>Tunney's Pasture</TripDestination> <TripStartTime>17:59</TripStartTime> <AdjustedScheduleTime>53</AdjustedScheduleTime> <AdjustmentAge>-1</AdjustmentAge> <LastTripOfSchedule>false</LastTripOfSchedule> <BusType>6E - 60</BusType> <Latitude /><Longitude /> <GPSSpeed /></Trip> </Trips> </RouteDirection> </Route> </GetNextTripsForStopResult> </GetNextTripsForStopResponse> </soap:Body> </soap:Envelope> Thanks for the help Link to comment https://forums.phpfreaks.com/topic/260565-simple-xml-problems/ Share on other sites More sharing options...
dcro2 Posted April 8, 2012 Share Posted April 8, 2012 It seems to be having trouble with the element names with colons, so just $response = curl_exec($c); $response = preg_replace("/(<\/?)(\w+)[^>]*>)/", "$1$2$3", $response); $xml = simplexml_load_string($response); //missed semicolon here Link to comment https://forums.phpfreaks.com/topic/260565-simple-xml-problems/#findComment-1335394 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.