1 Replies - 360 Views - Last Post: 11 September 2013 - 09:22 AM Rate Topic: -----

#1 Tech4LLC  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 08-May 13

Trouble with file_get_contents() accepting a variable

Posted 11 September 2013 - 09:19 AM

So I am currently working with the Wunderground API using PHP. However, there seems to be a little snag I've encountered. I have stored a couple Wunderground API keys in a text file named Keys.txt. I then want to retrieve each said key and store them into an array of strings, where I can then reference them to make a complete URL that will eventually be used to make the call and retrieve the data. Below is my code:

	// This retrieves my Wunderground keys.
	$keys = file_get_contents('Keys.txt');
	/* This splits the keys properly into an array.
	 * While I don't understand why, the first element,
	 * equating to the first line, always returns "",
	 * but otherwise, the array is stored just fine.
	 * This is an example of what the array will look
	 * like if I have stored 2 keys:
	 * [0] ""
	 * [1] "0aa00aa0000aa0aa"
	 * [2] "1bb1b1b11bbbb1bb"
	 * According to var_dump(), each of these are
	 * strings.
	$apiKey = explode(",", $keys);
	/* This concatenates everything together to form
	 * the full URL used to make the call to
	 * Wunderground and retrieve the current weather
	 * alerts of the area. Notice I am using in the
	 * first key, e.g. "0aa00aa0000aa0aa", which is
	 * contained in apiKey's second element (apiKey[1])
	 * which, according to var_dump() is indeed a string
	 * variable. That said, this all is stored together
	 * into the string variable, fullUrl, which should
	 * then look like this:
	 * "http://api.wunderground.com/api/0aa00aa0000aa0aa/alerts/q/34.933889,-103.760556.json"
	$URLWithVar = 'http://api.wunderground.com/api/' . 
		$apiKey[1] . 
	/* Just for giggles, I'll also use those weird
	 * characters, "", in the first element, too.
	 * This variable should now look like this:
	 * http://api.wunderground.com/api//alerts/q/34.933889,-103.760556.json
	$BadURL = 'http://api.wunderground.com/api/' . 
		$apiKey[0] . 
	/* For testing purposes, I'm going to also store
	 * another string variable, but this time without
	 * referencing the string variable that's holding the
	 * key.
	$URLWithoutVar = "http://api.wunderground.com/api/0aa00aa0000aa0aa/alerts/q/34.933889,-103.760556.json"
	/* This errors with the following error:
	 * Warning: file_get_contents() expects parameter 1 to be a valid path, string given in D:\Program Files\xampp\htdocs\Structures\test.php on line 59
	$alertDataWithVar = file_get_contents($URLWithVar);
	// This works properly.
	$alertDataWithoutVar = file_get_contents($URLWithoutVar);
	/* Interestingly, the URL with the "" characters
	 * by using apiKey[0] *does* work, though not
	 * surprisingly, it only returns Wunderground's
	 * own response stating that it was an invalid key.
	 * But unlike the first attempt at the call with
	 * the real key, this does not truly error, and
	 * still calls Wunderground and stores the response
	 * it gets from Wunderground.
	$alertDataWithBadURL = file_get_contents($BadURL);

As you can see, it seems like the way it's storing my keys as strings seem to throw a wrench into the next file_get_contents() call. Because if I just plug in the key in one long string, it works, and if I reference that first element which contains the weird "" characters, it works as well. It only errors when I try to use the keys that were retrieved from the Keys.txt file. And yes, I have tried typecasting everything explicitly into strings and I still get the exact same results. Any thoughts on how to solve this problem?

Is This A Good Question/Topic? 0
  • +

Replies To: Trouble with file_get_contents() accepting a variable

#2 no2pencil  Icon User is offline

  • Professor Snuggly Pants
  • member icon

Reputation: 6316
  • View blog
  • Posts: 29,981
  • Joined: 10-May 07

Re: Trouble with file_get_contents() accepting a variable

Posted 11 September 2013 - 09:22 AM

** Duplicate Topic **
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1