2015-04-01 98 views
0

爲什麼我得到這個錯誤:爲什麼這些函數不能看到我的變量?

Undefined variable key_2captcha

我運行此代碼到一個CAPTCHA傳遞給2captcha服務器:

<?php 
$id_Captcha=0; 
$key_2captcha="key2captcha"; 
function send_captcha($base_file){ 

    $ch = curl_init("http://2captcha.com/in.php"); 
    curl_setopt($ch, CURLOPT_POSTFIELDS, 
       array('method'=>"base64", 
        'key'=>$key_2captcha, 
        'numeric'=>1, 
        'max_len'=>1, 
        'body'=>$base_file, 
        'submit'=>'download and get the ID')); 


    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 


    $postResult = curl_exec($ch); 


    curl_close($ch); 

    return $postResult; 
} 

function getSolveCaptcha($id_captcha){ 
    $c = curl_init("http://2captcha.com/res.php?key=".$key_2captcha."&action=get&id=".$id_captcha); 
    curl_setopt($c, CURLOPT_RETURNTRANSFER, 1); 
    $postResult = curl_exec($c); 
    curl_close($c); 
    return $postResult; 
} 
?> 

我運行XAMPP這個代碼。

回答

-1

使用下面的代碼使用$ key_2captcha與全局。在這兩個功能。 read variable scope in PHP

function getSolveCaptcha($id_captcha){ 
    global $key_2captcha; 

    $c = curl_init("http://2captcha.com/res.php?key=".$key_2captcha."&action=get&id=".$id_captcha); 
    curl_setopt($c, CURLOPT_RETURNTRANSFER, 1); 
    $postResult = curl_exec($c); 
    curl_close($c); 
    return $postResult; 
} 
+0

值得指出的是,當然,前提是'global'變量幾乎都是錯誤的解決方案。跟蹤你可以使用什麼,並確保你不會破壞全球狀態,可能會很複雜。 – halfer 2015-04-01 16:00:55

-1

使用下面的代碼與$GLOBALS - 參考在全球範圍內

<?php 
    $id_Captcha=0; 
    $key_2captcha="key2captcha"; 
    function send_captcha($base_file){ 

     $ch = curl_init("http://2captcha.com/in.php"); 
     curl_setopt($ch, CURLOPT_POSTFIELDS, 
        array('method'=>"base64", 
         'key'=>$GLOBALS['key_2captcha'], 
         'numeric'=>1, 
         'max_len'=>1, 
         'body'=>$base_file, 
         'submit'=>'download and get the ID')); 


     curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 


     $postResult = curl_exec($ch); 


     curl_close($ch); 

     return $postResult; 
    } 

    function getSolveCaptcha($id_captcha){ 
     $c = curl_init("http://2captcha.com/res.php?key=".$GLOBALS['key_2captcha']."&action=get&id=".$id_captcha); 
     curl_setopt($c, CURLOPT_RETURNTRANSFER, 1); 
     $postResult = curl_exec($c); 
     curl_close($c); 
     return $postResult; 
    } 
    ?> 

參考PHP.net

1

我認爲你有一個variabile的範圍內解決問題,提供所有變量。

如果要將變量用於通用函數,則必須在函數的簽名中將此變量作爲參數傳遞。 不能將變量用作全局變量,因爲這是一種不好的做法,您必須製作泛型函數,以便您必須使用泛型參數。

試試這個代碼:

<?php 
$id_Captcha=0; 
$key_2captcha="key2captcha"; 
function send_captcha($base_file, $key_2captcha){ 

    $ch = curl_init("http://2captcha.com/in.php"); 
    curl_setopt($ch, CURLOPT_POSTFIELDS, 
       array('method'=>"base64", 
        'key'=>$key_2captcha, 
        'numeric'=>1, 
        'max_len'=>1, 
        'body'=>$base_file, 
        'submit'=>'download and get the ID')); 


    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 


    $postResult = curl_exec($ch); 


    curl_close($ch); 

    return $postResult; 
} 

function getSolveCaptcha($id_captcha, $key_2captcha){ 
    $c = curl_init("http://2captcha.com/res.php?key=".$key_2captcha."&action=get&id=".$id_captcha); 
    curl_setopt($c, CURLOPT_RETURNTRANSFER, 1); 
    $postResult = curl_exec($c); 
    curl_close($c); 
    return $postResult; 
} 

//Call Example 
send_captcha($base_file, $key_2captcha); 
?> 
+0

我希望你也需要'$ key_2captcha'參數作爲第二個函數呢? – halfer 2015-04-01 09:26:51

+0

是的,我會編輯我的答案!您需要使用它的每個功能的參數。 – 2015-04-01 15:51:55

相關問題