分类 php 下的文章

php使用curl发送post请求发生错误,记录下

php代码如下

$url = "xxxxxx";

$curl = curl_init();
curl_setopt($curl,CURLOPT_URL,$url);
curl_setopt($curl,CURLOPT_RETURNTRANSFER,1);
curl_setopt($curl, CURLOPT_TIMEOUT,30);   //只需要设置一个秒的数量就可以

$data = http_build_query(array('url_content'=>$urlData['url_id']));//转换成键值对的形
$cookie_file = "./google.txt"; //cookie存放地址
//设置时区
date_default_timezone_set('PRC');
curl_setopt($curl,CURLOPT_COOKIESESSION,1);
curl_setopt($curl,CURLOPT_COOKIEFILE,$cookie_file);
curl_setopt($curl,CURLOPT_COOKIEJAR,$cookie_file);
curl_setopt($curl,CURLOPT_COOKIE,session_name() . '=' . session_id());
curl_setopt($curl,CURLOPT_FOLLOWLOCATION,1);
curl_setopt($curl,CURLOPT_POST,1);
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);//要发送的数据
curl_setopt($curl,CURLOPT_HTTPHEADER,array("application/x-www-form-urlencoded;charset=utf-8","Content-length:".strlen($data)));//http
$OutPut = curl_exec($curl);//

这个请求的网址是当前主机下的另外一个文件,程序使用的是ThinkPHP,请求执行会失败,错误号 28 ,对应的错误是 CURLE_OPERATION_TIMEDOUT,查看错误日志发现是 ThinkPHP session()函数的下面一行报错

// 启动session
if(C('SESSION_AUTO_START'))  session_start();

在if上面添加了

file_put_contents("./curl.text","1",FILE_APPEND);

发现重复调用了很多次,把curl代码中设置cookie的四句代码去掉久ok了,先不调试了,之后有时间再来弄明白

利用余弦值计算字串的相似性

之前用字符串匹配的方式做过一次,感觉这个很厉害,竟然可以用余弦来做,准不准不知道,之前做过php的东西都没记录下来,现在开始记录下
参考网址:http://www.ruanyifeng.com/blog/2013/03/cosine_similarity.html

    public function test($value='')
    {
        $textB = "我喜欢看电视,不喜欢看电影。";
        $textA = "我不喜欢看电视,也不喜欢看电影。";
        // 1.分词
        $scws = scws_new();
        $scws->set_charset("utf8");
        $scws->send_text($textA);

        $wordAList = $scws->get_words('~un');
        $scws->send_text($textB);
        $wordBList = $scws->get_words('~un');
        /**
        *   分词完成之后的格式
        *   [0] => array(4) {
        *       ["word"] => string(9) "我喜欢"
        *       ["times"] => int(1)
        *       ["weight"] => float(4.8200001716614)
        *       ["attr"] => string(1) "n"
        *   }
        */
        // 2.列出所有的词
        // 提取多维数组的值
        $allWord = array_unique(array_merge(array_column($wordAList, 'word'),array_column($wordBList, 'word')));
        /**
        *   字符串数组
        *   [0] => string(9) "我喜欢"
        *   [1] => string(3) "看"
        *   [2] => string(6) "电视"
        *   [3] => string(3) "不"
        *   [4] => string(6) "喜欢"
        *   [5] => string(9) "看电影"
        *   [6] => string(3) "我"
        *   [11] => string(3) "也"
        */
        // 3.计算词频向量
        $textATimes = array();// 整型数组
        $textBTimes = array();
        foreach ($allWord as $key => $value) {
            // 提取多维数组的值
            $a = array_filter($wordAList, function($arr) use ($value) {
                if($value == $arr['word'])
                {
                    return $arr['times'];
                }
                return 0; 
            });
            $textATimes[] = (Int)(current($a)['times']);
            $b = array_filter($wordBList, function($arr) use ($value) {
                if($value == $arr['word'])
                {
                    return $arr['times'];
                }
                return 0; 
            });
            $textBTimes[] = (Int)(current($b)['times']);
        }

        // 使用余弦计算
        $topNumber = 0;
        $bottomNumberA = 0;
        $bottomNumberB = 0;
        foreach ($textATimes as $key => $value) {
            $topNumber +=  $value * $textBTimes[$key];
            $bottomNumberA += $value * $value;
            $bottomNumberB += $textBTimes[$key] * $textBTimes[$key];
        }
        $res = $topNumber / (sqrt($bottomNumberA) * sqrt($bottomNumberB));
        // 余弦值越接近1,就表明夹角越接近0度,也就是两个向量越相似,这就叫"余弦相似性"
        // float(0.79259392390122)
        dump($res);
    }

自己执行指定控制器的action

$whereWithForum = array(
                        'fid' =>  $tempForumInfo['board_id'],
                        'page' => 1,
                        'pageSize' => 5,
                        'sort' => 'new',
                        'filterType' => '',
                        'filterId' => 0,
                        'isImageList' => 0,
                        'topOrder' => 0
                    );
$newsForumList = Yii::app()->getController("forum/topiclist")->createAction('topiclist')->getResult($whereWithForum);

centos7安装nginx和php碰到的一些问题

nginx安装,参考网址:https://www.digitalocean.com/community/tutorials/how-to-install-linux-nginx-mysql-php-lemp-stack-on-centos-7

rpm -Uvh http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm

yum install nginx

安装php和php-fpm

yum install --enablerepo=remi --enablerepo=remi-php56 php  php-fpm

因为我的php安装的是5.6的,所以的指定这个源里面安装php-fpm,不然一直报错

修改配置文件

vi /etc/php.ini

修改成0(这个配置值说是不安全的设置,参考网址:http://www.laruence.com/2010/05/20/1495.html)

cgi.fix_pathinfo=0

修改配置文件

vim /etc/nginx/conf.d/default.conf

server {
    listen       80;
    server_name  www.scchary.com;
    root /home/samba1/public_html;
    #charset koi8-r;
    #access_log  /var/log/nginx/log/host.access.log  main;

    location / {
        #root   /usr/share/nginx/html;
        #root /home/samba1/public_html;
        index  index.php index.html index.htm;
    }

    #error_page  404              /404.html;

    # redirect server error pages to the static page /50x.html
    #
    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /usr/share/nginx/html;
    }

    # proxy the PHP scripts to Apache listening on 127.0.0.1:80
    #
    #location ~ \.php$ {
    #    proxy_pass   http://127.0.0.1;
    #}

    # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
    #
    location ~ \.php$ {
        #root           /home/samba1/public_html;
        fastcgi_pass   127.0.0.1:9000;
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
        include        fastcgi_params;
    }

    # deny access to .htaccess files, if Apache's document root
    # concurs with nginx's one
    #
    #location ~ /\.ht {
    #    deny  all;
    #}
}

修改上面配置文件的时候,运行php文件的时候,老是显示没有找到,后来在这里找到了答案,参考网址http://www.nginx.cn/562.html

server {
    listen   [::]:80;
    server_name  example.com www.example.com;
    access_log  /var/www/logs/example.com.access.log;  

    location / {
        root   /var/www/example.com;
        index  index.html index.htm index.pl;
    }

    location /images {
        autoindex on;
    }

    location ~ \.php$ {
        fastcgi_pass   127.0.0.1:9000;
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME  /var/www/example.com$fastcgi_script_name;
        include fastcgi_params;
    }
}

这个配置中有很多不合理的地方,其中一个明显的问题就是root指令被放到了location / 块。如果root指令被定义在location块中那么该root指令只能对其所在的location生效。其它locaiont中没有root指令,像location /images块不会匹配任何请求,需要在每个请求中重复配置root指令来解决这个问题。因此我们需要把root指令放在server块,这样各个location就会继承父server块定义的\$document_root,如果某个location需要定义一个不同的\$document_root,则可以在location单独定义一个root指令。

另一个问题就是fastCGI参数SCRIPT_FILENAME 是写死的。如果修改了root指令的值或者移动文件到别的目录,php-fpm会返回“No input file specified”错误,因为SCRIPT_FILENAME在配置中是写死的并没有随着$doucument_root变化而变化,我们可以修改SCRIPT_FILENAME配置如下:

fastcgi_param SCRIPT_FILENAME \$document_root\$fastcgi_script_name;

所以我们不能忘记在server块中配置root指令,不然\$document_root的值为空,只会传\$fastcgi_script_name到php-fpm,这样就会导致“No input file specified”错误。

最后在测试php文件里面输出phpinfo的时候,出现了一个未定义时区的错误,修改了配置文件还是报错,最后重启了下php-fpm就好了

centos6.6搭建LANP环境(yum)

nginx
自带的yum源的nginx版本比较低,到nginx官网下载版本对应的源http://nginx.org/en/linux_packages.html#stable

然后安装

rpm -ivf nginx-release-centos-6-0.el6.ngx.noarch.rpm

再安装nginx

yum install nginx

安装成功,启动nginx

service nginx start 

mysql
去mysql的官网下载源,http://dev.mysql.com/downloads/repo/yum/
这个页面有安装步骤,http://dev.mysql.com/doc/mysql-yum-repo-quick-guide/en/
安装下载的源

rpm -ivf mysql-community-release-el6-5.noarch.rpm

安装mysql

yum install mysql-community-server

启动mysql

service mysqld start

会报错,启动不成功

初始化 MySQL 数据库: 2015-06-08T13:15:59.011125Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2015-06-08T13:15:59.013681Z 0 [ERROR] Can't change data directory owner to mysql
2015-06-08T13:15:59.013693Z 0 [ERROR] Aborting

关闭下selinux

setenforce 0

再启动下,然后进行运行mysql的命令进行一些初始化工作(具体设置内容可以看centos7安装LANP)
运行这个命令需要的密码可以用这个命令看到

grep 'temporary password' /var/log/mysqld.log
mysql_secure_installation

php
安装php的源,比较新

yum install epel-release
rpm -ivh http://rpms.famillecollet.com/enterprise/remi-release-6.rpm

安装php(这里安装了一些php的扩展,有些不知道是干嘛的~~~可以根据需求去掉,但是php-fpm和php-mysqlnd最好别去掉,一个是之后和nginx整合需要用到的,一个连接mysql的)

yum install --enablerepo=remi --enablerepo=remi-php56 php php-fpm php-opcache php-devel php-mbstring php-mcrypt php-mysqlnd php-phpunit-PHPUnit php-pecl-xdebug php-pecl-xhprof

php和nginx
先启动php-fpm

service php-fpm start

修改nginx配置文件(配置在这里点击我)
然后就ok了

php和mysql
先打开selinux的一个选项

setsebool -P httpd_can_network_connect=1

再测试下,应该就ok了~~~~

设置下服务在开机的时候自启动

chkconfig --level 235 php-fpm on
chkconfig --level 235 mysqld on
chkconfig --level 235 nginx on

这样设置之后因为防火墙的关系,只有本机可以访问,查看防火墙的设定防火墙设定