file_get_contents版本:

 /**
* 发送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;
}

实战经验:

当我利用上述代码给另一台服务器发送http请求时,发现,如果服务器处理请求时间过长,本地的PHP会中断请求,即所谓的超时中断,第一个怀疑的是PHP本身执行时间的超过限制,但想想也不应该,因为老早就按照这篇文章设置了“PHP执行时间限制”(【推荐】PHP上传文件大小限制大全 ),仔细琢磨,想想,应该是http请求本身的一个时间限制,于是乎,就想到了怎么给http请求时间限制搞大一点。。。。。。查看PHP手册,果真有个参数 “ timeout ”,默认不知道多大,当把它的值设大一点,问题得已解决,弱弱地做个笔记~~~

Socket版本:

 /**
* Socket版本
* 使用方法:
* $post_string = "app=socket&version=beta";
* request_by_socket('blog.snsgou.com', '/restServer.php', $post_string);
*/
function request_by_socket($remote_server,$remote_path,$post_string,$port = 80,$timeout = 30) {
$socket = fsockopen($remote_server, $port, $errno, $errstr, $timeout);
if (!$socket) die("$errstr($errno)");
fwrite($socket, "POST $remote_path HTTP/1.0");
fwrite($socket, "User-Agent: Socket Example");
fwrite($socket, "HOST: $remote_server");
fwrite($socket, "Content-type: application/x-www-form-urlencoded");
fwrite($socket, "Content-length: " . (strlen($post_string) + 8) . "");
fwrite($socket, "Accept:*/*");
fwrite($socket, "");
fwrite($socket, "mypost=$post_string");
fwrite($socket, "");
$header = "";
while ($str = trim(fgets($socket, 4096))) {
$header .= $str;
} $data = "";
while (!feof($socket)) {
$data .= fgets($socket, 4096);
} return $data;
}

Curl版本:

 /**
* Curl版本
* 使用方法:
* $post_string = "app=request&version=beta";
* request_by_curl('http://blog.snsgou.com/restServer.php', $post_string);
*/
function request_by_curl($remote_server, $post_string) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $remote_server);
curl_setopt($ch, CURLOPT_POSTFIELDS, 'mypost=' . $post_string);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_USERAGENT, "snsgou.com's CURL Example beta");
$data = curl_exec($ch);
curl_close($ch); return $data;
}

Curl版本(2)

 /**
* 发送HTTP请求
*
* @param string $url 请求地址
* @param string $method 请求方式 GET/POST
* @param string $refererUrl 请求来源地址
* @param array $data 发送数据
* @param string $contentType
* @param string $timeout
* @param string $proxy
* @return boolean
*/
function send_request($url, $data, $refererUrl = '', $method = 'GET', $contentType = 'application/json', $timeout = 30, $proxy = false) {
$ch = null;
if('POST' === strtoupper($method)) {
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_HEADER,0 );
curl_setopt($ch, CURLOPT_FRESH_CONNECT, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_FORBID_REUSE, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
if ($refererUrl) {
curl_setopt($ch, CURLOPT_REFERER, $refererUrl);
}
if($contentType) {
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type:'.$contentType));
}
if(is_string($data)){
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
} else {
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));
}
} else if('GET' === strtoupper($method)) {
if(is_string($data)) {
$real_url = $url. (strpos($url, '?') === false ? '?' : ''). $data;
} else {
$real_url = $url. (strpos($url, '?') === false ? '?' : ''). http_build_query($data);
} $ch = curl_init($real_url);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type:'.$contentType));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
if ($refererUrl) {
curl_setopt($ch, CURLOPT_REFERER, $refererUrl);
}
} else {
$args = func_get_args();
return false;
} if($proxy) {
curl_setopt($ch, CURLOPT_PROXY, $proxy);
}
$ret = curl_exec($ch);
$info = curl_getinfo($ch);
$contents = array(
'httpInfo' => array(
'send' => $data,
'url' => $url,
'ret' => $ret,
'http' => $info,
)
); curl_close($ch);
return $ret;
}

调用 WCF接口 的一个例子:$json = restRequest($r_url,'POST', json_encode($data));

转至:http://blog.snsgou.com/post-161.html

最新文章

  1. Java基础知识
  2. max virtual memory areas vm.max_map_count [65530] likely too low, increase to at least [262144]
  3. 浅谈java性能分析
  4. ASP.NET MVC Web API 学习笔记---第一个Web API程序
  5. 20145225《Java程序设计》 第9周学习总结
  6. php cURL library is not loaded
  7. -_-#【减少 DOM 元素】textarea, script 延迟渲染
  8. 【转】关于onActivityResult方法不执行的问题汇总
  9. LINUX VPS 查看系统信息命令
  10. jd.py
  11. Java面试之框架篇(九)
  12. flex 布局 实现电商页面商品展示floor
  13. python黑帽子
  14. 要继续看Python写算法的内容请到那里去
  15. dir()函数
  16. OpenGL笔记(五) 着色器渲染(以Android为例)
  17. ionic4 ios调试打包
  18. 为Python编写一个简单的C语言扩展模块
  19. appium+python自动化☞环境搭建
  20. Action Required: Listings Deactivated for Potential Pricing Error

热门文章

  1. MySQL的库、表详细操作
  2. MySQL Migration Tool报“initialized java loader”错误的问题
  3. windows下Composer因php_openssl扩展缺失而安装失败
  4. java的instanceof简单使用
  5. gcc对open(2)支持重载吗
  6. "Calendars" and "DateFormats" should not be static
  7. http协议--文章一
  8. 介绍Windows Azure HDInsight服务的Hadoop Storm的视频
  9. jQuery插件开发之windowScroll
  10. 发布.net 4.0的站点到IIS7.5下时无法访问