Skip to content

Instantly share code, notes, and snippets.

@lixingcong
Last active July 5, 2021 08:04
Show Gist options
  • Save lixingcong/75bbbea58a36685b1f4378664dc9e0f0 to your computer and use it in GitHub Desktop.
Save lixingcong/75bbbea58a36685b1f4378664dc9e0f0 to your computer and use it in GitHub Desktop.
PHP测试POST/GET/JSON解析ECHO的demo
<?php
function get($url, $hostname, $data=NULL)
{
$fullUrl=$url;
if(NULL!==$data){
$fullUrl.= '?'.http_build_query($data);
}
$opts = array('http' =>
array(
'method' => 'GET',
'header' => "token: ".'MyToken'."\r\nHost: ".$hostname."\r\n",
)
);
$context = stream_context_create($opts);
$result = file_get_contents($fullUrl, false, $context);
return $result;
}
print_r(get('http://localhost', 'localhost', ['ddd'=>233]));
<?php
function post($url, $hostname, $data)
{
$opts = array('http' =>
array(
'method' => 'POST',
'header' => "Content-Type: application/json; charset=UTF-8\r\nAccept-Encoding:gzip\r\ntoken: ".'MyToken'."\r\nHost: ".$hostname."\r\n",
'content' => json_encode($data)
)
);
$context = stream_context_create($opts);
$result = file_get_contents($url, false, $context);
return $result;
}
print_r(post('http://localhost', 'localhost', ['ddd'=>233]));
<?php
// post_data变量为常见的post数据数组
// files为二维数组,第一维为field,第二维分别是文件二进制内容+传给服务器的名字
// files = [ 'document' => [ 'name' => '1.mp3', 'binary' = file_get_contents("/tmp/1.mp3") ] ];
function httpPostDataFile($url, $hostname, $post_data = null, $files = null)
{
$boundary = '########BB########'.microtime(true);
$eol="\r\n";
$options = array(
// use key 'http' even if you send the request to https://...
'http' => array(
'header' => "Content-Type: multipart/form-data; boundary=".$boundary.$eol.'Host: ".$hostname.$eol
'method' => 'POST',
'content' => ''
),
);
if (is_array($post_data)) {
foreach ($post_data as $name => $data) {
$c='--'.$boundary.$eol;
$c.='Content-Disposition: form-data; name="' . $name.'"';
$c.=$eol.$eol;
if(is_array($data))
$c.=json_encode($data).$eol;
else
$c.=$data.$eol;
$options['http']['content'].=$c;
}
}
if (is_array($files)) {
foreach ($files as $field => $file) {
// 这里调试最麻烦了,要结合postman中的code片段查看HTTP请求的原文
$c='--'.$boundary.$eol;
$c.='Content-Disposition: form-data; name="' . $field.'"; filename="'.$file['name'].'"'.$eol;
$c.='Content-Type: application/octet-stream'.$eol.$eol;
$c.=$file['binary'];
$c.=$eol;
$options['http']['content'] .= $c;
}
}
if (is_array($post_data) && count($post_data) || is_array($files) && count($files)) {
$options['http']['content'] .= '--' . $boundary . '--'. $eol;
}
//可以调试输出HTTP请求报文
//print_r($options);
$context = stream_context_create($options);
return file_get_contents($url, false, $context);
}
$fileContent=file_get_contents('/tmp/hello.txt');
$postFile=[
'document' => [
'binary' => $fileContent,
'name' => 'hello.txt'
]
];
httpPostDataFile('http://localhost', ['dd'=>12334455], $postFile);
<?php
//print_r($_SERVER);
$requsetMethod=$_SERVER["REQUEST_METHOD"];
if ('POST' === $requsetMethod)
{
echo('You sent me a POST request!'."\n");
if (strpos($_SERVER["CONTENT_TYPE"], 'application/json') !== false){
$isGbk=false;
if(strpos($_SERVER["CONTENT_TYPE"], 'charset=gbk') !== false || strpos($_SERVER["CONTENT_TYPE"], 'charset=gb2312') !== false)
$isGbk=true;
echo('Content-Type is '.($isGbk?'gbk':'utf8').' JSON!'."\n");
$body=file_get_contents("php://input");
header("Content-Type: text/plaintext; charset=utf-8");
//print_r($body);
if($isGbk)
$body=iconv('GB2312', 'UTF-8',$body);
$json=json_decode($body, true);
if($json)
print_r($json);
else
echo("Invalid json format!");
}else{
foreach($_POST as $k=>$v)
echo($k.'='.$v."\n");
}
}else if ('GET' === $requsetMethod){
echo('You sent me a GET request!'."\n");
foreach($_GET as $k=>$v)
echo($k.'='.$v."\n");
}else{
echo("Error: Not a POST or GET request!");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment