Jump to content

[PHP] Notice: Undefined variable Error


:w00t:

Recommended Posts

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;

                }

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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);

                } 

Link to comment
Share on other sites

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 :)

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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] => )

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.