Jump to content

curl with variables - nytimes


zrosen88

Recommended Posts

Hey all,

 

I'm kinda new here (as a poster at least) and any help would be great!

 

I'm trying to use cURL to save a news article from nytimes.com -- my script is:

 

#!/usr/local/bin/php -q
<?php
echo exec("curl -o /path/to/server/homecontent/vars.txt http://www.nytimes.com/2008/07/08/business/08fannie.html?_r=1&partner=rssnyt&emc=rss&oref=slogin");
?>

 

I know the script is fine, because when I change the nytimes link to something else (i.e. http://www.google.com/) it works exactly as it should.

 

I thought it could be due to the variables at the end, but even cutting out the

?_r=1&partner=rssnyt&emc=rss&oref=slogin

makes no difference for me.

 

Any help would be greatly appreciated!

 

Zack

Link to comment
https://forums.phpfreaks.com/topic/113663-curl-with-variables-nytimes/
Share on other sites

I was under the impression what I am using was related to the PHP cURL functions. I'll take a look around at them, though, thanks!

 

Any idea why what I'm currently using isn't working for the NYTimes site, however?

 

Much thanks, Ken!

So I tried another idea, using more cURL functions:

<?php
$ch = curl_init("http://www.nytimes.com/2008/07/08/business/08fannie.html?_r=1&partner=rssnyt&emc=rss&oref=slogin");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_BINARYTRANSFER, true);
$output = curl_exec($ch);

$fh = fopen("x.txt","w");
fwrite($fh, $output);
fclose($fh);
?>

 

The file, x.txt, is created, however it is blank. All permissions are as they should be (0777).

 

Zack

Hey DarkWater,

 

Thanks, but unfortunately my host doesn't allow that (Dreamhost). Fantastic service, fantastic support, fantastic uptime, but they don't allow file_get_contents

 

In a measure to improve security, DreamHost has disabled the PHP option allow_url_fopen. This would normally allow a programmer to open, include or otherwise use a remote file using a URL, rather than a local file path.

 

Zack

Hey Ken --

 

You mean like this?

 

<?php
$ch = curl_init("http://www.nytimes.com/2008/07/08/business/08fannie.html?_r=1&partner=rssnyt&emc=rss&oref=slogin");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_BINARYTRANSFER, true);

curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);

$output = curl_exec($ch);

$fh = fopen("x.txt","w");
fwrite($fh, $output);
fclose($fh);
?>

 

If so, still nothing  ??? Unless I did something I shouldn't have; I'm relatively new to cURL, and apologize for any ignorance!

 

Zack

This is the code I used that worked (with my debugging statements still in place):

<?php
$ch = curl_init("http://www.nytimes.com/2008/07/08/business/08fannie.html?_r=1&partner=rssnyt&emc=rss&oref=slogin");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_BINARYTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
$output = curl_exec($ch);
echo '<pre>curl_getinfo:' . print_r(curl_getinfo($ch),true) . '</pre>';
if ($output == '') echo 'Error: ' . curl_error($ch). '<br>';
else {
echo '<pre>Output: ' . htmlentities($output) . '</pre>';
$fh = fopen("x.php","w");
fwrite($fh, $output);
fclose($fh);
}
?>

 

Ken

Ken --

 

Perfect! That did it!  ;D

 

Now I just have to personally figure out how to work around the advertisements that NYTimes puts randomly between viewing articles!

 

Thanks again! Couldn't have done it without you!

 

Zack Rosen, Website Director

88.7FM WNHU

CT's #1 College Radio Station

http://www.wnhu.net/

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.