美文网首页
各种get、post请求

各种get、post请求

作者: 苍老师的眼泪 | 来源:发表于2020-09-15 17:17 被阅读0次

(1)curl

1. get(注意当uri上面包含&等符号时,用双引号将整个uri包起来)
curl "localhost/api/color?name=Edison&age=12" -X GET

2. post
curl localhost/api/color -X POST -d "name=Edison&age=25"

3. post with json(注意请求体必须用反斜杠转义)
curl localhost/api/color -X POST -H "Content-Type: application/json"  -d "{\"name\": \"Edison\", \"age\":24}"

(2)xhr

1. get
        var url = 'http://localhost/api/test?'
        var params = "name=Edison&age=25";

        var xhr = new XMLHttpRequest();

        xhr.onload = function () {
            if (xhr.status == 200) 
                console.log(JSON.parse(xhr.responseText))
        }

        xhr.open('GET', url + params);
        xhr.send();
2. post
        var url = 'http://localhost/api/test?abc=cdf'
        var params = "name=Edison&age=25";

        var xhr = new XMLHttpRequest();

        xhr.onload = function () {
            if (xhr.status == 200) {
                console.log(JSON.parse(xhr.responseText))
            }
        }

        xhr.open('POST', url);
        xhr.setRequestHeader("Content-type","application/x-www-form-urlencoded");
        xhr.setRequestHeader("X-Requested-With", "XMLHttpRequest");
        xhr.send(params);
3. post with json
        var url = 'http://localhost/api/test?abc=cdf'
        var params = "name=Edison&age=25";

        var xhr = new XMLHttpRequest();

        xhr.onload = function () {
            if (xhr.status == 200) {
                console.log(JSON.parse(xhr.responseText))
            }
        }

        xhr.open('POST', url);
        xhr.setRequestHeader("Content-type","application/json");

        var profile = {
            name: "Edison",
            age: 24,
            gender: "male"
        }
        xhr.send(JSON.stringify(profile));

(3) axios (axios v0.20.0)

1. get (headers必须是自定义的头;参数:axios.get(url[, config]))
        axios.get('http://localhost/api/test', {
            headers: {
                
                'X-Requested-With': 'XMLHttpRequest',
                'My-Header': 'qwer',
            },
            params: {
                ID: 12345,
                name: "Edison",
                age: 25
            },
        })
            .then(function (response) {
                console.log(response);
            })
            .catch(function (error) {
                console.log(error);
            })
2. post(参数:axios.post(url[, data[, config]]) )
        axios.post('http://localhost/api/test', {
                params: {
                    ID: 12345,
                    name: "Edison",
                    age: 25
                },
            },
            {
                headers: {
                    'X-Requested-With': 'XMLHttpRequest0',
                    'Content-Type': 'qwer',
                },
            })
            .then(function (response) {
                console.log(response);
            })
            .catch(function (error) {
                console.log(error);
            })

(4) php (http)

1. get
<?php
$uri = 'http://localhost/api/test?name=Edison&age=20';

$opts = [
    'http' => [
        'method' => "GET",
    ]
];

$context = stream_context_create($opts);

$result = file_get_contents($uri, false, $context);

if ($result !== false)
    print_r(json_decode($result));

2. post
<?php

$uri = 'http://localhost/api/test?name=Edison&age=20';

$data = ['foo' => 'bar', 'bar' => 'baz'];
$data = http_build_query($data);

$opts = [
    'http' => [
        'method' => "POST",
        'header' => "Accept-language: en\r\n" .
            "Cookie: foo=bar\r\n" . 
            "Content-Type: application/x-www-form-urlencoded\r\n" .
            "My-Header: asdf",
        'content' => $data
    ]
];

$context = stream_context_create($opts);

$result = file_get_contents($uri, false, $context);

if ($result !== false)
    print_r(json_decode($result));

(5)php (https)

<?php

        $results = Ocpc::whereIn('id', $request->get('ids'))->get()->toArray();
        
        
 
        
        $cv1 = array(
            'logidUrl' => 'http://ssf1.xayuy.com/?bd-df-190603617&bd_vid=12223909403864348178', // 您的落地页url
            'newType' => 1 
        );
        
        $cv2 = array(
            'logidUrl' => 'http://ssf1.xayuy.com/?bd-tf-190624022&renqun_youhua=2241878&bd_vid=9639005579925129023', // 您的落地页url
            'newType' => 35
        );
        $conversionTypes = [];

        foreach ($results as $result) {
    
            
            $deviceType = 2;
            if ($result['port'] == "Android")
                $deviceType = 0;
            elseif ($result['port'] == "Ios") {
                $deviceType = 1;
            }
            
    
            
            $conversionType = array(
                'logidUrl' => $result['url'], // 您的落地页url
                'newType' => 35,
                'deviceType' => $deviceType,
                // public  $deviceId;
                'isConvert' => $result['valid'],
                'convertTime' => strtotime($result['convert_time']),
                // 'convertValue' => $result['value'],
                'confidence' => $result['confidence']
            );
            
            $conversionTypes[] = $conversionType;
        }

        var_dump($conversionTypes);


        $token = 'BGj1fhEIY53DSGkE8HIovwFjirKsjQH3@8RzOCsOUUVykZ4yCkV0PwyvaZqAKrePn';
        $reqData = array('token' => $token, 'conversionTypes' => $conversionTypes);
        $reqData = json_encode($reqData);

        $ch = curl_init();
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_URL, "https://ocpc.baidu.com/ocpcapi/api/uploadConvertData");
        curl_setopt($ch, CURLOPT_POSTFIELDS, $reqData);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_HTTPHEADER, array(
                'Content-Type: application/json; charset=utf-8',
                'Content-Length: ' . strlen($reqData)
            )
        );

        $response = curl_exec($ch);
        $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
        echo $httpCode;
        if ($httpCode === 200) {
   
            $res = json_decode($response, true);
            $status = $res['header']['status'];
            if ($status == 0) {
                echo "//all success";
                Ocpc::whereIn('id', $request->get('ids'))->update(['status' => 2]);
            } else if ($status == 1) {
                echo "//partly success";
                Ocpc::whereIn('id', $request->get('ids'))->update(['status' => 2]);
            } else if ($status == 2) {
                echo "//all failed";
                Ocpc::whereIn('id', $request->get('ids'))->update(['status' => 2]);
            } else if ($status == 3) {
                echo "//invalid token";
                Ocpc::whereIn('id', $request->get('ids'))->update(['status' => 2]);
            } else if ($status !== 4) {
                echo "//server error";
            }
        }
        
        curl_close($ch);

相关文章

  • 各种get、post请求

    (1)curl (2)xhr (3) axios (axios v0.20.0) (4) php (http) (...

  • iOS请求方法和网络安全

    GET和POST请求 GET和POST请求简介 GET请求模拟登陆 POST请求模拟登陆 GET和POST的对比 ...

  • iOS请求方法和网络安全

    GET和POST请求GET和POST请求简介GET请求模拟登陆POST请求模拟登陆GET和POST的对比保存用户信...

  • java发送http请求

    restTemplate get请求 post请求 apache.http.client get请求 post请求...

  • Get和Post的区别

    Get请求和Post请求区别如下: Post请求比Get请求更安全,get请求直接将参数放置在URL中,post请...

  • Okhttp3

    简介 配置 请求思路 get请求思路 post请求思路 get,post 同步和异步请求 异步请求(get) 同步...

  • Requests库相关操作

    用法 GET各种请求方式 get请求 解析json 获取二进制数据 添加headers(浏览器伪装) POST各种...

  • gf框架 ghttp使用

    案例中包含以下内容 get请求 get请求携带参数 post请求携带参数 post请求发送xml数据 post请求...

  • HttpUtil工具

    HttpUtil工具,http get post请求,https get post请求,ajax response...

  • ajax 请求的时候 get 和 post 方式的区别?

    get和post的区别 get请求不安全,post安全 get请求数据有限制,post无限制 get请求参数会在u...

网友评论

      本文标题:各种get、post请求

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