通过file_get_contents()发送GET、POST请求

get

$data = array( 'name'=>'zhezhao','age'=>'23');

$query = http_build_query($data);

$url = 'http://localhost/get.php';//这里一定要写完整的服务页面地址,否则php程序不会运行

$result = file_get_contents($url.'?'.$query);

post

 $v) {
                    $dec_arr[$k] = is_string($v) || is_numeric($v) ? trim($v) : $v;
                }
            }
            return $dec_arr;
    }

 $array = [
	'app_type' => '27',
	'sign_key' => 'ca7bb651cd70641b916f0ee31b736418',
	'u_token' =>'38864236402f910689ffa21c5871150c',
	'invoice_id'=>'14'
];
$array = ['yb'=>get_enc_data($array)];//加密
$body = http_build_query($array);
$content = stream_context_create(
	[
		'http' => [
			'method' => 'post',
			'header' => "Content-Type: application/x-www-form-urlencoded; charset=UTF-8"."Content-Length: " . mb_strlen($body),
			'content' => $body
			]
	]);

$response = file_get_contents('http://127.0.0.1/XXX',true,$content);
$response = get_dec_data($response);//解密
var_dump($response);

 

你可能感兴趣的:(通过file_get_contents()发送GET、POST请求)