簡體   English   中英

這些函數為什么看不到我的變量?

[英]Why can't these functions see my variable?

為什么會出現此錯誤:

未定義的變量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中運行此代碼。

我認為您有一個可變范圍解析問題。

如果要將變量用於通用函數,則必須在函數簽名中將此變量作為參數傳遞。 不要將變量用作全局變量,因為這是一種不好的做法,您必須創建通用函數,因此必須使用通用參數。

試試這個代碼:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM