全局范围在PHP函数中不起作用

时间:2016-09-06 08:14:36

标签: php

我在配置文件中声明了配置变量,并希望从同一页面上的函数中访问这些变量。此函数将对服务进行SOAP调用以获取某些数据。

我将此配置文件包含在各种页面上,但是当我访问该函数时,即使我声明了全局分数,变量也是空的。我的(简化)代码如下所示:

的config.php

 // Set environment
 $environment = 'dev';

 switch($environment){
      case 'dev':
           $username = 'dev';
           $client = new SoapClient('http://dev.example.com/wsdl?wsdl', array('trace' => 1, 'exceptions' => 0));
      break;
      case 'prod';
           $username = 'prod';
           $client = new SoapClient('http://prod.example.com/wsdl?wsdl', array('trace' => 1, 'exceptions' => 0));
      break;
      default:
           $username = 'prod';
           $client = new SoapClient('http://prod.example.com/wsdl?wsdl', array('trace' => 1, 'exceptions' => 0));
 }

 function fetch_data($request_id){
      global $username;

      $xml_post_string = '
           <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ver="https://example.com">
                <soapenv:Header/>
                <soapenv:Body>
                     <ver:options>
                          <ver:rid>'.$request_id.'</ver:rid>
                          <ver:uid>'.$username.'</ver:uid>
                     </ver:options>
                </soapenv:Body>
           </soapenv:Envelope>
      ';

      echo '<pre>';
      echo 'Var: '.$username; // = empty
      echo '</pre>';

      // Curl headers
      $headers = array(
           "Content-type: text/xml;charset=\"utf-8\"",
           "Accept: text/xml",
           "Cache-Control: no-cache",
           "Pragma: no-cache",
           "SOAPAction: "https://example.com",
           "Content-length: ".strlen($xml_post_string),
      );

      // Curl options
      $ch = curl_init();
      curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
      curl_setopt($ch, CURLOPT_URL, 'https://example.com/call?wsdl');
      curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
      curl_setopt($ch, CURLOPT_TIMEOUT, 10);
      curl_setopt($ch, CURLOPT_POST, true);
      curl_setopt($ch, CURLOPT_POSTFIELDS, $xml_post_string); // the SOAP request
      curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);

      // Execute Curl call
      $response = curl_exec($ch); // = credential error
 }

在page.php上我包括&amp;像这样调用函数:

 require_once('config.php');

 [..]

 $response = fetch_data('1');

当我在XML字符串中输入用户名时,我得到了我想要的响应,但是当我使用变量时(如示例中所示),我得到$username的空值。

但为什么?

修改:将exit;更改为break;并添加了进程&amp;输出示例

**编辑2:** [..]

之间的代码
 require_once('config.php');

 global $current_user, $wp_query;

 $user                                                       = wp_get_current_user();

 $user_meta                                                  = get_user_meta($user->data->ID, 'exID');
 $user_member_number                                         = trim($user_meta[0]);

 // Set locale for date notation
 setlocale(LC_ALL, 'nl_NL');

 $policy_id                                                  = $_GET['polis'];

 $customer_call = array(
      'options' => array(
           'Credentials' => array(
                'UserId'                                     => $username // Normally more credentials like password are required as well
           )
      )
 );
 $customer_response                                          = $client->FetchCustomer($customer_call);

 $member_details                                             = $customer_response->FetchCustomerResult->Result->PlatformCustomer;

 $product_call = array(
      'options' => array(
           'Credentials' => array(
                'UserId'                                     => $username // Normally more credentials like password are required as well
           )
      ),
 );
 $product_response                                           = $client->FetchProduct($product_call);


 if($product_response->FetchProductResult->Succeeded){
      // Extract results
      $product_info                                          = $product_response->FetchProductResult->Result;

      // Differentiate product response, depending on the amount of rows returned
      if($product_info->SummarizedProductInfo >= 2){
           $products                                         = $product_response->FetchProductResult->Result->Summary;
      }else{
           $products                                         = $product_response->FetchProductResult->Result;
      }

      // Policy numbers for given user (for authentication purpose)
      $member_active_policies                                = array();

      // Iterate through policies and write policy number to array
      foreach($products as $product){ 
           array_push($member_active_policies, $product->PolicyNumber);
      }

      // Check if requested polis belongs to user
      if(in_array($policy_id, $member_active_policies)){
           // Iterate through products to find the requested
           foreach($products as $product){
                if($product->PolicyNumber == $policy_id){

                     // Set member variables, all with prefix $member_

                     // Set product variables, all with prefix $product_

                     // Fetch information about other members
                     $all_members                            = fetch_data(1)->GetProductInfo;

1 个答案:

答案 0 :(得分:0)

我在这里可以看到2个问题。

  1. 您的fetch_data函数未返回任何内容。我想它应该返回$xml_post_string

  2. 即使您返回$xml_post_string,如果您的环境是&#34; dev&#34;,当您提供&#34; prod&#34;时,您的代码将会正常工作,它将退出&#34; exit&#34;上的脚本,因此您的脚本将不具有fetch_data功能。