美文网首页
PHP Curl请求的形式总结

PHP Curl请求的形式总结

作者: Medicine_8d60 | 来源:发表于2020-03-13 17:39 被阅读0次

Curl GET方法

        $header = array("Content-Type: text/html; charset=UTF-8");
        $ch = curl_init("http://132.232.129.53:8082/selectTrack.htm?documentCode=".$trackNumber);
        curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_TIMEOUT, 30);
        $data = curl_exec($ch);
                curl_close($ch);
        $info =json_decode($data,true);
        // var_dump($info[0]['data']);
        return $info[0]['data'];

Curl Post Json数据方法

    $params = array(
        "referenceCode"=> "200",
            "accessInfo" => array(
                        "serviceKey"=> "aad92e0d0c524d83854b56c7b6d11173",
                        "accessChannel"=> "WEB"
             )
    );
    // return json_decode(json_encode($params), true);
        $arrParams = json_encode($params);
        $header = array("Content-type: application/json");
        $ch = curl_init('http://52.76.171.3:8080/api/Order/submitOrder_new');
        curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_TIMEOUT, 30);
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $arrParams );
        $data = curl_exec($ch);
        curl_close($ch);

Curl POST xml 并接收xml数据方法

        $xmlData = '<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:tel="http://telcel.myy.io/">
                    <soapenv:Header/>
                    <soapenv:Body>
                    <tel:Unsubscribe3>
                    <tel:userId>'.$userID.'</tel:userId>
                    <tel:user>kechuang</tel:user>
                    <tel:pass>1Jj31Dll</tel:pass>
                    </tel:Unsubscribe3>
                    </soapenv:Body>
                    </soapenv:Envelope>';
        // echo $xmlData;exit;
        //第一种发送方式,也是推荐的方式:
        $url = 'http://dev.myy.io/ws/pincode.asmx'; 
        $header[] = "Content-type: text/xml";        //定义content-type为xml,注意是数组
        $ch = curl_init ($url);
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $xmlData);
        $response = curl_exec($ch);
        // if(curl_errno($ch)){
            // print curl_error($ch);
        // }
        curl_close($ch);
        preg_match("/<ResponseCode>(.*?)<\/ResponseCode>/i", $response , $out_code);
        $code = $out_code[0];
        // str_replace("<ResponseCode>","",$code);

        preg_match("/<ResponseText>(.*?)<\/ResponseText>/i", $response , $out_text);
        $text = $out_text[0];
        $time = date("Y-m-d h:i:s", time());
        //记录日志:要写入文件的文件名(可以是任意文件名),如果文件不存在,将会创建一个.log.txt位置在项目的根目录下。
        $file  = 'log.txt';
        $content = "code---".$code."text---".$text."time----".$time."\r\n";
        $f  = file_put_contents($file, $content,FILE_APPEND);
                  
                if(preg_match("/<ResponseCode>0<\/ResponseCode>/i", $code)){
                    echo '<script>';
                    echo 'window.location.href="./unsubdone.php";';
                    echo '</script>';
                }else{
                
                    echo '<script>';
                    echo 'layer.msg("La cancelación de la suscripción falló");';
                    echo '</script>';
                }        

相关文章

网友评论

      本文标题:PHP Curl请求的形式总结

      本文链接:https://www.haomeiwen.com/subject/ngqjshtx.html