:w00t: Posted September 21, 2011 Share Posted September 21, 2011 Hello i am new to this forum and coding. I am trying to fix a auto torrent downloading script which is php and curl. I have been stucked with Undefined offset error. Your help will be very appretiated, thanks. Starting download: Mp3-data1 Notice: Undefined variable: temp in /var/www/html/torrents/bgrahul2.php on line 338 Array ( [0] => HTTP/1.1 200 OK Server: Transmission Content-Type: application/json; charset=UTF-8 Date: Wed, 21 Sep 2011 19:58:44 GMT Content-Length: 49 {"arguments":{"torrents":[]},"result":"success"} [1] => ) Marked those lines to bold $result = callTransmission(array("filename" => "/var/www/html/donofwarez/xxx/torrents/$torrentfile", "download-dir" => "/home/Downloads/$title/"), "torrent-add", $arr[1]); callTransmission(array("seedRatioLimit" => 50, "seedRatioMode" => 1), "torrent-set", $arr[1]); /* echo "<pre>"; print_r($result); echo "</pre>"; */ if (preg_match('/"percentDone":[0-9.]+/i', $result[0], $matches)) $temp = str_replace('"id":', "", $matches[0]); // $temp="hlDr1wqu2ONtM3McVGHoVzfrkp2UgUcGwDt66IuxAe0LBkru"; $id[0] = $temp; //***OMG! We need the ids as integers! *** foreach ($id as $key => $value) { $id[$key] = (int) $value; } Quote Link to comment https://forums.phpfreaks.com/topic/247621-php-notice-undefined-variable-error/ Share on other sites More sharing options...
btherl Posted September 21, 2011 Share Posted September 21, 2011 The right fix for that depends on how you want the program to behave. One possible fix is: $temp = 0; # Default value if it can't be found in $result[0] if (preg_match('/"percentDone":[0-9.]+/i', $result[0], $matches)) $temp = str_replace('"id":', "", $matches[0]); // $temp="hlDr1wqu2ONtM3McVGHoVzfrkp2UgUcGwDt66IuxAe0LBkru"; $id[0] = $temp; But instead you might want to skip the later code if $temp wasn't able to be set. Quote Link to comment https://forums.phpfreaks.com/topic/247621-php-notice-undefined-variable-error/#findComment-1271593 Share on other sites More sharing options...
:w00t: Posted September 21, 2011 Author Share Posted September 21, 2011 Here is my full code. I am quite new to php. please, bear with me Marked those lines to bold $result = callTransmission(array("filename" => "/var/www/html/torrents/$torrentfile", "download-dir" => "/home/Downloads/$title/"), "torrent-add", $arr[1]); callTransmission(array("seedRatioLimit" => 50, "seedRatioMode" => 1), "torrent-set", $arr[1]); /* echo "<pre>"; print_r($result); echo "</pre>"; */ if (preg_match('/"percentDone":[0-9.]+/i', $result[0], $matches)) $temp = str_replace('"id":', "", $matches[0]); // $temp="hlDr1wqu2ONtM3McVGHoVzfrkp2UgUcGwDt66IuxAe0LBkru"; $id[0] = $temp; //***OMG! We need the ids as integers! *** foreach ($id as $key => $value) { $id[$key] = (int) $value; } //***Check whether torrents are complete*** $check = true; while ($check) { $c = 0; foreach ($id as $value) { $result = callTransmission(array("fields" => array("id", "name", "percentDone"), "ids" => array($value)), "torrent-get", $arr[1]); print_r($result); exit; preg_match('/"percentDone":[0-9.]+/i', $result[0], $matches); $adii = substr($matches[0], 14); $anuj = (int) ($adii * 100); update_progress($anuj); //CALL FUNCTION $anujstatus = "Download In Progress"; $downcomplete = "Download Finished"; anujstatus($anujstatus); if ($anuj=='100') { aditya(100); anujstatus($downcomplete); echo "<br><br><br><br> Download complete"; break 2; } if ($matches[0] == '"percentDone":1.0000') $c++; } if ($c==count($id)) { $check = false; break 2; } sleep(5); } Quote Link to comment https://forums.phpfreaks.com/topic/247621-php-notice-undefined-variable-error/#findComment-1271598 Share on other sites More sharing options...
:w00t: Posted September 21, 2011 Author Share Posted September 21, 2011 The right fix for that depends on how you want the program to behave. One possible fix is: $temp = 0; # Default value if it can't be found in $result[0] if (preg_match('/"percentDone":[0-9.]+/i', $result[0], $matches)) $temp = str_replace('"id":', "", $matches[0]); // $temp="hlDr1wqu2ONtM3McVGHoVzfrkp2UgUcGwDt66IuxAe0LBkru"; $id[0] = $temp; But instead you might want to skip the later code if $temp wasn't able to be set. i tried commenting $temp, but it came up with new fatal error. Then i tried with your above code, error disappeared but script did'nt executed. I have posted my full code. thanks Quote Link to comment https://forums.phpfreaks.com/topic/247621-php-notice-undefined-variable-error/#findComment-1271599 Share on other sites More sharing options...
:w00t: Posted September 21, 2011 Author Share Posted September 21, 2011 $id = array(); Quote Link to comment https://forums.phpfreaks.com/topic/247621-php-notice-undefined-variable-error/#findComment-1271600 Share on other sites More sharing options...
btherl Posted September 21, 2011 Share Posted September 21, 2011 Ok, let's trace back the cause of the error: if (preg_match('/"percentDone":[0-9.]+/i', $result[0], $matches)) $temp was not set because this preg_match() did not match. It didn't match because it didn't find "percentDone" followed by a number in $result[0]. $result[0] came from callTransmission(). The difficulty is that you don't know at which point things failed. The first step in finding out where it failed is to uncomment this code: /* echo "<pre>"; print_r($result); echo "</pre>"; */ So you can see what is in $result, and see if it really does have the "percentDone" that preg_match() is looking for. You can uncomment that code by removing the "/*" and the "*/" that surround it. If you're not sure what the output means, please post it here so we can take a look. Quote Link to comment https://forums.phpfreaks.com/topic/247621-php-notice-undefined-variable-error/#findComment-1271604 Share on other sites More sharing options...
:w00t: Posted September 22, 2011 Author Share Posted September 22, 2011 Ok, let's trace back the cause of the error: if (preg_match('/"percentDone":[0-9.]+/i', $result[0], $matches)) $temp was not set because this preg_match() did not match. It didn't match because it didn't find "percentDone" followed by a number in $result[0]. $result[0] came from callTransmission(). The difficulty is that you don't know at which point things failed. The first step in finding out where it failed is to uncomment this code: /* echo "<pre>"; print_r($result); echo "</pre>"; */ So you can see what is in $result, and see if it really does have the "percentDone" that preg_match() is looking for. You can uncomment that code by removing the "/*" and the "*/" that surround it. If you're not sure what the output means, please post it here so we can take a look. I did what you said. Uncommented "/*" and the "*/". Array ( [0] => HTTP/1.1 200 OK Server: Transmission Content-Type: application/json; charset=UTF-8 Date: Thu, 22 Sep 2011 09:11:07 GMT Content-Length: 46 {"arguments":{},"result":"duplicate torrent"} [1] => ) Notice: Undefined variable: temp in /var/www/html/donofwarez/xxx/bgrahul2.php on line 338 Array ( [0] => HTTP/1.1 200 OK Server: Transmission Content-Type: application/json; charset=UTF-8 Date: Thu, 22 Sep 2011 09:11:07 GMT Content-Length: 49 {"arguments":{"torrents":[]},"result":"success"} [1] => ) Quote Link to comment https://forums.phpfreaks.com/topic/247621-php-notice-undefined-variable-error/#findComment-1271666 Share on other sites More sharing options...
btherl Posted September 22, 2011 Share Posted September 22, 2011 Ok that tells you the problem - your script expects a response saying "percentDone:53" or something similar, but instead the response is "duplicate torrent". You need to check if the response is "duplicate torrent", and if it is, do something appropriate. Quote Link to comment https://forums.phpfreaks.com/topic/247621-php-notice-undefined-variable-error/#findComment-1271876 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.