PHP and CURL

onyourmark
onyourmark used Ask the Experts™
on
Hi, I have the following script. I am trying to access
http://search.twitter.com/search?q=Japan
and have the results written to a file, but the file is empty. Can anyone see why and how I can fix it?
Thanks.
<?php
    $curl = curl_init();
    $fp = fopen("somefile.txt", "w");
    curl_setopt ($curl, CURLOPT_URL, "http://search.twitter.com/search?q=Japan");
    curl_setopt($curl, CURLOPT_FILE, $fp);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
 
    $result = curl_exec ($curl);
 
    curl_exec ($curl);
    curl_close ($curl);
?>

Open in new window

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Commented:
You're not writing anything to the file.  See the attached code snippet.
<?php
    $curl = curl_init();
    $fp = fopen("somefile.txt", "w");
    curl_setopt ($curl, CURLOPT_URL, "http://search.twitter.com/search?q=Japan");
    curl_setopt($curl, CURLOPT_FILE, $fp);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
 
    $result = curl_exec ($curl);
    // Need to write to your local file, or else it will be empty
    fwrite($fp, $result);
    // It's a good idea to explicitly close your file handles in your code rather than relying on PHP to clean it up for you
    fclose($fp);
    curl_exec ($curl);
    curl_close ($curl);
?>

Open in new window

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial