7 Replies - 620 Views - Last Post: 11 September 2013 - 10:39 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:20 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:

<?php
	// 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] . 
		'/alerts/q/34.933889,-103.760556.json';
	
	/* 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] . 
		'/alerts/q/34.933889,-103.760556.json';
	
	/* 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 andrewsw  Icon User is offline

  • Fire giant boob nipple gun!
  • member icon

Reputation: 3326
  • View blog
  • Posts: 11,248
  • Joined: 12-December 12

Re: Trouble with file_get_contents() accepting a variable

Posted 11 September 2013 - 09:40 AM

I assume it might be a problem with character encoding. I would open the txt file in something like Notepad++; what character encoding does it indicate? This is usually at the bottom-right corner of Notepad++. The first character (or two) can be byte order marks.

I'm not very experienced in this area though. Perhaps someone else can offer an insight.
Was This Post Helpful? 0
  • +
  • -

#3 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6052
  • View blog
  • Posts: 23,487
  • Joined: 23-August 08

Re: Trouble with file_get_contents() accepting a variable

Posted 11 September 2013 - 10:20 AM

As andrewsw said, if you saved the Keys.txt file in UTF-8 format (which is something Notepad will do) a byte-order marker is inserted into the start of the document.

Use Notepad++ (a free program) instead.
Was This Post Helpful? 0
  • +
  • -

#4 Atspulgs  Icon User is offline

  • D.I.C Regular

Reputation: 68
  • View blog
  • Posts: 380
  • Joined: 29-July 09

Re: Trouble with file_get_contents() accepting a variable

Posted 11 September 2013 - 10:21 AM

If you print this out $URLWithVar, does it look any different from the expected url? I would probably say something similar to andrew, but he said it already :)
Was This Post Helpful? 0
  • +
  • -

#5 Tech4LLC  Icon User is offline

  • New D.I.C Head

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

Re: Trouble with file_get_contents() accepting a variable

Posted 11 September 2013 - 10:22 AM

I am already using Notepad++ as my editor, although it does indicate as ANSI as UTF-8. Would this be the cause of it all?
Was This Post Helpful? 0
  • +
  • -

#6 Tech4LLC  Icon User is offline

  • New D.I.C Head

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

Re: Trouble with file_get_contents() accepting a variable

Posted 11 September 2013 - 10:28 AM

As for the printing of the $URLWithVar, it does print the URL properly. In fact, I can even then copy-paste it into its own query and it works fine.
Was This Post Helpful? 0
  • +
  • -

#7 Atspulgs  Icon User is offline

  • D.I.C Regular

Reputation: 68
  • View blog
  • Posts: 380
  • Joined: 29-July 09

Re: Trouble with file_get_contents() accepting a variable

Posted 11 September 2013 - 10:38 AM

When you var_dump the particular string, does its size match the amount of characters you see? I honestly cant think of anything else, maybe theres a character thats not rendered but can still mess with the function call.
Was This Post Helpful? 0
  • +
  • -

#8 Tech4LLC  Icon User is offline

  • New D.I.C Head

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

Re: Trouble with file_get_contents() accepting a variable

Posted 11 September 2013 - 10:39 AM

The var_dump does register the correct amount of characters.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1