0

为什么我会收到此错误:

未定义的变量 key_2captcha

我运行此代码将验证码传递给 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 中运行此代码。

4

3 回答 3

1

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

如果要将变量用于泛型函数,则必须将此变量作为函数签名中的参数传递。不要将变量用作全局变量,因为这是一种不好的做法,您必须制作泛型函数,因此您必须使用泛型参数。

试试这个代码:

<?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);
?>
于 2015-04-01T08:01:13.320 回答
-1

使用下面的代码将 $key_2captcha 与全局一起使用。在这两个功能。在 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;
}
于 2015-04-01T07:58:05.367 回答
-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

于 2015-04-01T08:00:58.337 回答