云服务器 99 / 年,新老同享(可以99/年续费),开发者力荐特惠渠道,新客户在享受9折
阿里云推广

php如何调用 basic auth接口 验证方式

  • 内容
  • 评论
  • 相关
//国航发送给第三方  basic auth 验证方式
    public  function send_to_ameco()
    {

        $out_trade_no = "2023081416181400037";
        $flag = 1;
        $payinfo = Db::table("client_order_pay_record")->where("  out_trade_no='$out_trade_no' ")->find();
        $mid = $payinfo['mid'];
        //获取员工信息
        $member_info = Db::table('client_merchant_member')->where('id', $mid)->find();
        //取出订单表
        $order_keynum = $payinfo['order_keynum'];
        $orderinfo = Db::table("client_order_info")->where("  keynum='$order_keynum' ")->find();
        $send_data = array();
        $send_data['supplier_code'] = $member_info['remark1'];
        $send_data['staff_no'] = $member_info["out_code"];
        $send_data['order_time'] = date("Y-m-d H:i:s");
        $send_data['order_no'] = $orderinfo["order_sn"];
        //$send_data['order_name'] = $orderinfo['goodsinfo'];
        $send_data['order_name'] =  mb_substr_with_ellipsis($orderinfo['goodsinfo'], 0, 30, "等");

        $send_data['points'] = (float)$payinfo["member_money"];
        $send_data['price'] = (string)$orderinfo["order_total_money"];
        $item = array();
        $orderdetail_list = Db::table("client_order_orderdetail")->where("  order_keynum='$order_keynum' ")->select();
        //print_r($orderdetail_list);die;
        foreach ($orderdetail_list as $k => $v) {
            $row = array();
            $row['mat_code'] = $v["goodssku"];
            $row['mat_name'] = $v["goodsname"];
            $row['qty'] = (string)$v["number"];
            $row['unit_price'] = (string)$v["goodsintegral"];
            $row['total_price'] = (string)bcmul($v["goodsintegral"], $v["number"], 2);
            $row['type'] = "百货";
            $item[] = $row;
        }
        $send_data['items'] = $item;

        print_r($send_data);

        //接口地址
        $api_url = "https://www.host.com:80/ESB/OPEN/API/OUS009/SYS0277/SVC20300047";
        $json_data = json_encode($send_data, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES);

        $username = 'xxxxx';
        $password = 'yyyyyy';

        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $api_url);

        curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_ANY);  //basic auth
        curl_setopt($ch, CURLOPT_USERPWD, "$username:$password"); //basic auth

        curl_setopt($ch, CURLOPT_HEADER, true);
        curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
        curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (compatible; MSIE 5.01; Windows NT 5.0)');
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
        curl_setopt($ch, CURLOPT_AUTOREFERER, 1);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $json_data);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt(
            $ch,
            CURLOPT_HTTPHEADER,
            array(
                'Content-Type: application/json',
                'Content-Length: ' . strlen($json_data)
            )
        );
        $tmpInfo = curl_exec($ch);

        $errorno = curl_errno($ch);
        //curl错误或者服务端的返回头不是200都是错误
        if ($errorno  || curl_getinfo($ch, CURLINFO_HTTP_CODE) != '200') {
            //请求发送失败
            $flag = 0;
            $error_msg = "curl错误:$errorno" . ",接口返回状态码" . curl_getinfo($ch, CURLINFO_HTTP_CODE);
        } else {

            list($header, $body) = explode("\r\n\r\n",  $tmpInfo, 2);
            $rt_json = $body;
            $rt_arr = json_decode($rt_json, 1);
            if ($rt_arr["success"] != 'true') {
                //业务失败
                $flag = 0;
                $error_msg = $rt_arr["message"];
            }
        }

        echo $error_msg;
        die;
    }

本文标签:

版权声明:若无特殊注明,本文皆为《菜鸟站长》原创,转载请保留文章出处。

本文链接:php如何调用 basic auth接口 验证方式 - https://wziyi.net/?post=411

发表评论

电子邮件地址不会被公开。 必填项已用*标注