dianeinflorida
dianeinflorida

Reputation: 53

In a foreach loop and write a file

I currently have this code:

$json = file_get_contents('http://openstates.org/api/v1/legislators/?active=true&state=fl&&chamber=upper&apikey=ba09e678541e4b6ea28348c4517a6d50');
$obj = json_decode($json);


foreach($obj as $index => $user) 
    echo $user->photo_url;

Works great at echoing the list. What I would like to do instead of echoing the information could I possibly take the links and download the image files?

Or instead of echoing save the names to a one big file list?

I have been trying all sorts of of stuff for the past 2 days. Any help would be greatly appreciated.

Upvotes: 0

Views: 1106

Answers (3)

Cranespud
Cranespud

Reputation: 1

What about:

foreach($obj as $index => $user) 
    exec("wget $user->photo_url -O $path_to_local_image");

Upvotes: 0

dm03514
dm03514

Reputation: 55962

You have to try something. If you want to download the url you can use file_put_contents('image_name', file_get_contents($user->photo_url))

or you can open up a file and write all the names to it. Just typing "writing to file in php" will give you php documentation on file_put_contents and fopen. One can use fopen to open a file for ewriting and write to that file using fwrite

Upvotes: 1

Related Questions