基本授权PHP与服务器相关

时间:2017-03-27 09:01:01

标签: php curl

我有一些与opencart中的curl php中的HTTP_HEADERS相关的问题。下面的代码是来电者。

    $ch = curl_init();

    $url = 'http://aaa.com/index.php?route=common/home/getTotalCustomer';
    $url2 = 'http://bbb.com/index.php?route=common/home/getTotalCustomer';
    $url3 = 'http://ccc.com/index.php?route=common/home/getTotalCustomer';

    $header = array('Authorization:Basic ' . base64_encode('user:password'));

    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_FAILONERROR, true);
    curl_setopt($ch, CURLOPT_HEADER, false);

    $results = curl_exec($ch);

    echo '<pre>';
    print_r(json_decode($results, true));
    echo '</pre>';

收件人代码如下:

public function getTotalCustomer(){
    $json = array();

    $this->load->model('account/customer');

    if(isset($_SERVER['PHP_AUTH_USER']) && isset($_SERVER['PHP_AUTH_PW'])){
        if(($_SERVER['PHP_AUTH_PW'] == 'password') && ($_SERVER['PHP_AUTH_USER'] == 'user')){
            $json['total_customer'] = $this->model_account_customer->getTotalCustomer();
        }
    } else{
        $json['message'] = 'failed';
    }

    $this->response->addHeader('Content-Type: application/json');
    $this->response->setOutput(json_encode($json));
}

我曾在不同服务器的多个域中尝试过。某些服务器可以返回数据但某些服务器无法返回数据。为什么呢?

1 个答案:

答案 0 :(得分:0)

您发送的标题不正确 你通过了 Authorization:Basic <username:password>
它应该是
Authorization: Basic <username:password>
注意空间。