Php 为什么可以';这些函数没有看到我的变量吗?

Php 为什么可以';这些函数没有看到我的变量吗?,php,2captcha,Php,2captcha,为什么会出现此错误: 未定义的变量键\u 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,

为什么会出现此错误:

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

使用下面的代码使用$key\u 2captcha和全局。在这两种功能中


将下面的代码与
$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;
    }
    ?>


Ref

我认为您存在可变范围解决问题

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

请尝试以下代码:

<?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\u 2captcha
参数?是的,我会编辑我的答案!每个函数都需要使用它的参数。当然,值得指出的是,
global
变量几乎总是错误的解决方案。跟踪您可以使用的内容,确保您不会破坏全球国家,这可能很复杂。
<?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);
?>