1

I am working on installing the aws sdk for PHP on my Windows machine (Win7 64 bit) PHP v 5.5.12. I tried using all the 3 methods viz Composer,zip and PHAR mentioned here.All the 3 ways give me the same error as below.

Warning: SimpleXMLElement::__construct(): Entity: line 1: parser error : Space required after the Public Identifier in C:\wamp\www\aws-sdk-php-master\src\Aws\Common\Exception\Parser\DefaultXmlExceptionParser.php on line 41

Here's the function which givess the error

public function parse(RequestInterface $request, Response $response)
{
    $data = array(
        'code'       => null,
        'message'    => null,
        'type'       => $response->isClientError() ? 'client' : 'server',
        'request_id' => null,
        'parsed'     => null
    );

    if ($body = $response->getBody(true)) {
        $this->parseBody(new \SimpleXMLElement($body), $data); //THIS LINE GIVES ERROR
    } else {
        $this->parseHeaders($request, $response, $data);
    }

    return $data;
}

Here is how I try to use it.

 error_reporting(E_ALL);
 define('AWS_KEY', 'MyAWSKEY');
 define('AWS_SECRET_KEY', 'MYSECRETKEY');
 define('HOST', 'http://localhost');   //tried changing host to diff values,but don't   
                                       //think thats the issue
// require the AWS SDK for PHP library
 require 'aws-autoloader.php';
 //require '../../aws.phar';

 use Aws\S3\S3Client;

 // Establish connection with an S3 client.
 $client = S3Client::factory(array(
'base_url' => HOST,
'key'      => AWS_KEY,
'secret'   => AWS_SECRET_KEY
));

$o_iter = $client->getIterator('ListObjects', array(
'Bucket' => 'mybucketname'
));

foreach ($o_iter as $o) {
   echo "{$o['Key']}\t{$o['Size']}\t{$o['LastModified']}\n";
} 

Heres the stacktrace

 [17-Jul-2014 04:19:01 Europe/Paris] PHP Fatal error:  Uncaught exception 'Exception'  with message 'String could not be parsed as XML' in C:\wamp\www\aws-sdk-php-master\src\Aws\Common\Exception\Parser\DefaultXmlExceptionParser.php:41
Stack trace:
#0 C:\wamp\www\aws-sdk-php- master\src\Aws\Common\Exception\Parser\DefaultXmlExceptionParser.php(41): SimpleXMLElement- >__construct('<!DOCTYPE HTML ...')
#1 C:\wamp\www\aws-sdk-php-master\src\Aws\S3\Exception\Parser\S3ExceptionParser.php(33): Aws\Common\Exception\Parser\DefaultXmlExceptionParser->parse(Object(Guzzle\Http\Message\Request), Object(Guzzle\Http\Message\Response))
#2 C:\wamp\www\aws-sdk-php-master\src\Aws\Common\Client\ExpiredCredentialsChecker.php(61): Aws\S3\Exception\Parser\S3ExceptionParser->parse(Object(Guzzle\Http\Message\Request), Object(Guzzle\Http\Message\Response))
#3 C:\wamp\www\aws-sdk-php-master\vendor\guzzle\guzzle\src\Guzzle\Plugin\Backoff\AbstractBackoffStrategy.php(39): Aws\Common\Client\ExpiredCredentialsChecker->getDelay(0, Object(Guzzle\Http\Message\Request), Object(Guzzle\Http\Message\Resp in C:\wamp\www\aws-sdk-php-master\src\Aws\Common\Exception\Parser\DefaultXmlExceptionParser.php on line 41

Tried googling it and learnt might be due to magic_quotes in php.ini to be on,but thats not the case,actually I don't have magic_quotes itself in my ini file. Checked the stack trace and it shows the same error.I am not able to figure out if its a system issue or some configuration problem as I get it for all the methods. Is this a issue with some configuration? OR I am missing something?

KillABug
  • 1,414
  • 6
  • 34
  • 69
  • 1
    Just FYI: magic quotes has been removed in PHP 5.4 (yay!). What you should do in trouble-shooting this issue is to actually dump `$body` so that you can take a look what is tried to be parsed as XML (and which isn't XML). That should give much more insight. If you're still unsure after dump, add the first lines of `$body` to your question. (You're perhaps parsing a HTML page with an error message instead of XML, compare http://stackoverflow.com/q/14465945/367456 which also shows that if you increase error logging you can find out more w/o dumping `$body` as PHP does it in the error messages). – hakre Jul 16 '14 at 18:39
  • See as well: [How to get useful error messages in PHP?](http://stackoverflow.com/q/845021/367456) – hakre Jul 16 '14 at 18:42
  • What service and operation are you using? Can you share the code sample where you actually execute an operation? – Jeremy Lindblom Jul 17 '14 at 01:34
  • @JeremyLindblom Hey I have added the code to the question.I am trying to use S3 client using the factory load method.My autoloader is the default one as I used composer here.I dont think I need to change it. – KillABug Jul 17 '14 at 02:05

1 Answers1

1

Oh, I see what's going on. You should not be setting the base_url parameter. By setting that value you are telling the SDK to send the requests to your localhost, instead of the actual S3 service. Your localhost is returning HTML, not a XML, which is why SimpleXML is throwing parse errors.

To use S3, you should provide your credentials (key and secret), and optionally a region if you are intentionally not using S3's default region. Do not override the base_url value, which is something that the SDK resolves internally based on its knowledge of the service endpoints.

Note: we also encourage users to use a credential profile profile instead of explicit credentials (key and secret) so you can store your credentials in a file outside of your code and project, in order to prevent accidental credential leaks (e.g., committing credentials into version control). The S3 page in the User Guide walks you through how to properly create the client and provides a link to learn more about the credential file/profile.

Jeremy Lindblom
  • 6,437
  • 27
  • 30
  • ohhh!!!Why did not I try it!! :P Poor on my part.Thank you for the prompt help!!! But I would suggest a comment beside the `HOST` parameter saying not mandatory or can be left blank would be helpful for beginners.You being the author of the SDK can probably do it. :P – KillABug Jul 17 '14 at 04:43
  • Yes I have gone through the profile in the docs and will implement it for sure. – KillABug Jul 17 '14 at 04:45
  • Regarding "I would suggest a comment beside the HOST parameter saying not mandatory or can be left blank", what page/section would be the best place for that message? – Jeremy Lindblom Jul 17 '14 at 16:50
  • `define('HOST', ''); //Can be left blank` and a link probably to the docs suggesting the scenarios in which it must be changed.(Like in case of overriding the default region).Also,I changed it majorly because it said `base_url` which in general relates to the environment path(I use the same variable in my project).Mentioning it as not the environment base url but aws specific might also be easy to understand.But yes again its a suggestion,not sure if its the best possible way! :P – KillABug Jul 17 '14 at 17:25
  • @KillABug Where did you find that? Do you have a link? This doesn't sound familiar, and that concerns me because I wrote a lot of the documentation. – Jeremy Lindblom Jul 18 '14 at 00:31
  • You are right,its **not from the docs**.The docs have it the right way `//Instantiate the S3 client with your AWS credentials $s3Client = S3Client::factory(array( 'key' => 'YOUR_AWS_ACCESS_KEY_ID', 'secret' => 'YOUR_AWS_SECRET_ACCESS_KEY', ));` I got it from an example while browsing online.[Here](http://docs.dreamobjects.net/s3-examples/php2.html#list-a-bucket-s-content) is the link.Probably we should ask them to add some comments over there. – KillABug Jul 18 '14 at 05:27
  • 1
    Yeah, that link is for a different company's service that happens to be compatible with the S3 API. They modify the host, because they want the SDK requests to send requests to their service. In their use case, what they have documented is correct. – Jeremy Lindblom Jul 18 '14 at 15:22