注册 登录  
 加关注
   显示下一条  |  关闭
温馨提示!由于新浪微博认证机制调整,您的新浪微博帐号绑定已过期,请重新绑定!立即重新绑定新浪微博》  |  关闭

慵懒的乌龟

——若有,且珍惜~

 
 
 

日志

 
 

php中curl,fscokopen和file_get_contents 使用范例  

2013-01-06 15:33:43|  分类: 网络 |  标签: |举报 |字号 订阅

  下载LOFTER 我的照片书  |

范例1(file_get_contents):

#!/usr/local/php-fpm/bin/php
<?php
header('cache-control: private');
header('Content-Type: text/html; charset=utf-8');

function http_request($url, $data = array(), $timeout = 30, $method = 'POST', $proxy = false) {
        $data = is_array($data) ? http_build_query($data) : $data;
        $http = array(
                'timeout' => $timeout,
                'method' => $method,
                'header'=> "Content-type: application/x-www-form-urlencoded\r\n" .
                "Content-Length: " . strlen($data) . "\r\n",
                'timeout' => $timeout,
                'content' => $data
            );
    if($proxy){
        $http['proxy'] = $proxy;
        $http['request_fulluri'] = true;
    }
        $opts = array(
                'http' => $http,
        );
    $context = stream_context_create($opts);
    $rs = file_get_contents($url, false, $context);
    return $rs;
}

#$appid=$argv[1];
#$ip=$argv[2];
$request_url="http://www.*******.com"
$event_id = 0;
$timestamp = 1;
$request = array(

        "version" => 2,
        "componentName" => "组件名称",
        "eventId" => "$event_id",
        "timestamp" => "$timestamp",
        "interface" => array(
                "interfaceName" => "接口名称",
                "para" => array(
                        ),
        ),
);

echo "***********  send **************\n";
print_r($request);
echo "******************************\n";
$params = json_encode($request);
$datas = http_request($request_url, $params);

echo "***********  recv *************\n";
print_r(json_decode($datas,true));
echo "******************************\n";

 

范例2(curl):

<?php
define("FW",true);
/**
 * @abstract send http request
 * @param $url
 * @param $data
 * @param $timeout
 * @param $method
 * @param $proxy
 * @return unknown_type
 */
function http_request($url, $data = array(), $timeout = 30, $method = 'POST', $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(is_string($data)){
   curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
            echo "json data \n";       
  }else{
   curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));
                 echo "no json data \n"; 
}
 }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_RETURNTRANSFER, 1);
  curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
 }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;
}

$args = array (
  "version" => 1,
  "caller" => "***",
  "password" => "password",
  "callee" => "C***B",
  "eventId" => rand(),
  "timestamp" => 1349932699,
  "interface" =>
  array (
    "interfaceName" => "调用的接口名",
    "para" =>
    array (
      "id" =>
      array (
        0 => 0,
      ),
      "level" => 0,
    ),
  ),
);

$json = json_encode($args);
print_r($json);
echo "\n\n";
$ret = http_request("http://www.****.com", $json);

print_r($ret);

exit;

范例3(fscokopen):

function triggerRequest($url, $post_data = array(), $cookie = array())…{
$method = "GET"; //可以通过POST或者GET传递一些参数给要触发的脚本
$url_array = parse_url($url); //获取URL信息,以便平凑HTTP HEADER
$port = isset($url_array['port'])? $url_array['port'] : 80;

$fp = fsockopen($url_array['host'], $port, $errno, $errstr, 30);
if (!$fp) …{
return FALSE;
}
$getPath = $url_array['path'] ."?". $url_array['query'];
if(!empty($post_data))…{
$method = "POST";
}
$header = $method . " " . $getPath;
$header .= " HTTP/1.1\r\n";
$header .= "Host: ". $url_array['host'] . "\r\n "; //HTTP 1.1 Host域不能省略
/**//*以下头信息域可以省略
$header .= "User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.13) Gecko/20080311 Firefox/2.0.0.13 \r\n";
$header .= "Accept: text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,q=0.5 \r\n";
$header .= "Accept-Language: en-us,en;q=0.5 ";
$header .= "Accept-Encoding: gzip,deflate\r\n";
*/

$header .= "Connection:Close\r\n";
if(!empty($cookie))…{
$_cookie = strval(NULL);
foreach($cookie as $k => $v)…{
$_cookie .= $k."=".$v."; ";
}
$cookie_str = "Cookie: " . base64_encode($_cookie) ." \r\n";//传递Cookie
$header .= $cookie_str;
}
if(!empty($post_data))…{
$_post = strval(NULL);
foreach($post_data as $k => $v)…{
$_post .= $k."=".$v."&";
}
$post_str = "Content-Type: application/x-www-form-urlencoded\r\n";//POST数据
$post_str .= "Content-Length: ". strlen($_post) ." \r\n";//POST数据的长度
$post_str .= $_post."\r\n\r\n "; //传递POST数据
$header .= $post_str;
}
fwrite($fp, $header);
//echo fread($fp, 1024); //我们不关心服务器返回
fclose($fp);
return true;
}

  评论这张
 
阅读(658)| 评论(0)
推荐 转载

历史上的今天

评论

<#--最新日志,群博日志--> <#--推荐日志--> <#--引用记录--> <#--博主推荐--> <#--随机阅读--> <#--首页推荐--> <#--历史上的今天--> <#--被推荐日志--> <#--上一篇,下一篇--> <#-- 热度 --> <#-- 网易新闻广告 --> <#--右边模块结构--> <#--评论模块结构--> <#--引用模块结构--> <#--博主发起的投票-->
 
 
 
 
 
 
 
 
 
 
 
 
 
 

页脚

网易公司版权所有 ©1997-2017