If i push a string which doesn't exist or misspelled to function benzer($ilk_artist) i am getting errors bottom. Whether i push a valid artist name or not variable $completeurl is always defined. So i can't put if($completeurl) I think i should control whether $completeurl is valid before simplexml_load_file($completeurl). Do you have an idea that how i can do it?
function benzer($ilk_artist)
{
$completeurl = 'http://ws.audioscrobbler.com/2.0/?method=artist.getsimilar&artist='.trim($ilk_artist).'&api_key='.LASTFM_APP_ID;
$completeurl = urlencode($completeurl);
$xml = simplexml_load_file($completeurl);
if(!$xml)
{
return false;
}
$artists = $xml->similarartists->artist;
$length = count($artists);
for ($i = 0; $i < $length; $i++) {
$artistname[$i] = $artists[$i]->name;
}
return simplexml_kurtul($artistname);
}
errors:
[17-Dec-2012 11:43:33] PHP Warning: simplexml_load_file(http://ws.audioscrobbler.com/2.0/?method=artist.getsimilar&artist=tgyu+6uh6n&api_key=APIKEY) [<a href='function.simplexml-load-file'>function.simplexml-load-file</a>]: failed to open stream: HTTP request failed! HTTP/1.0 400 Bad Request
in /home6/.../public_html/.../functions/fns.php on line 102
[17-Dec-2012 11:43:33] PHP Warning: simplexml_load_file() [<a href='function.simplexml-load-file'>function.simplexml-load-file</a>]: I/O warning : failed to load external entity "http%3A%2F%2Fws.audioscrobbler.com%2F2.0%2F%3Fmethod%3Dartist.getsimilar%26artist%3Dtgyu+6uh6n%26api_key=APIKEY0" in /home6/.../public_html/.../functions/fns.php on line 102
What about not printing out the warnings by adding '#'?
$xml = #simplexml_load_file($completeurl);
if(!$xml)
{
return false;
}
Related
Error:
Warning: simplexml_load_file(): feed.rss:2393: parser error : xmlParseEntityRef: no name in C:\xampp\htdocs\mklbet\index.php on line 16
Warning: simplexml_load_file(): <title>We have a winner & it is ME</title> in C:\xampp\htdocs\mklbet\index.php on line 16
My problem is that when i use simplexml_load_file($url) - and the in the xml feed there is a '&' it will make an error - how can i get this to work?
if (file_exists($url)) {
$tips = simplexml_load_file($url);
} else {
exit('Failed to open stream');
}
foreach ($tips->channel->item as $entry) {
$title=htmlentities($entry->title);
}
}
This is how i wrote the code, i am getting the rss feed from another site so i cant edit the character
I'm getting JSON arrays with file_get_contents, but if the page doesn't exist I want to give an message: Player not found. But now I get this error:
Warning: file_get_contents(urlwithan404errorhere)
[function.file-get-contents]: failed to open stream: HTTP request
failed! HTTP/1.1 404 Not Found in -------
I don't get this error when the api gives me an correct JSON array.
This is the code is use:
$gegevens = file_get_contents('');
$array_2 = json_decode($gegevens, TRUE);
$summonerid = $array_2[$naam]["id"];
Your code could become like this:
$gegevens = #file_get_contents('');
if ($gegevens !== FALSE) {
$array_2 = json_decode($gegevens, TRUE);
$summonerid = $array_2[$naam]["id"];
} else {
$summonerid = 0;
}
Explanations:
* the # in front of file_get_contents is to stop showing php error in case the request failed.
* if the $summonerid equals to 0, then you have no player found
Try something like this:
if (($gegevens = #file_get_contents('')) === false) {
printf("<h1>Player not found!</h1>\n");
return;
}
// ... continue here ...
The # will suppress any error message if file_get_contents()fails. It will return falsein this case (use === for comparison to avoid confusion with empty files), which you can use for failure detection.
I am trying to code a file shredder in PHP, and I get these errors:
Warning: fopen(calculate): failed to open stream: Permission denied in C:\XAMPP\htdocs\shred.php on line 9
Warning: fwrite() expects parameter 1 to be resource, string given in C:\XAMPP\htdocs\shred.php on line 11
<?PHP
$files = glob("*");
foreach ($files as $files) {
$size = filesize($files);
$bytes = "1";
$writes = "1";
while ($writes <= "3") {
$data = fopen($files, "w");
while ($bytes <= $size) {
fwrite($files, "0");
$bytes = $bytes + 1;
}
fclose($data);
$writes = $writes + 1;
}
// unlink($files);
}
?>
I have no idea what to do at this point. The files aren't read only.
You should use $data as the first parameter of fwrite.
Make sure you have the appropriate permissions because that is the common problem in using
fopen()
you can see the the same errors with this one.
So I have a PHP program that a line from a text file. Then it uses that line of text it read to point to another text file
$posts = "posts/posts.txt";
$postsLines = file($posts);
$fetchingPost = TRUE;
$postNumber = 0;
$postPointer;
$postPointerString;
$postLines;
$postTag;
$postTitle;
$postContent;
$endCondition = "end";
while ($fetchingPost == TRUE) {
$endOfFile = strcmp($postsLines[$postNumber], $endCondition);
if ($endOfFile == 0) {
$fetchingPost = FALSE;
}
if ($endOfFile <> 0) {
$postPointer[$postNumber] = $postsLines[$postNumber];
$postLines = file($postPointer[$postNumber]);
$postNumber = $postNumber + 1;
}
}
And I get this errors when I run it, I am using the WAMP server
Warning: file(posts/leapMotionSandbox.txt ): failed to open stream: Invalid argument in C:\wamp\www\noahhuppert\Paralax v2\index.php on line 45
Warning: file(posts/topDownShooter.txt ): failed to open stream: Invalid argument in C:\wamp\www\noahhuppert\Paralax v2\index.php on line 45
Please help
The elements of the array returned by file() have a newline at the end of each line. This is not a valid filename character on Windows (it's valid on Unix, although it would be perverse to include newlines in a filename).
From the documentation:
Each line in the resulting array will include the line ending, unless FILE_IGNORE_NEW_LINES is used, so you still need to use rtrim() if you do not want the line ending present.
Your loop can also be simplified greatly. There's no need for the $fetchingPost or $endOfFile variables, just test for the end in the while() condition.
while (($line = rtrim($postsLines[$postNumber]) != $endCondition) {
$postPointer[$postNumber] = $line;
$postLines = file($line);
$postNumber++;
}
Alternatively, you can do:
$postsLines = file($posts, FILE_IGNORE_NEW_LINES);
i load file from server:
$url = 'http://www.sample.com/test.xml';
$xml = simplexml_load_file($url);
And if servers is close i get error:
Warning: simplexml_load_file() [function.simplexml-load-file]: php_network_getaddresses: getaddrinfo failed:...
Warning: simplexml_load_file() [function.simplexml-load-file]: I/O warning : failed to load external entity
how to check if the file is reached?
From the manual page for simplexml_load_file
Returns an object of class SimpleXMLElement with properties containing the data held within the XML document. On errors, it will return FALSE.
which means you can do
$url = 'http://www.sample.com/test.xml';
$xml = simplexml_load_file($url);
// check what was returned
if(FALSE === $xml) {
echo 'could not open file';
}