Using Array Variables For FILE_GET_CONTENTS

Posted by Whoshooter on Stack Overflow See other posts from Stack Overflow or by Whoshooter
Published on 2013-11-02T02:05:05Z Indexed on 2013/11/02 3:53 UTC
Read the original article Hit count: 238

Filed under:
|

I have a script all done now and everything has been debugged and it works, except for the last hurdle. This script grabs pertinent information on bank web sites, takes that data and uses it to populate a template and then it's all posted to Wordpress - BUT I get an error because the file_get_contents function fails taking each url from the Array.

I've var_dumped the array and all the URLS are there in the [0] key, so this is what I tried.

  • master data is the scraped URL source the script uses
  • urlscrape_array is the collection of URLS

    $master_data = file_get_contents($urlscrape_array[0]);

When I run the script using a URL like below it works beautifully every time;

   $master_data = file_get_contents("http://www.somesite/somepage.html");

This is the error I get when I try to use the first example'

   Warning: file_get_contents() expects parameter 1 to be string, array given    in /home3/path/public_html/mysite.com/boise_project/scriptmainpage.php on line 13

As requested here is a sample of the var_dump on $urlscrape_array[0]

  array(504) { [0]=> string(56) "http://www.somepage.com/somepage-3178.html" [1]=> string(54) "http://www.somepage.com/somepage-16.html" [2]=> string(56) "http://www.somepage.com/somepage-3202.html" [3]=> string(56) "http://www.somepage.com/somepage-4324.html" [4]=> string(56) "http://www.somepage.com/somepage-4777.html" [5]=> string(56) "http://www.somepage.com/somepage-5140.html" [6]=> string(56) "http://www.somepage.com/somepage-5220.html" [7]=> string(56) "http://www.somepage.com/somepage-9205.html" [8]=> string(56) "http://www.somepage.com/somepage-3251.html" [9]=> string(56) "http://www.somepage.com/somepage-3323.html" [10]=> string(56) "http://www.somepage.com/some-page-3797.html" [11]=> string(56) "http://www.somepage.com/some-page-4145.html" [12]=> string(56) "http://www.somepage.com/some-page-3191.html" [13]=> string(55) "http://www.somepage.com/some-page-329.html" [14]=> string(56) etc....


   Error as per the foreach statement provided by Uptown
   Warning: Invalid argument supplied for foreach() in /home3/bettyt45/public_html/bdbud.com/boise_project/boise-wordpress.php on line 12

NULL

print_r resulst below;

    Array ( [0] => Array ( [0] => http://www.somesite.com/some-page-3178.html [1] => http://www.somesite.com/some-page-16.html [2] => http://www.somesite.com/some-page-3202.html [3] => http://www.somesite.com/some-page-4324.html [4] => http://www.somesite.com/some-page-4777.html [5] => http://www.somesite.com/some-page-5140.html [6] => http://www.somesite.com/some-page-5220.html [7] => http://www.somesite.com/some-page-9205.html [8] => http://www.somesite.com/some-page-3251.html [9] => http://www.somesite.com/some-page-3323.html [10] => http://www.somesite.com/some-page-3797.html [11] => http://www.somesite.com/some-page-4145.html [12] => http://www.somesite.com/some-page-3191.html [13] => http://www.somesite.com/some-page-329.html [14] => http://www.somesite.com/some-page-3341.html [15] => http://www.somesite.com/some-page-3758.html [16] => http://www.somesite.com/some-page-4180.html [17] => http://www.somesite.com/some-page-9014.html [18] => http://www.somesite.com/some-page-5987.html [19] => http://www.somesite.com/some-page-1542.html [20] => http://www.somesite.com/some-page-3004.html [21] => http://www.somesite.com/some-page-9034.html [22] => http://www.somesite.com/some-page-3385.html [23] => http://www.somesite.com/some-page-3435.html [24] => http://www.somesite.com/some-page-6389.html [25] => http://www.somesite.com/some-page-6992.html [26] => http://www.somesite.com/some-page-7051.html 

HERE IS THE CODE I USED TO CREATE THE ARRAY ABOVE;

       $urlscrape_data = file_get_contents('http://www.bdbud.com/boise_project/boise-urls.htm');
       preg_match_all('~http\:\/\/www.creditunionsonline.com\/credit\-union\-\d{1,4}?\.html~',   $urlscrape_data, $urlscrape_matches);

$urlscrape_array = $urlscrape_matches;

© Stack Overflow or respective owner

Related posts about php

Related posts about arrays