I am trying to get the contents from a gzipped file that is returned to me after using Mailchimp API doing a batch operation request. I expect to get only a JSON string as response, but also receive a bunch of numbers and random (?) strings.
This is what I do.
$gz = gzopen($response->response_body_url, "r");
$contents = trim(gzread($gz, 10000));
print_r($contents); //see output below
gzclose($gz);
This is what is returned to me.
0000777000000000000000000000000012705141572007721 5ustar
rootroot./05fa27ceab.json0000666000000000000000000000121212705141572012327
0ustar
rootroot[{"status_code":400,"operation_id":null,"response":"{\"type\":\"http://developer.mailchimp.com/documentation/mailchimp/guides/error-glossary/\",\"title\":\"Member
Exists\",\"status\":400,\"detail\":\"xxxx.xxxx#xxxx.xx is
already a list member. Use PUT to insert or update list
members.\",\"instance\":\"\"}"},{"status_code":400,"operation_id":null,"response":"{\"type\":\"http://developer.mailchimp.com/documentation/mailchimp/guides/error-glossary/\",\"title\":\"Member
Exists\",\"status\":400,\"detail\":\"xxxx2.xxxx2#xxxx2.xx is
already a list member. Use PUT to insert or update list
members.\",\"instance\":\"\"}"}]
What am I missing here? Why won't it work?
It looks like you may be dealing with a .tar.gz file instead of just gzip. The easiest way to do that is either with the PharData extension or by just saving it to disk and using a shell tool to unzip.
Here's an answer to a question on how to deal with .tar.gz files in php
Related
I'm currently learning PHP and programming in general. I'm currently writing my first website where I have a simple login system and users can enter some data in plain text. Upon entry, PHP encrypts this data.
I want to add a feature where users can then download an unencrypted copy of the data and I want to do it in such a way that the files on disk remain encrypted the entire time and an unencrypted copy is not stored anywhere.
At the moment I should be able to decrypt the data line by line and store it in an array, but I don't know what I could do once I get to that point. Is there a way for PHP to treat an array as if it was just a text file and then ask the client to download it? Or maybe I could just somehow stream the array to a file on the client, line by line?
I have no code to show at the moment as I'm still trying to work out in my head how it will all be structured.
The solution must also be portable between a windows and Linux server.
Assuming the encrypted file is plain text. Second, writing in pseudo-code, because I do not know your solution
First, you need to decrypt the data in something that can be echoed to the user. Or more generally, something that is represented as a String.
I am doing something simillar with PDF files, so I am showing the similar on TXT file:
$encryptedData = $this->getEncryptedData(); // load what needs to be decrypted. Pseudocode
$filename = 'download.txt'; // file name to download. Will be download.txt
$contents = $this->decrypt($encryptedData) // $contents should be String. Decryption is pseudocode
header("Content-Type: text/plain"); //set header as TXT file
header("Content-Disposition:attachment;filename={$filename}"); //force download prompt from the browser
echo $contents; //print decrypted data to TXT
exit; //stop script after download
Is there a way to check if content received from remote server is a .zip archive? I found many solutions here, but all of them are for local files; therefore, these solutions use file handle resources like fopen, fgets, etc.
Put simply, curl is used to get content from remote server. Depending on validation results on remote server (which I don't have access to), it returns a .zip archive OR error message in plain text. If .zip archive is returned, the content I receive from curl looks like:
PK
A‘OCBÇ—= =
and many more symbols here....
Then I simply use fwrite function to write received content into local file. Naturally, if no archive is returned, content looks like:
some random message here
This is all I have. So, is there any way to check if returned content is a .zip archive? Sure, I could use a solution like:
if (stristr($content, "some random message here")) {//not zip archive}
but this is lame...
After some more research, I found out zip archives should start with "PK", so I wrote such a code (in case someone has a similar problem in future):
if (substr($content, 0, 2) == 'PK' && strlen($content)>100000) {echo "this is zip";}
This code is not the greatest choice (because you need to trust remote server), but it's better than nothing. I also added strlen function to verify that returned content is "big enough" (over 100 KB) to be a zip archive.
If someone has a better solution which works without using file handle resource, I'll be happy to hear it.
If you are on a Linux or *BSD server you can do something like this:
$result = shell_exec('file cccr_logo.zip');
echo 'result:['.$result.']';
Which should spit out something like this:
result:[cccr_logo.zip: Zip archive data, at least v2.0 to extract ]
If you want to test against a standard $result, you can use the -b switch like so:
$result = shell_exec('file -b cccr_logo.zip');
echo 'result:['.$result.']';
And now a valid zip will be (no file name):
result:[Zip archive data, at least v2.0 to extract ]
I'm trying to decode the PDF data returned by the MWS Fulfilment Inbound API - GetPackageLabels request. The documentation is here: http://docs.developer.amazonservices.com/en_UK/fba_inbound/FBAInbound_GetPackageLabels.html
I have successfully made the call and got a response, but a using straight base64_decode in php the resulting pdf is corrupted. As I also can't get the check sum to match it would suggest that I'm missing a step in the decoding process. I've tried downloading from multiple test shipments, so it isn't a case of a single request getting corrupted in transit. I've outlined the steps I'm using below, any help would be appreciated.
XML response:
<GetPackageLabelsResponse xmlns="http://mws.amazonaws.com/FulfillmentInboundShipment/2010-10-01/">
<GetPackageLabelsResult>
<TransportDocument>
<PdfDocument>UEsDBBQACAAIAFlZUUMAAAAAAAAAAAAAAAARAAAAUGFja2FnZUxhYmVscy5wZGbtVmtUE2ca1m7xEkWRcogX8EyruESWzCW3CWI0ECKXQGIChQJBJ8kEx01mMBkEvNS7uOUiiwpYrbWg4lG3iFarsAVF3S1WEEUpmlq3glKgYKtoi2C7k6CGJWf9sftrz9n8mXzv5fue93ue9z2fn0omD4S5fJbfvbarzSwYgABKt5wVHAwqcDKVXgbAMArKCRONW0C5CaNxGa6nDLhEYqUtOGZmZe6NzoNZmyGPMOXxjBJEeHZ9QtboW24D0jm31v9NafwsIfiv4xNM/d8GzBzNV3ntibwsXPFdtPZrDXp0zOCJVEHjIckxn9SUydf2JRLHaPf2us4noz44nF8YzdL2dXxbXVnf1rWtbM11ZdfFmoj5t1tH71oi6u1FPFk4aXgBgPlnB4w4gWvASGwlptFbiDQajNQA9prUEsnLSIFLiSI+/PoSv9hlU9sgj60Pn3utHs+eUthSaOp9A/Uzh4+a99mVtLa35zTNiVlRB/V1Bvxe+GX5Qs/bP+VuH3j46TrfmkXd/sdRt2zkHd9xcu8tRYX9m9gJ7Jay94tn1rfemzTF84C+/O7ZHk/63IbZJ3ovw3Vxmx+pFR/mXIj3HQhb8vFJld9e/p0bujfGs/sfX3xULL30uSYnICzZllsVvm5jYDdPldsaVD6rqBY6NSlIUTyZ3iRT329V1XQV/dy4OsX288KPK4LHxVRL+m4vSpm89Xnb+V0ZnR/9MHZB+tdtNzzKvDOKn0fxlxO8313I+6Jxsc2j9uzF5Lr4RGxTATHmUM+XDT7srvbzR9TbpKsObGksvRZ65rRyILqRp0yx9SmrO5rO+uz1bSlfWpeSvzEwz3vqXyI8J05XG8ryjOwjJcC2BxO+z272v6pqua5IKvwlZ/YH3u7v5D8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pXv3FeFWXCSZsixsweGMrsxSysgcCwdJ6gYov9FGi/kYv9nZAh1RCYYYQAdEsB/Kh1QjgDiVxqKxg0E9lI6ArGAi4gAlA9zhajWWYcD9XAl27FamSrS7SWBUYTBmmSvVT08Z9hzKAYz49Ykf2jEjwMgI5OGPUWcDxEAHvEMgXkj9gYciS8uMhSjMROVCr5L4Bm4RWXBjThz9y/bjSBpjR4z2VmLoUgclBHWNBOWJaP0sQRtwgHako7bL5epUIFlUek0GJtBhVKmdDOpwI20wxHNdAcYZ8WlNI3pl5ntTA5dyQt+hyEdJoFQRnI0ZfGXmrFVFMnVU2aOnUNDuh63+BN2AgGEyzwngWR/XRZgojKwVAK3hyVzOKAD3LBUIA3T/5E5EjCmm4yEyWQHAZgwHW6yckAGn4xpY39ZEALBPBiCRTAMQwIokTMEgqDIf+N3Is9k7o3FzAwBy0kYIBQIeALACLyywYyAHR7ylQ1hViNtL3OFThsCobBrLh8ZaYNhPs/FxmB1sQmEqItNLHbJRRDXcxEezzWOhwpdbHzEpV6EaRqnjbZghAm32OlWUxSjXJ5DlxEyIClYh+AYCvMwCIN4sNiAYpAQESECHQSJxbiIb5AEG/S4niECRXViVKQTQyIxhDE4DDox36gXCCGJFowgjRTgUJUa1BCrmCEuYDiz0piFdjCGiFCY5ecXppSz/glQSwcIM2het9cHAACfDAAAUEsBAhQAFAAIAAgAWVlRQzNoXrfXBwAAnwwAABEAAAAAAAAAAAAAAAAAAAAAAFBhY2thZ2VMYWJlbHMucGRmUEsFBgAAAAABAAEAPwAAABYIAAAAAA==</PdfDocument>
<Checksum>n0Cdtwupz9V68HnMp0Go7Q==</Checksum>
</TransportDocument>
</GetPackageLabelsResult>
<ResponseMetadata>
<RequestId>87a9d81a-6ec4-4d45-b2fc-8d0e6dfbe0a8</RequestId>
</ResponseMetadata>
</GetPackageLabelsResponse>
In PHP I've successfully extracted the PdfDocument string into $data, which matches the above. I then output the data using the following:
$pdf_document = base64_decode($data);
header("Content-type: application/pdf");
header("Content-Length: " . strlen($pdf_document));
header("Content-Disposition: inline; filename=PackageLabels.pdf");
echo $pdf_document;
exit();
I have checked that the script isn't outputting any data before the pdf data is echoed out. I've also used file_put_contents($filename, $pdf_document) to save the data to a file and that file is also corrupted.
I also can't get the md5 checksum to match:
echo base64_encode(md5($pdf_document, true)); // outputs: Pn0C06ZOgzDBfYwWXBvvow==
// from response: n0Cdtwupz9V68HnMp0Go7Q==
The contents of that PdfDocument is a ZIP file - the first three letters ("UEs") decode to "PK" which is the magic identifier for ZIP files. For testing purposes, you can use this website to download a copy of that ZIP. PHP has functions that let you do the same, you'll have to check what's compiled into your PHP version.
The contents of said ZIP archive is a single file labeled PackageLabels.pdf which is the PDF you're looking for.
The Checksum is also base64-encoded and the one in your example decodes to 9f409db70ba9cfd57af079cca741a8ed. This is supposed to be the MD5 hash of the document data. However, I have yet to find out how exactly it is calculated. It did NOT match the MD5 of any of the following:
The raw Base64 encoded string
The zip file
The contained pdf file
I am trying to fetch the meta information from URL results passed after a search. I have been using the OpenGraph library and also PHP's native get_meta_tags function to retrieve the meta tags.
My problem is when I am reading through the contents of a URL that happens to be a .m4v extension. The program tries to read the contents of that file but it is way too large (and not mention, completely useless as it is all junk) and my program refuses to let it go. Therefore, I am stuck till the program throws a timeout error and moves on.
Is there any way to stop reading the contents of the file if it is way too large? I tried file_get_contents() with the maxlen parameter, but it still seems to read through the entire page. How can I quickly determine if a file is structured with tags before I dive in to farm it for meta?
get_headers() is what you need, there's a Content-Type and Content-Length in the response that you might be interested in.
You might want to:
$headers=get_headers($url,1);
Use php's filesize($yourFile); to find the file size in bytes:
$size = filesize($yourFile);
if ($size < 1000) {
$string = file_get_contents($yourFile);
}
I've got a webservice which expects a parameter of type "xs:base64Binary" - this is a file to store in the database.
I'm trying to consume the service using PHP 5's native webservice classes. I've tried a few things:
// Get the posted file
$file = file_get_contents($_FILES['Filedata']['tmp_name']);
// Add the file, encoding it as a base64
$parameters = array("fileBytes" => base64_encode($file));
// Call the webservice
$response = $client->attachFile($parameters);
The result is an error saying "Bad Request." If the file is a text file and I don't base64_encode, it works fine. Problem results when posting a binary file such as an image.
Anyone know the trick here?
EDIT 1
Also problematic is if I encode the text file, it seems to work but of course it's encoded and ends up being junk once downloaded and viewed again (i.e, the text is encoded and doesn't seem to get de-coded by the server).
As far as I know, base64_encode() should be doing the job.
Are you 100% sure $file contains something? Have you made a dump?
Ok, so it seems there is no need to use base64_encode. The file_get_contents already puts it into the required format.
Additionally, the problem was because I had the server side config setting for the maxArrayLength too low.