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

时间:2015-04-01 07:49:26

标签: php

为什么我会收到此错误:

  

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

3 个答案:

答案 0 :(得分: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);
?>

答案 1 :(得分:-1)

使用下面的代码使用$ key_2captcha和global。在这两个功能。 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;
}

答案 2 :(得分:-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