(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);
网友评论