0

I'm requesting a zip file from a remote location, and I'm getting some strange affects here. Here is a snippet:

$result = drupal_http_request($serviceUrl, $options);
    $file = $result->data;

    dpm($result, 'file result'); //returns the object, and I can see $result->data is a string of characters
    dpm($result->data, 'data'); //But, this is empty
    dpm($file, 'file'); //This is also empty
    dpm($result, 'file result'); // Still shows me the object with $result->data

The http request works fine as it returns with a code 200

enter image description here

I want to use it as follows (example):

temp_filename = "my_zip.zip";
$zip_file = fopen($temp_filename, "w+");
fwrite($zip_file, $file); //$file is $result->data, but is empty.
fclose($zip_file);

Anybody got an idea why I can't get $result->data?

The code is inside an object method, not a standard PHP function. Not sure if that has anything to do with it.

Also worth mentioning that I can read $result->code and $result->status_message with no problems.

| improve this question | | | | |
  • 1
    There's definitely a result in data, you can see it in the Krumo output, so you can definitely 'get' it...what problem are you having exactly? It's not possible for data to be there and not be there at the same time, and since it's definitely there, I can't imagine what issue you're having. Does the file not get written or is it corrupt? If it's the latter, use a proper method to write the file eg ZipArchive. – Clive Sep 12 '14 at 7:07
  • Well that is what's confusing me as well. There is definitely data in the $result object. And the data is definitely a string of characters (which is the content of a zip file). But if I take the data out ($file = $result->data), the $file is blank. I don't need to zip it, it is already a zip file I'm downloading, and I need to unzip it later on. Maybe I should change my question to: How to download a zip file using drupal_http_request, because I can't find any examples anywhere. – Quentin Campbell Sep 12 '14 at 7:33
  • 1
    Probably to do with the fact zip files are binary (you're currently writing as ASCII). Not sure drupal_http_request is the best tool for this particular job. Better to use system_retrieve_file or your favourite standard php method – Clive Sep 12 '14 at 7:51
  • Got it working. I need to use drupal_http_request as I am passing custom tokens in header. system_retrieve_file is basically a wrapper for drupal_http_request, where I can't set headers. And the reason I can't see the data is probably what you said it being binary. I got it working by re writing it in a standard function putting it all together in one function. Thanks! – Quentin Campbell Sep 12 '14 at 8:15
  • No worries :) It'd be great if you could put in an answer detailing how you solved it, just so this will be useful to future visitors – Clive Sep 12 '14 at 8:40
3

I got it working myself. Kudos to Clive who mentioned that the data is binary which probably means why I can't view it with dpm().

In closure, here is a code example to fetch a zip file with drupal_http_request where you can set header information.

function MY_MODULE_download_zip($service_url, $token, $request_body) {
    $request_headers = array(
        'Content-Type' => 'text/xml', //sending additional data as XML
        'Content-Length' => strlen($request_body), //See options['data']
        'charset' => 'utf-8',
        'loginToken' => $token, //I needed to pass a login token
        'Cache-Control' => 'no-cache',
        'Pragma' => 'no-cache',
        'Connection' => 'keep-alive'
    );

    $options = array(
        'method' => 'POST',
        'headers' => $request_headers,
        'data' => $request_body, //I needed to pass more info here as a XML
        'timeout' => 60.0, //it's a big file
    );
    $result = drupal_http_request($service_url, $options);

    if ($result->code != 200) {
      watchdog('ERROR', 'ERROR: :error', array(':error' => $result->error);
      return FALSE;
    }

    $temp_filename = "my_zip_file.zip";
    $zip_file = fopen($temp_filename, "w+");
    fwrite($zip_file, $result->data);
    fclose($zip_file);

    return TRUE;
}
| improve this answer | | | | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.