网站建设资讯

NEWS

网站建设资讯

php发送请求数据 php发送数据包

求助PHP如何POST提交数据

用PHP向服务器发送HTTP的POST请求,代码如下:

创新互联服务项目包括通河网站建设、通河网站制作、通河网页制作以及通河网络营销策划等。多年来,我们专注于互联网行业,利用自身积累的技术优势、行业经验、深度合作伙伴关系等,向广大中小型企业、政府机构等提供互联网行业的解决方案,通河网站推广取得了明显的社会效益与经济效益。目前,我们服务的客户以成都为中心已经辐射到通河省份的部分城市,未来相信会继续扩大服务区域并继续获得客户的支持与信任!

?php

/**    

* 发送post请求    

* @param string $url 请求地址    

* @param array $post_data post键值对数据    

* @return string    

*/    

function send_post($url, $post_data) {    

$postdata = http_build_query($post_data);    

$options = array(    

'http' = array(    

'method' = 'POST',    

'header' = 'Content-type:application/x-www-form-urlencoded',    

'content' = $postdata,    

'timeout' = 15 * 60 // 超时时间(单位:s)    

)    

);    

$context = stream_context_create($options);    

$result = file_get_contents($url, false, $context);             

return $result;    

}

使用的时候直接调用上面定义的send_post方法:

$post_data = array(

'username' = 'username',

'password' = 'password'

);

send_post('网址', $post_data);

PHP如何通过Post请求发送Json数据

首先要把数据转换成json格式,再通过curl方法调用接口并传参数

代码如下:

$keyword = urlencode($_POST['keyword']);

$parameters = json_encode(array('keyWord'=$keyword,'areaCode'='*'));

$post_data['appToken'] = "323ds7674354fds32fdsda60173";//随便写的

$post_data['parameters'] = $parameters;

$url = '';//随便写的

$ch = curl_init();

curl_setopt($ch, CURLOPT_POST, 1);

curl_setopt($ch, CURLOPT_URL, $url);

curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);//用post方法传送参数

curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

$response = curl_exec($ch);

curl_close($ch);

之后就返回数据即可。

php怎么响应客户端发送http请求

http请求有get,post。

php发送http请求有三种方式[我所知道的有三种,有其他的告诉我]。

1. file_get_contents();详情见:

2. curl发送请求。

3. fsocket发送。

下面说使用curl发送。

首先环境需要配置好curl组件。

在windows中让php支持curl比较简单:

在php.ini中将extension=php_curl.dll前面的分号去掉,

有人说需要将php根目录的libeay32.dll和ssleay32.dll需要拷贝到系统目录下去。我实验不拷贝也可以。

在linux中,如果使用源码安装,需要在make 之前,./configure --with-curl=path,

其中,path是你的 libcurl库的位置,比如你安装libcurl库之后,

path可能就是/usr/local/,libcurl可以是静态库,也可以是动态库。

注意libcurl库configure的时候,可以将一些不需要的功能去掉,

比如ssl , ldap等。在php configure的时候,会去检查libcurl中某些功能是否被开启,进而去相应地调整生成的php。


分享标题:php发送请求数据 php发送数据包
链接分享:http://cdweb.net/article/dojjojs.html