Jump to content

Transferring fopen to cURL


jmg498

Recommended Posts

I'm pretty new with PHP, but a script I had been running for so long relied on fopen to retrieve information about a data file.  The host I am using decided to disable fopen, so now I'm stuck trying to modify the code over to use cURL. 

 

Here is the original code:

 

$crdat = fopen( "ftp://tgftp.nws.noaa.gov/SL.us008001/DF.of/DC.radar/DS.p37cr/SI.".$rregi.$radar."/sn.last" , "r" ) or die($lerro);

 

And here is my pathetic attempt to modify it to use cURL:

 

function get_content_curl($url) {
      $ch = curl_init();
      curl_setopt ($ch, CURLOPT_URL, $url);
      curl_setopt ($ch, CURLOPT_HEADER, 0);
      curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
      $string = curl_exec ($ch);
      curl_close ($ch);
      return $string;
} 

$url = "ftp://tgftp.nws.noaa.gov/SL.us008001/DF.of/DC.radar/DS.p37cr/SI.".$rregi.$radar."/sn.last";
$crdat = get_content_curl($url);

 

This modification does not work (no surprise), and any help someone could offer would be greatly appreciated.  That is, assuming the help offered is something more than "Take a PHP class dummy"  :)

 

Thank you!

Link to comment
https://forums.phpfreaks.com/topic/144405-transferring-fopen-to-curl/
Share on other sites

They disabled $page = file_get_contents ( "ftp://tgftp.nws.noaa.gov/SL.us008001/DF.of/DC.radar/DS.p37cr/SI." . $rregi . $radar . "/sn.last" ); too?

 

I'm assuming so (if that's possible) because I already tried using file_get_contents and it doesn't work.  The sucky thing is I don't get any errors returned either.  It appears to try and load some data but then just goes to a blank page.

 

When I contacted the tech support people, they told me I should use cURL.  And didn't offer much more assistance than that.  I guess that's what you get when you pay for an inexpensive hosting service.  >:(

Try this...

 


<?php

$rregi  = '';
$radar = ''
$url     = "ftp://tgftp.nws.noaa.gov/SL.us008001/DF.of/DC.radar/DS.p37cr/SI." . $rregi . $radar . "/sn.last";

$io = curl_init ();

curl_setopt ( $io, CURLOPT_URL, $url );

curl_setopt ( $io, CURLOPT_FTP_USE_EPSV, true );

curl_setopt ( $io, CURLOPT_FTPASCII, true );

curl_setopt ( $io, CURLOPT_FTPPORT, 21 );

curl_setopt ( $io, CURLOPT_HEADER, false );

curl_setopt ( $io, CURLOPT_RETURNTRANSFER, true );

curl_setopt ( $io, CURLOPT_USERAGENT, 'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0)' );

$out = curl_exec ( $io );

curl_close ( $io );

echo $out;
?>

Change my last example to this... (tested)

 


<?php
$rregi  = '';
$radar = ''
$url = "ftp://tgftp.nws.noaa.gov/SL.us008001/DF.of/DC.radar/DS.p37cr/SI." . $rregi . $radar . "/sn.last";
$io = curl_init ();

curl_setopt ( $io, CURLOPT_URL, $url );

curl_setopt ( $io, CURLOPT_USERPWD, 'anonymous:[email protected]' );

curl_setopt ( $io, CURLOPT_FTP_USE_EPSV, true );

curl_setopt ( $io, CURLOPT_HEADER, false );

curl_setopt ( $io, CURLOPT_RETURNTRANSFER, true );

curl_setopt ( $io, CURLOPT_USERAGENT, 'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0)' );

$out = curl_exec ( $io );

curl_close ( $io );

echo $out;

?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.