Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Arbitrary 'Unable to parse response code from HTTP response due to malformed response' Exception is raised #76

Open
JwJason opened this issue May 21, 2013 · 3 comments

Comments

@JwJason
Copy link

JwJason commented May 21, 2013

Demo code:

<?php

include('include/httpful.phar');

function send($uri, $body) {
    $response = \Httpful\Request::post($uri)
                ->sendsXml()
                ->body($body)   
                ->expectsXml()
                ->send();
    return $response;
}

$uri = "http://spatial.virtualearth.net/REST/v1/Dataflows/Geocode/?input=xml&output=xml&key=";
$key = "Aor625SZAtBjek2AZV08BvD0x-vZ0CgjNYWkbIyE1uGmylsFRNzcPprIkFaFOZJI";

$xmlBody1 = "<GeocodeFeed >
<GeocodeEntity Id='1' xmlns='http://schemas.microsoft.com/search/local/2010/5/geocode'>
    <GeocodeRequest Culture='en-us' Query='1501 NE Davis St, Portland, OR'>
    </GeocodeRequest>
</GeocodeEntity>

<GeocodeEntity Id='2' xmlns='http://schemas.microsoft.com/search/local/2010/5/geocode'>
    <GeocodeRequest Culture='en-us' Query='350 W Burnside St, Portland, OR'>
    </GeocodeRequest>
</GeocodeEntity>

<GeocodeEntity Id='3' xmlns='http://schemas.microsoft.com/search/local/2010/5/geocode'>
    <GeocodeRequest Culture='en-us' Query='1005 W Burnside St, Portland, OR'>
    </GeocodeRequest>
</GeocodeEntity>

<GeocodeEntity Id='4' xmlns='http://schemas.microsoft.com/search/local/2010/5/geocode'>
    <GeocodeRequest Culture='en-us' Query='911 NW 14th Ave, Portland, OR'>
    </GeocodeRequest>
</GeocodeEntity>
</GeocodeFeed>";

$xmlBody2 = "<GeocodeFeed >
<GeocodeEntity Id='1' xmlns='http://schemas.microsoft.com/search/local/2010/5/geocode'>
    <GeocodeRequest Culture='en-us' Query='1501 NE Davis St, Portland, OR'>
    </GeocodeRequest>
</GeocodeEntity>

<GeocodeEntity Id='2' xmlns='http://schemas.microsoft.com/search/local/2010/5/geocode'>
    <GeocodeRequest Culture='en-us' Query='350 W Burnside St, Portland, OR'>
    </GeocodeRequest>
</GeocodeEntity>

<GeocodeEntity Id='3' xmlns='http://schemas.microsoft.com/search/local/2010/5/geocode'>
    <GeocodeRequest Culture='en-us' Query='1005 W Burnside St, Portland, OR'>
    </GeocodeRequest>
</GeocodeEntity>

<GeocodeEntity Id='4' xmlns='http://schemas.microsoft.com/search/local/2010/5/geocode'>
    <GeocodeRequest Culture='en-us' Query='911 NW 14th Ave, Portland, OR'>
    </GeocodeRequest>
</GeocodeEntity>

<GeocodeEntity Id='5' xmlns='http://schemas.microsoft.com/search/local/2010/5/geocode'>
    <GeocodeRequest Culture='en-us' Query='12000 SW 49th Ave  Portland, OR'>
    </GeocodeRequest>
</GeocodeEntity>
</GeocodeFeed>";

echo '<h1>Request #1:</h1>';
$r = send($uri . $key, $xmlBody1);
print_r($r);

echo '<h1>Request #2:</h1>';
$r = send($uri . $key, $xmlBody2);
print_r($r);

?>

Demo output:

Request #1:

Httpful\Response Object ( [body] => SimpleXMLElement Object ( [Copyright] => Copyright © 2013 Microsoft and its suppliers. All rights reserved. This API cannot be accessed and the content and any results may not be used, reproduced or transmitted in any manner without express written permission from Microsoft Corporation. [BrandLogoUri] => http://spatial.virtualearth.net/Branding/logo_powered_by.png [StatusCode] => 201 [StatusDescription] => Created [AuthenticationResultCode] => ValidCredentials [TraceId] => 7b3cbdd74b644ec39a655a28f883c8b3|BAYM001576|02.00.117.2900| [ResourceSets] => SimpleXMLElement Object ( [ResourceSet] => SimpleXMLElement Object ( [EstimatedTotal] => 1 [Resources] => SimpleXMLElement Object ( [DataflowJob] => SimpleXMLElement Object ( [Id] => edf0d066d6604a898ae9e10630eb6fdf [Link] => https://spatial.virtualearth.net/REST/v1/dataflows/Geocode/edf0d066d6604a898ae9e10630eb6fdf [Description] => Geocode [Status] => Pending [CreatedDate] => 2013-05-21T04:10:25.0189272-07:00 [CompletedDate] => SimpleXMLElement Object ( ) [TotalEntityCount] => 4 [ProcessedEntityCount] => 0 [FailedEntityCount] => 0 ) ) ) ) ) [raw_body] => Copyright © 2013 Microsoft and its suppliers. All rights reserved. This API cannot be accessed and the content and any results may not be used, reproduced or transmitted in any manner without express written permission from Microsoft Corporation.http://spatial.virtualearth.net/Branding/logo_powered_by.png201CreatedValidCredentials7b3cbdd74b644ec39a655a28f883c8b3|BAYM001576|02.00.117.2900|1edf0d066d6604a898ae9e10630eb6fdfhttps://spatial.virtualearth.net/REST/v1/dataflows/Geocode/edf0d066d6604a898ae9e10630eb6fdfGeocodePending2013-05-21T04:10:25.0189272-07:00400 [headers] => Httpful\Response\Headers Object ( [headers:Httpful\Response\Headers:private] => Array ( [cache-control] => no-cache [transfer-encoding] => chunked [content-type] => application/xml; charset=utf-8 [location] => https://spatial.virtualearth.net/REST/v1/dataflows/Geocode/edf0d066d6604a898ae9e10630eb6fdf [server] => Microsoft-IIS/8.0 [x-ms-bm-ws-sn] => BAYM001576 [x-ms-bm-ws-info] => 0 [x-sds-elapsed] => 989 [x-aspnet-version] => 4.0.30319 [x-powered-by] => ASP.NET [date] => Tue, 21 May 2013 11:10:24 GMT ) ) [raw_headers] => HTTP/1.1 201 Created Cache-Control: no-cache Transfer-Encoding: chunked Content-Type: application/xml; charset=utf-8 Location: https://spatial.virtualearth.net/REST/v1/dataflows/Geocode/edf0d066d6604a898ae9e10630eb6fdf Server: Microsoft-IIS/8.0 X-MS-BM-WS-SN: BAYM001576 X-MS-BM-WS-INFO: 0 X-SDS-Elapsed: 989 X-AspNet-Version: 4.0.30319 X-Powered-By: ASP.NET Date: Tue, 21 May 2013 11:10:24 GMT [request] => Httpful\Request Object ( [uri] => http://spatial.virtualearth.net/REST/v1/Dataflows/Geocode/?input=xml&output=xml&key=Aor625SZAtBjek2AZV08BvD0x-vZ0CgjNYWkbIyE1uGmylsFRNzcPprIkFaFOZJI [method] => POST [headers] => Array ( ) [raw_headers] => POST /REST/v1/Dataflows/Geocode/?input=xml&output=xml&key=Aor625SZAtBjek2AZV08BvD0x-vZ0CgjNYWkbIyE1uGmylsFRNzcPprIkFaFOZJI HTTP/1.1 Host: spatial.virtualearth.net User-Agent: Httpful/0.1.7 (cURL/7.24.0 PHP/5.4.7 (WINNT) Apache/2.4.3 (Win32) OpenSSL/1.0.1c .4.7 Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.31 (KHTML, like Gecko) Chrome/26.0.1410.64 Safari/537.31) Content-Type: application/xml Accept: /; q=0.5, text/plain; q=0.8, text/html;level=3;q=0.9, application/xml [strict_ssl] => [content_type] => application/xml [expected_type] => application/xml [additional_curl_opts] => Array ( ) [auto_parse] => 1 [serialize_payload_method] => 2 [username] => [password] => [serialized_payload] => [payload] => [parse_callback] => [error_callback] => [follow_redirects] => [max_redirects] => 25 [payload_serializers] => Array ( ) [_ch] => Resource id #23 [_debug] => ) [code] => 201 [content_type] => application/xml [parent_type] => application/xml [charset] => utf-8 [is_mime_vendor_specific] => [is_mime_personal] => [parsers:Httpful\Response:private] => )

Request #2:

Fatal error: Uncaught exception 'Exception' with message 'Unable to parse response code from HTTP response due to malformed response' in phar://C:/xampp/www/geocode-2/include/httpful.phar/Httpful/Response.php:138 Stack trace: #0 phar://C:/xampp/www/geocode-2/include/httpful.phar/Httpful/Response.php(37): Httpful\Response->parseCode('HTTP/1.1 100 Co...') #1 phar://C:/xampp/www/geocode-2/include/httpful.phar/Httpful/Request.php(189): Httpful\Response->_construct('HTTP/1.1 201 Cr...', 'HTTP/1.1 100 Co...', Object(Httpful\Request)) #2 C:\xampp\www\geocode-2\test.php(10): Httpful\Request->send() #3 C:\xampp\www\geocode-2\test.php(50): send('http://spatial....', '?...') #4 {main} thrown in phar://C:/xampp/www/geocode-2/include/httpful.phar/Httpful/Response.php on line 138

The first request goes through fine, but when a little more xml data is added to the POST body, suddenly the response becomes "malformed".

@nategood
Copy link
Owner

Httpful uses simple xml for parsing. Can you test your XML directly via simple to verify it isn't a bug there?

@kalpik
Copy link

kalpik commented Dec 20, 2013

I'm getting this error when using PUT. Trying to send an XML file as body.

@bholaday
Copy link

I am having this same error. Im parsing JSON data with php. I get the same error after looping through the 88th iteration in my code. I checked the output for JSON errors and didn't find any...

Fatal error: Uncaught exception 'Exception' with message 'Unable to parse response code from HTTP response due to malformed response' in
phar:///mypath/httpful.phar/Httpful/Response.php:138 Stack trace: #0
phar:///mypath/httpful.phar/Httpful/Response.php(37): Httpful\Response->_parseCode(NULL) #1
phar:///mypath/httpful.phar/Httpful/Request.php(214): Httpful\Response->__construct('<html>??<head><...', NULL, Object(Httpful\Request)) #2
/mypath/checkDuplicates.php(77): Httpful\Request->send() #3 {main} thrown in
phar:///mypath/httpful.phar/Httpful/Response.php on line 138

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants