为什么这些函数看不到我的变量?



为什么会出现此错误:

未定义的变量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中运行此代码。

我认为您有一个可变范围解析问题。

如果要将该变量用于泛型函数,则必须将该变量作为函数签名中的参数传递。不将变量用作全局变量,因为这是一种糟糕的做法,您必须生成泛型函数,所以必须使用泛型参数。

试试这个代码:

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

使用下面的代码,将$key_2captcha与global一起使用。在两种功能中。读取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;
}

将以下代码与$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

相关内容

  • 没有找到相关文章

最新更新