Jump to content

Recommended Posts

<?php


$url = "http://www.partner.viator.com/partner/admin/tools/links_feeds/downloadFeed.jspa?feed=Products&PUID=13594";

$response = file_get_contents($url);
$json_arr = json_decode($response, true);

echo "<pre>";
print_r($json_arr);


?>

hi I have been having trouble outputting the following url contents to screen becuae it is in a zipped format.  I have done quite a few url downloads but not one that is zipped and I cant seem to figure out what to do, here is my code

 

 

 

Link to comment
https://forums.phpfreaks.com/topic/286821-unzip-url-download/
Share on other sites

<?PHP
  //use latest minorRev 14
  $url  ='http://www.partner.viator.com/partner/admin/tools/links_feeds/downloadFeed.jspa?feed=Products&PUID=13594';

  $header[] = "Accept: application/json";
  $header[] = "Accept-Encoding: gzip";
  $ch = curl_init();
  curl_setopt( $ch, CURLOPT_HTTPHEADER, $header );
  curl_setopt($ch,CURLOPT_ENCODING , "gzip");
  curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'GET');
  curl_setopt( $ch, CURLOPT_URL, $url );
  curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );
  $response = json_decode(curl_exec($ch), true);
  //$response = curl_exec($ch);
  ?>
  <html>
  <body>
  <table>
   <Tr>
   <TD><?PHP

    print_r($url);
    print_r("<BR>");
    print_r("<pre>");
    print_r ($response);
    print_r("</pre>");


    ?>
  </TD>
  </Tr>
  </table>
  </body>
</html>

ok I have tried this so far but it only displays the url and not the results, any ideas what could be wrong

 

 

 

 

Link to comment
https://forums.phpfreaks.com/topic/286821-unzip-url-download/#findComment-1471954
Share on other sites

Your issue is with headers. Both in the curl request and also with what you are trying to output.

 

A side issue is that you are putting the response in a variable and then attempting to output it. While it is possible to do this (if you output the proper headers first), it's not really advisable. Problem is that who knows how big the file is. So if you attempt to put a 1gb file into a string then there's a good chance php is going to run out of memory. It would be better to just let the file pass through directly to the the client.

 

So, having said that, this is what your code should look like:

 

<?php 
// callback function to set the appropriate 
// headers for the zip file to be pushed to the client
function curlHeaderCallback($ch, $header) { 
  if (preg_match('/^HTTP/i', $header)) { 
    header($header); 
    header('Content-Disposition: attachment; filename="file-name-here.zip"'); 
  } 
  return strlen($header); 
} 

$url  ='http://www.partner.viator.com/partner/admin/tools/links_feeds/downloadFeed.jspa?feed=Products&PUID=13594';

$ch = curl_init(); 
curl_setopt($ch, CURLOPT_URL, $url); 
curl_setopt($ch, CURLOPT_BINARYTRANSFER, 1); 
curl_setopt($ch, CURLOPT_FAILONERROR, 1); 
// this is to set the headers to be output to the client
curl_setopt($ch, CURLOPT_HEADERFUNCTION, 'curlHeaderCallback'); 

curl_exec ($ch); 

// output a message if the curl failed
$intReturnCode = curl_getinfo($ch, CURLINFO_HTTP_CODE); 
if ($intReturnCode != 200) { 
  echo 'was error: ' . $intReturnCode; 
} 

curl_close ($ch); 

?>
Link to comment
https://forums.phpfreaks.com/topic/286821-unzip-url-download/#findComment-1472029
Share on other sites

You know.. you said you wanted to output the zip, and all of your coding efforts have reflected that. Now you are saying you want to put it in a database. Not being clear about what you want is a good way to get people to stop giving you help even when you throw money at them, and you're not even throwing money. I suggest moving forward you try your very hardest to explain what it is you are actually trying to do so that you don't waste anybody's time.

 

Here is how you can download the file directly to your server instead of output it to the browser. This will put the file in the same directory that the script is run.

<?php 
$url  ='http://www.partner.viator.com/partner/admin/tools/links_feeds/downloadFeed.jspa?feed=Products&PUID=13594';
$ch = curl_init(); 
$fp = fopen (dirname(__FILE__) . '/file-name-here.zip', 'w+'); 
curl_setopt($ch, CURLOPT_FILE, $fp); 
curl_setopt($ch, CURLOPT_URL, $url); 
curl_setopt($ch, CURLOPT_BINARYTRANSFER, 1); 
curl_exec ($ch); 
curl_close ($ch); 
fclose($fp);
?>

I want to obtain it myself and load it into the MySQL database to query

That is a whole other can of worms to deal with, and again, my ability to help is hampered by the fact that you aren't providing necessary information. Are you just wanting to put the name of the zip file in some column so you can reference by name? Are you wanting to put the actual zip file into a column? Are you wanting to parse the file and put data from the file into separate columns and/or tables? If it's the last, then you need to unzip the file, and you will need to provide details about file structure and values and mappings to your database.

Link to comment
https://forums.phpfreaks.com/topic/286821-unzip-url-download/#findComment-1472145
Share on other sites

hey Josh

 

sorry about this mistake I will try my upmost not to mislead anyone again, at first I was initially thinking of displaying the results of the download to the browser but got lost in the new technology of what I am actually trying to achieve........My plan now is to grab the file and insert it into a MySQL database with their updated prices, that way we can design our own search form to enable the user to find activities and prices etc and present this in a readable manner....

 

sorry for any inconvenience caused and thanks for your assistance

 

alan

Link to comment
https://forums.phpfreaks.com/topic/286821-unzip-url-download/#findComment-1472268
Share on other sites

Hi Josh

 

 

with an update to your question, I have managed to get the file to download in zip format now to my home directory, what I am actually trying to do is load it into the database to create a form so users can search for activities,

 

the plan is this, grab the file which I have now managed to do and it goes into the home directory as stated with the code you supplied.

 

I want to initially do is put this in another directory eg /feed directory to then load this file on a nightly basis into MySQL database, note table is not created yet...

 

thanks

Link to comment
https://forums.phpfreaks.com/topic/286821-unzip-url-download/#findComment-1472278
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.