怎样通过file_get_contents()发送GET、POST要求?(办法介绍)
发布时间:09/01 来源:未知 浏览:
关键词:
效劳器端施行HTTP恳求,大家经常使用的就是CURL,curl工具确实是很好的数据文件传输工具,那么除此之外还有其他的工具能实现这个功效吗?
此刻为你介绍一个很常见的工具 file_get_content()
纳尼,这不是PHP文件操纵函数吗??? 居然还能实现GET POST 恳求???
这里可以很明白的告诉你,完全可以!!!
废话不多说,直接上代码。有问题来打我、记得带医药费
1、【GET恳求】
$data = array( 'name'=>'zhezhao','age'=>'23'); $query = http_build_query($data); $url = 'http://localhost/get.php';//这里必然要写完全的效劳页面地址,不然php程序不会运转 $result = file_get_contents($url.'?'.$query);
2、【POST恳求】
$data = array('user'=>'jiaxiaozi','passwd'=>'123456'); $requestBody = http_build_query($data); $context = stream_context_create(['http' => ['method' => 'POST', 'header' => "Content-Type: application/x-www-form-urlencoded\r\n"."Content-Length: " . mb_strlen($requestBody), 'content' => $requestBody]]); $response = file_get_contents('http://server.test.net/login', false, $context);
相关教程引荐:《PHP教程》
以上就是怎样通过file_get_contents()发送GET、POST恳求?(办法介绍)的具体内容,更多请关注百分百源码网其它相关文章!