我是靠谱客的博主 舒适嚓茶,最近开发中收集的这篇文章主要介绍curl 命令行使用 发送json 提交表单 上传文件 php curl post 测试注册,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

* curl POST JSON

curl "http://192.168.4.157:8060/v1/validate/getcode" -X POST -d '{"mobilenumber":"18771099612"}' -H "Content-Type:application/json" -v

* curl POST Form

curl "http://192.168.4.157:8060/v1/validate/getcode" -X POST -d 'mobilenumber=13691150835' -H "Content-Type: application/x-www-form-urlencoded; charset=UTF-8" -v

*curl upload file

curl localhost:8000/api/v1/upimg -F "file=@/Users/fungleo/Downloads/401.png" -H "token: 222" -v

 

* register.php

<?php
error_reporting(E_ALL);
ini_set("display_errors", 1);
include dirname(__FILE__).'/lib/MockValue.php';
function build_query(/* array */$map) {
$a = [];
array_walk($map, function($value, $name) use (&$a) {
$a[] = $name.'='.urlencode($value);
});
return implode('&', $a);
}
function extract_http_body($content, $sep = "rnrn") {
$body = strstr($content, $sep);
// printf("body=[%s]n", $body);
return rtrim( substr($body, strlen($sep)) );
}
$ch = curl_init();
$headers = [
"Content-Type: application/x-www-form-urlencoded; charset=UTF-8",
"Accept: application/json, text/javascript, text/plain */*; q=0.01",
"Origin: http://m.jd100.com",
"Referer: http://m.jd100.com/user/register?figure=0&treaty=on",
"X-Requestd-with: XMLHttpRequest",
"Cache-Control: no-cache",
"Connection: keep-alive",
"User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.87 Safari/537.36",
"X-Forwarded-For: ".MockValue::getIP().", 192.168.247.131, 192.168.247.132",
"X-Real-Ip: 192.168.4.157",
];
$username = MockValue::getName();
// $username = "testtest12345";
$password = MockValue::genPassword(); //
'123456',
$a = [
'username' => $username, // 'putongj4',
'userpwd' => $password,
'userfigure' => 0, // MockValue::pick(['0', '1']),
'grade' => MockValue::getGradeValue(),
'mobile' => "15654466917",
// MockValue::getMobile(),
'verifycode_sms' => '719699',
'gradetype' => '1',
'areainfo' => MockValue::getArea(), // '山西_长治_长子县',
// 'luckdrawcode' => md5(sprintf("%s%djiandan100", $username, time())),
'specialcode' => MockValue::getNumber(6),
'sourcetype' => '2',
'app' => 1,
'sourcechannel' => rand(3, 37),
"mobilebind" => "1",
];
$postdata = build_query($a);
// echo $postdata.PHP_EOL;
array_push($headers, sprintf("Content-Length: %d", strlen($postdata)));
curl_setopt_array($ch, [
// CURLOPT_URL => 'http://172.16.0.224:8060/v1/reg', // request URL
CURLOPT_URL => 'http://192.168.4.157:8060/v1/reg', // request URL
CURLOPT_HEADER => 1,
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_POST => 1,
CURLOPT_BINARYTRANSFER => 1,
CURLOPT_HTTPHEADER => $headers,
CURLOPT_POSTFIELDS => $postdata,
CURLOPT_CONNECTTIMEOUT => 3,
]);
$data = curl_exec($ch);
if (curl_errno($ch)) {
$msg = curl_error($ch);
printf("%sn", $msg);
} else {
printf("%sn", $data);
// write token
o.data.token
$body = extract_http_body($data, "rnrn");
// printf("body=[%s]n", $body);
$json = json_decode($body, false);
// if (is_object($json) && $json->code == 0) {
if ($json->code == 0) {
file_put_contents("token.txt", $json->data->token);
}
}
curl_close($ch);

* ./lib/MockValue.php

<?php
class MockValue {
public static $letters = "abcdefghijklmnopqrstuvwxyz";
public static $upper = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
public static $numeric = "0123456789";
public static $email_suffix =
["@gmail.com", "@yahoo.com", "@msn.com", "@hotmail.com", "@aol.com", "@ask.com",
"@live.com", "@qq.com", "@0355.net", "@163.com", "@163.net",
"@263.net", "@3721.net", "@yeah.net", "@126.com", "@sina.com", "@sohu.com", "@yahoo.com.cn"];
public static $mobile_prefix = ["134", "135", "136", "137", "138", "139", "150", "151", "152", "157", "158", "159", "130",
"131", "132", "155", "156", "133", "153"];
public static $grade = ['高三','高二','高一','初三','初二','初一','小六','六年级','七年级','八年级','九年级','高中','初中','小学'];
public static $gradeValue = ["03-2016", "03-2017", "03-2018", "02-2016", "02-2017", "02-2018", "02-2015", "02-2016", "02-2017", "02-2018", "01-2013"];
public static function getNumber(/* int */$width) /* int */ {
$min = 1;
if ($width <= 1) {
$width = 1;
return rand(0, 9);
}
$width -= 1;
for ($i = 0; $i <$width; $i++) {
$min *= 10;
}
$max = $min * 10 - 1;
return rand($min, $max);
}
public static function genPassword() {
return self::random(rand(5,10), self::$letters.self::$numeric);
}
public static function getMobile() {
return self::random(1, self::$mobile_prefix) . self::random(8, self::$numeric);
}
public static function getGrade() {
return self::random(1, self::$grade);
}
public static function getGradeValue() {
return self::pick(self::$gradeValue);
}
public static function getElement($list) {
if (is_string($list)) {
$n = strlen($list);
} else if (is_array($list)) {
$n = count($list);
} else {
throw new InvalidArgumentException("list must string or array");
}
return $list[rand(0, $n-1)];
}
public static function getName() {
return self::random(8, self::$letters);
}
public static function getNumStr($len) {
return self::random($len, self::$numeric);
}
public static function getIP() {
return sprintf("%d.%d.%d.%d", rand(1,254), rand(1,254), rand(1,254), rand(1,254));
}
private static function random(/*int */$length, /* ArrayAccess */ $list) {
if ($length <= 1) {
$length = 1;
}
$s = "";
if (is_string($list)) {
$n = strlen($list);
} else if (is_array($list)) {
$n = count($list);
} else {
throw new InvalidArgumentException("list must string or array");
}
while ($length--) {
$s .= $list[ rand(0, $n-1) ];
// inclusive $n-1
}
return $s;
}
public static function pick($list) {
$n = count($list);
if ($n < 1) {
throw new RunTimeException("Empty list");
}
return $list[ rand(0, $n-1) ];
}
public static function getHD() {
$op = self::$letters. self::$upper. self::$numeric;
return self::random(22, $op)."==";
}
public static function getArea() {
$c = file_get_contents(dirname(__FILE__)."/district.json");
$a = json_decode($c, true);
$pick = function($list) {
return $list[ rand(0, count($list)-1) ];
};
$city = $pick($a);
$district = $pick($city["items"]);
$country = $pick($district["items"]);
return sprintf("%s_%s_%s", $city["name"], $district["name"], $country);
}
public static function main() {
echo self::getNumber(5).PHP_EOL;
echo self::getMobile().PHP_EOL;
echo self::getGrade().PHP_EOL;
echo self::getHD().PHP_EOL;
echo self::getArea().PHP_EOL;
}
}
// MockValue::main();

地区json 参照 https://blog.csdn.net/fareast_mzh/article/details/89083909

 

php 接收json格式数据

<?php
$input = file_get_contents('php://input');
var_dump($input);

php -S 0.0.0.0:8089

 

测试发送json

curl -i -X POST -d '{c:3,d:[{"name1":"value1"},{"name2":"value2"]}' -H "Content-Type: application/json" http://47.93.27.106:8089/input.php?a=1

输出:

HTTP/1.1 200 OK
Host: 47.93.27.106:8089
Date: Thu, 18 Jul 2019 09:45:51 GMT
Connection: close
X-Powered-By: PHP/7.3.6
Content-type: text/html; charset=UTF-8

string(46) "{c:3,d:[{"name1":"value1"},{"name2":"value2"]}"

 

最后

以上就是舒适嚓茶为你收集整理的curl 命令行使用 发送json 提交表单 上传文件 php curl post 测试注册的全部内容,希望文章能够帮你解决curl 命令行使用 发送json 提交表单 上传文件 php curl post 测试注册所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(42)

评论列表共有 0 条评论

立即
投稿
返回
顶部