oracle765 Posted March 9, 2014 Share Posted March 9, 2014 <?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 Quote Link to comment Share on other sites More sharing options...
.josh Posted March 9, 2014 Share Posted March 9, 2014 use ZipArchive to unzip it. Or, if you're looking to make the browser prompt a download of the zip file instead of output gibberish, use header to set the content type (read the user notes, there are entries for what to set for zip files). Quote Link to comment Share on other sites More sharing options...
oracle765 Posted March 9, 2014 Author Share Posted March 9, 2014 <?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 Quote Link to comment Share on other sites More sharing options...
oracle765 Posted March 10, 2014 Author Share Posted March 10, 2014 im totally at a loss I have tried allsorts Quote Link to comment Share on other sites More sharing options...
.josh Posted March 10, 2014 Share Posted March 10, 2014 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); ?> Quote Link to comment Share on other sites More sharing options...
oracle765 Posted March 11, 2014 Author Share Posted March 11, 2014 that gets the file now, its actually about 7mb, but I don't want to give this zipped file to the user, I want to obtain it myself and load it into the MySQL database to query Quote Link to comment Share on other sites More sharing options...
oracle765 Posted March 11, 2014 Author Share Posted March 11, 2014 maybe I am going the wrong way about it Quote Link to comment Share on other sites More sharing options...
.josh Posted March 11, 2014 Share Posted March 11, 2014 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. Quote Link to comment Share on other sites More sharing options...
oracle765 Posted March 12, 2014 Author Share Posted March 12, 2014 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 Quote Link to comment Share on other sites More sharing options...
oracle765 Posted March 12, 2014 Author Share Posted March 12, 2014 how do you actually run this php file to do that, I now have the php file sitting on my server. I have tried going to the browser and entering the url and pressing enter but nothing seems to happen thankyou Quote Link to comment Share on other sites More sharing options...
oracle765 Posted March 12, 2014 Author Share Posted March 12, 2014 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 Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.