fread() is not a valid stream resource

I get a error when i run the code below. It isnt a error but the code dosnt find the search criteria when i know if there. It usualy says: fread(): Is not a valid stream resource in cleartorrent.php line 1. Can any help so it works everytime?

[php]<?php
// Include the JSON parser.
require_once(‘JSON.php’);

// Set UserName & Password.
$ini_array[‘username’] = ‘username’;
$ini_array[‘password’] = ‘password’;
$ini_array[‘port’] = ‘port’;

// Grab authentication token.
$url = ‘http://’ . $ini_array[‘username’] . ‘:’ . $ini_array[‘password’] . ‘@localhost:’ . $ini_array[‘port’] . ‘/gui/token.html’;
$handle = fopen($url, ‘rb’);
$token = ‘’;
while (!strpos($token,’’)) {$token .= fread($handle, 1);}
fclose($handle);
$token = str_replace("

","",$token);
$token = str_replace(’
’,’’,$token);

// Retrieve JSON results.
$url = ‘http://’ . $ini_array[‘username’] . ‘:’ . $ini_array[‘password’] . ‘@localhost:’ . $ini_array[‘port’] . ‘/gui/?list=1&token=’ . $token;
$handle = fopen($url, ‘rb’);
$body = ‘’;
while (!strpos($body,’}’)) {
$body .= fread($handle, 1);
}
fclose($handle);

// Decode JSON results.
$json = new Services_JSON();
$json = $json->decode($body);

// Find completed downloads.
$completedTorrent = ‘’;
foreach($json->torrents as $searchresult){
if ($searchresult[4] >= 1000){
$completedTorrent = $searchresult[0];
$completedTorrentName = $searchresult[2];
}
}

// Remove completed torrent.
if (!empty($completedTorrent)){
$url = ‘http://’ . $ini_array[‘username’] . ‘:’ . $ini_array[‘password’] . ‘@localhost:’ . $ini_array[‘port’] . ‘/gui/?action=remove&token=’ . $token . ‘&hash=’ . $completedTorrent;
echo 'Removed ’ . $completedTorrentName;
$handle = fopen($url, ‘rb’);
}
else {echo ‘No completed torrents found.’;}
?>
[/php]

The site it searches on looks like this:

{“build”:25113,“label”: [
]
,“torrents”: [

[“8B611A96922B922CD5CC3AD29672CFFA0DB9CE00”,201,“Revenge.S01E01.HDTV.XviD-LOL.avi”,366673664,1000,366673664,93830912,255,0,0,-1,"",0,216,0,278,65536,-1,0],
[“821DEC2C9F134E2507620C2338A604CCCA778FA0”,201,“Revenge.S01E02.HDTV.XviD-LOL.avi”,366917002,1000,366917002,2670592,7,5926,16,92424,"",1,164,0,191,103734,-1,0],
[“968A7743A136EDAEE8C8074C7F646EBBAAFF5534”,201,“Revenge.S01E03.HDTV.XviD-LOL.avi”,366999674,1000,366999674,4227072,11,4949,5,110380,"",2,173,0,173,95682,-1,0],
[“F62F4C1AD05936628A262AAFBF8E3FFEDF78E2E3”,201,“Revenge.S01E04.HDTV.XviD-LOL.avi”,366644756,1000,366644756,3162112,8,6477,4,84422,"",1,169,0,167,106355,-1,0],
[“767BFDD0C85A654DA8A3E0FAE0D6A1EE6118617D”,201,“Revenge.S01E05.HDTV.XviD-LOL.avi”,366763624,1000,366763624,2408448,6,5612,7,97601,"",2,232,0,253,104483,-1,0]]
,“torrentc”: “1791355444”
,“rssfeeds”: []
,“rssfilters”: []
}

The code was written http://www.aeonity.com/xuanming/automatically-remove-torrents-after-download

From the sound of the error, it seems that the URL you’re passing to fread isn’t valid for whatever reason. Just to debug it, echo out your concatenated $url variable to see what it looks like, and confirm that it’s valid.

If that checks out, keep in mind that on some server set ups, the allow_url_fopen PHP ini directive is set to false, which means that you can’t pass URL’s to file reading functions such as fread(). If you have control over your own ini settings make sure allow_url_fopen is set to true…if you don’t have that control and can’t confirm that it’s enabled, you may be SOL.

Sponsor our Newsletter | Privacy Policy | Terms of Service