2015-04-01 99 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

相关问题