【sprintf用法】这样写PHP的curl的配置文件觉得靠谱点,用file_get_contens不容易出错(和curl比较)。

jackxiang 2009-4-9 15:33 | |
原型:

<?php
$keys= "123,234,456";
define("URL","10.69.3.23:8199/?class=0&keys=%s");
$txt = sprintf(URL, urlencode($keys));
echo $txt;
?>

配置文件分离:
define.php

define('BLOGBINDURL',"http://o.xianguo.com/sina/blogbind?email=%s&puid=%s&blogUrl=%s&action=%s&api
key=%s&time=%s&securitykey=%s");


ZZZ.php

        public function bindblog($email,$uid,$blogurl,$action)
        {
                $time = time();
                $securitykey = md5($uid . BIND_BLOG_APIKEY . $time . BIND_BLOG_PRIKEY);
                $url = sprintf(BLOGBINDURL, urlencode($email), urlencode($uid), urlencode("http://".
$blogurl), urlencode($action), BIND_BLOG_APIKEY, urlencode($time), urlencode($securitykey));
                $flag = Check::request($url,$result);
                if(1 == $flag)  {
                        $resultReturn = json_decode($result,true);
                        if(0 === $resultReturn['status'])
                                return 1;
                        if(1 === $resultReturn['status'])
                                return 0;
                } else  {
                        return 0;
                }

        }



Check.php


  static public function request($url,&$result,$timeout=10) {
    $ctx = stream_context_create(array(
      'http' => array(
      'timeout' =>$timeout
      )
    ));
    $result = file_get_contents($url,false,$ctx);
    if ($result === false) {
      error_log("request $url   timeout",0);
      return 0;
    }
    return 1;
  }


参看:http://hi.baidu.com/studyphp/blog/item/055eda62b6bbe0d8e6113a4b.html

作者:jackxiang@向东博客 专注WEB应用 构架之美 --- 构架之美,在于尽态极妍 | 应用之美,在于药到病除
地址:https://jackxiang.com/post/1703/
版权所有。转载时必须以链接形式注明作者和原始出处及本声明!


最后编辑: jackxiang 编辑于2009-4-9 15:53
评论列表
发表评论

昵称

网址

电邮

打开HTML 打开UBB 打开表情 隐藏 记住我 [登入] [注册]