php stdev 標準差計算範例

用這組數字20,25,21,30,26,28,23,26,30,27計算標準差

可以在這個頁面計算

https://www.easycalculation.com/statistics/standard-deviation.php

答案是 3.4383458555

用 LibreOffice 計算也是這個答案

用 PHP PECL 計算的方式如下

$a = array();
array_push($a, 20);                                                                                                                                                                                          
array_push($a, 25);
array_push($a, 21);
array_push($a, 30);
array_push($a, 26);
array_push($a, 28);
array_push($a, 23);
array_push($a, 26);
array_push($a, 30);
array_push($a, 27);
echo stats_standard_deviation($a, true);

PHP 預設是沒有安裝這個 function 的,必須透過下面這個方式安裝

sudo apt-get install php-pear php5-dev
sudo pecl install stats
sudo echo "extension=stats.so" >> /etc/php5/cli/php.ini

如果 PHP 版本太高,無法安裝 PECL stats 套件的話

$ sudo pecl install stats
WARNING: channel "pecl.php.net" has updated its protocols, use "pecl channel-update pecl.php.net" to update
pecl/stats requires PHP (version >= 5.3.0, version <= 5.6.99), installed version is 7.0.9-1+deb.sury.org~trusty+1
No valid packages found
install failed

下面這個網址有提供實作,貼到自己的 PHP 裡面就可以用了
http://php.net/manual/en/function.stats-standard-deviation.php#99792

ElasticSearch php spider and index page

show you the screen shot:
Screenshot from 2015-04-29 22:15:43
below demo site is on Amazon EC2 free tier, will close before 2016/4/1
http://owen-wen.twbbs.org/

and I have create a github for this simple example
https://github.com/wenchiching/elasticsearch_webspider

the php spider from here http://phpcrawl.cuab.de/
the php dom parser from here http://simplehtmldom.sourceforge.net/

https://github.com/wenchiching/elasticsearch_webspider/commit/65f628441d98471f4d37c030ad08bda31efa67e9?diff=split
this commit show how to retrieve web and index web into ElasticSeach

And when I want to index more page, I encounter a problem that a page will be indexed again, and redundant index produced.

because I didn’t specify an ID when index a page, so if I want to index a page with an ID, what ID should I use? I have no idea.

Can I use an increment integer as ID? No, it will just index the same page with another integer ID

Head first elasticsearch-php

read https://wenchiching.wordpress.com/2015/04/02/head-first-elasticsearch/ to install ElasticSearch

follow the quick start http://www.elastic.co/guide/en/elasticsearch/client/php-api/current/_quickstart.html

1. edit a file named composer.json and content as below

{
    "require": {
        "elasticsearch/elasticsearch": "~1.0"
    }
}

2. download php lib via below command

curl -s http://getcomposer.org/installer | php
php composer.phar install

3. edit a php file (such as: example.php) and content as below

require 'vendor/autoload.php';

$client = new Elasticsearch\Client();
$params = array();
$params['body']  = array('testField' => 'abc');
$params['index'] = 'my_index';
$params['type']  = 'my_type';
$params['id']    = 'my_id';
$ret = $client->index($params);

4. test if index success

curl -XPOST 'localhost:9200/my_index/_search?pretty' -d '
{
  "query": { "match": { "testField": "abc" } }
}'

you can see something like below if success

{
  "took" : 4,
  "timed_out" : false,
  "_shards" : {
    "total" : 5,
    "successful" : 5,
    "failed" : 0
  },
  "hits" : {
    "total" : 1,
    "max_score" : 0.30685282,
    "hits" : [ {
      "_index" : "my_index",
      "_type" : "my_type",
      "_id" : "my_id",
      "_score" : 0.30685282,
      "_source":{"testField":"abc"}
    } ]
  }
}

php 下載 youtube 的影片,並且取出 mp3 or ogg 音源

如何從 youtube 下載影片並且自動轉出 mp3 or ogg 檔案


解析出來的網址如下:


array(10) {
  [0]=>
  string(422) "url=http://o-o.preferred.ascc-tpe1.v24.lscache3.c.youtube.com/videoplayback?sparams=id%2Cexpire%2Cip%2Cipbits%2Citag%2Csource%2Cratebypass%2Ccp&fexp=912302%2C906429%2C913105&itag=46&ip=140.0.0.0&signature=3CA6BBF9474841CF105653580E29EB74BB9148FC.325416BE1EBD7407467F642EEFBECCFEBCD5951D&sver=3&ratebypass=yes&source=youtube&expire=1330387565&key=yt1&ipbits=8&cp=U0hSRVNTVV9LT0NOMl9LTVlGOmhZR1cwOUZseVVa&id=e7bceb9ff3396c4f"
  [1]=>
  string(421) "url=http://o-o.preferred.ascc-tpe1.v6.lscache1.c.youtube.com/videoplayback?sparams=id%2Cexpire%2Cip%2Cipbits%2Citag%2Csource%2Cratebypass%2Ccp&fexp=912302%2C906429%2C913105&itag=37&ip=140.0.0.0&signature=3DD8E663008956A26A1DABFADFC0BD852D0C8CF5.CD9C6458FAB292D8144FA8284ED55479B8065734&sver=3&ratebypass=yes&source=youtube&expire=1330387565&key=yt1&ipbits=8&cp=U0hSRVNTVV9LT0NOMl9LTVlGOmhZR1cwOUZseVVa&id=e7bceb9ff3396c4f"
  [2]=>
  string(422) "url=http://o-o.preferred.ascc-tpe1.v14.lscache3.c.youtube.com/videoplayback?sparams=id%2Cexpire%2Cip%2Cipbits%2Citag%2Csource%2Cratebypass%2Ccp&fexp=912302%2C906429%2C913105&itag=45&ip=140.0.0.0&signature=5E6D3DBAEA96C7937278BD88FA520F7FAF287A6D.196CA2DBD1F58D5B0C63370FC0CE2097321F53A2&sver=3&ratebypass=yes&source=youtube&expire=1330387565&key=yt1&ipbits=8&cp=U0hSRVNTVV9LT0NOMl9LTVlGOmhZR1cwOUZseVVa&id=e7bceb9ff3396c4f"
  [3]=>
  string(421) "url=http://o-o.preferred.ascc-tpe1.v7.lscache1.c.youtube.com/videoplayback?sparams=id%2Cexpire%2Cip%2Cipbits%2Citag%2Csource%2Cratebypass%2Ccp&fexp=912302%2C906429%2C913105&itag=22&ip=140.0.0.0&signature=9521DF5BF21112B0C5AC95D4135057DE6EDF6EAD.0DBD9897B1442F30F2436D61459281F266B0E171&sver=3&ratebypass=yes&source=youtube&expire=1330387565&key=yt1&ipbits=8&cp=U0hSRVNTVV9LT0NOMl9LTVlGOmhZR1cwOUZseVVa&id=e7bceb9ff3396c4f"
  [4]=>
  string(422) "url=http://o-o.preferred.ascc-tpe1.v16.lscache4.c.youtube.com/videoplayback?sparams=id%2Cexpire%2Cip%2Cipbits%2Citag%2Csource%2Cratebypass%2Ccp&fexp=912302%2C906429%2C913105&itag=44&ip=140.0.0.0&signature=708BC1DE44BF36D838A7A343343D448D13070901.B6D3417B3410D1CA4AE590DFD1A2444859711B61&sver=3&ratebypass=yes&source=youtube&expire=1330387565&key=yt1&ipbits=8&cp=U0hSRVNTVV9LT0NOMl9LTVlGOmhZR1cwOUZseVVa&id=e7bceb9ff3396c4f"
  [5]=>
  string(470) "url=http://o-o.preferred.ascc-tpe1.v19.lscache3.c.youtube.com/videoplayback?sparams=id%2Cexpire%2Cip%2Cipbits%2Citag%2Csource%2Calgorithm%2Cburst%2Cfactor%2Ccp&fexp=912302%2C906429%2C913105&algorithm=throttle-factor&itag=35&ip=140.0.0.0&burst=40&sver=3&signature=5FBC0A0F53CA3B475D3F50404F0A9CCD30CDD972.7C8E3CA2F856B4A907608B862206CE9C6BFC9D63&source=youtube&expire=1330387565&key=yt1&ipbits=8&factor=1.25&cp=U0hSRVNTVV9LT0NOMl9LTVlGOmhZR1cwOUZseVVa&id=e7bceb9ff3396c4f"
  [6]=>
  string(421) "url=http://o-o.preferred.ascc-tpe1.v9.lscache2.c.youtube.com/videoplayback?sparams=id%2Cexpire%2Cip%2Cipbits%2Citag%2Csource%2Cratebypass%2Ccp&fexp=912302%2C906429%2C913105&itag=43&ip=140.0.0.0&signature=B9811AF3C187D1C938A3B7762CB563895A7179ED.7F50E525FCF19E50EC72C7BFA42F71F08F94D9E6&sver=3&ratebypass=yes&source=youtube&expire=1330387565&key=yt1&ipbits=8&cp=U0hSRVNTVV9LT0NOMl9LTVlGOmhZR1cwOUZseVVa&id=e7bceb9ff3396c4f"
  [7]=>
  string(470) "url=http://o-o.preferred.ascc-tpe1.v22.lscache7.c.youtube.com/videoplayback?sparams=id%2Cexpire%2Cip%2Cipbits%2Citag%2Csource%2Calgorithm%2Cburst%2Cfactor%2Ccp&fexp=912302%2C906429%2C913105&algorithm=throttle-factor&itag=34&ip=140.0.0.0&burst=40&sver=3&signature=C73DA387F4955ACAAE339AAE67677A62B631EC73.A066F4F997AA4C6D1C90BE3FAE005EF2DE18A753&source=youtube&expire=1330387565&key=yt1&ipbits=8&factor=1.25&cp=U0hSRVNTVV9LT0NOMl9LTVlGOmhZR1cwOUZseVVa&id=e7bceb9ff3396c4f"
  [8]=>
  string(421) "url=http://o-o.preferred.ascc-tpe1.v9.lscache7.c.youtube.com/videoplayback?sparams=id%2Cexpire%2Cip%2Cipbits%2Citag%2Csource%2Cratebypass%2Ccp&fexp=912302%2C906429%2C913105&itag=18&ip=140.0.0.0&signature=4C1A34563C1B2A42B1C023A2C620C070AC7DEAB7.0CF6081EA6930AA4EC68FD025F6F98B00F902A11&sver=3&ratebypass=yes&source=youtube&expire=1330387565&key=yt1&ipbits=8&cp=U0hSRVNTVV9LT0NOMl9LTVlGOmhZR1cwOUZseVVa&id=e7bceb9ff3396c4f"
  [9]=>
  string(469) "url=http://o-o.preferred.ascc-tpe1.v12.lscache5.c.youtube.com/videoplayback?sparams=id%2Cexpire%2Cip%2Cipbits%2Citag%2Csource%2Calgorithm%2Cburst%2Cfactor%2Ccp&fexp=912302%2C906429%2C913105&algorithm=throttle-factor&itag=5&ip=140.0.0.0&burst=40&sver=3&signature=D4AB3700B913D6FA884D91AB5A27B7C219DE9912.CF9CB67707CAD08979DF1CFBED4724BC39C78E92&source=youtube&expire=1330387565&key=yt1&ipbits=8&factor=1.25&cp=U0hSRVNTVV9LT0NOMl9LTVlGOmhZR1cwOUZseVVa&id=e7bceb9ff3396c4f"
}

裡面有 ip 這個參數,基本上我試了一下,在 A 這個電腦產生的網址真的沒有辦法在 B 電腦下載(兩邊 IP 不同)

產生出來的連結裡面有個 expire 參數,帶的是 unix time 時間

網址後面帶有 itag 這個參數,這個數值可以提供影片的格式和畫素資訊,請直接看 wiki 的整理:
http://en.wikipedia.org/wiki/YouTube#Quality_and_codecs

$ ffmpeg -i input_name -y -vn -f ogg output_name
$ ./youtube-dl.py --extract-audio --audio-format mp3 http://www.youtube.com/watch?v=57zrn_M5bE8
-y 輸出檔名相同的話直接覆蓋
-vn 只取出音源
-f 輸出格式

另外,上面的 php 程式碼部份,可以選擇取出目前 youtube 上面的影片 webm 格式

ffmpeg 我安裝 0.10 版本才有 webm 的 vp8 格式支援(Ubuntu10.04 server LTS)

sudo apt-get install libfaac-dev libmp3lame-dev

./configuration –enable-pthreads –enable-gpl –enable-libfaac –enable-libmp3lame –enable-nonfree

http://ffmpeg.org/releases/ffmpeg-0.10.tar.gz

試跑的過程中有時候成功有時候失敗,原因不明。
而且學網很慢= =a

<?php
//產生出來的連結不一定第一次點擊就可以使用
//所以網路上的下載方式有些會要求使用者貼上 info 資訊
//目的應該就是先產生第一次點擊
//保證使用者要下載的時候該連結一定可以生效
$file_contents = file_get_contents("http://www.youtube.com/get_video_info?video_id=57zrn_M5bE8");
$file_contents = urldecode(urldecode($file_contents));//至此,產生出來的 http link 可以使用了
preg_match_all('/url=(.+?)id=w{16}/', $file_contents, $p);//$p[0]:包含id ,$p[1]:不含id
$i = 0;
//echo '

preview image

'; foreach( $p[0] as $url ){ $itag = strstr($url, 'itag='); $url = substr($url, strlen("url=")) ; $itag = strstr($itag, '&', true); $itag = strstr($itag, '='); $itag = substr($itag, strlen("=")); $info[$i]["url"] = $url; $info[$i]["itag"] = $itag; $i++; } $max = $info[0]; foreach( $info as $i ){ if( $i["itag"] > $max["itag"] ){ $max = $i; } } //echo $max["itag"]."
n"; //echo $max["url"]."
n"; switch($max["itag"]){ case "46": echo '1080p WebM'; exec('curl "http://www.youtube.com/get_video_info?video_id=57zrn_M5bE8" > /tmp/youtube.tmp'); exec('curl "'.$max["url"].'" > a.webm'); break; case "45": echo '720p WebM'; break; case "44": echo '480p WebM'; break; case "43": echo '360p WebM'; break; case "38": echo '2304p MP4'; break; case "37": echo '1080p MP4'; break; case "22": echo '720p MP4'; break; case "18": echo '360p MP4'; break; case "102": echo '720p WebM 3D'; break; case "101": echo '480p WebM 3D'; break; case "100": echo '360p WebM 3D'; break; case "84": echo '720p MP4 3D'; break; case "85": echo '520p MP4 3D'; break; case "82": echo '360p MP4 3D'; break; case "83": echo '240p MP4 3D'; break; case "35": echo '480p FLV'; break; case "34": echo '360p FLV'; break; case "6": echo '270p FLV'; break; case "5": echo '224p FVL'; break; case "17": echo '3GP'; break; case "13": echo '3GP'; break; default: echo "未知的格式n"; break; } ?>

change php exec shell to bash

當我們用 php 的 exec 指令,想要叫電腦執行某些指令或者 shell script 時候

可能會遇到問題

因為 exce 使用 /bin/sh 作為執行的環境,而 linux 有可能使用 /bin/sh -> /bin/dash 當作 /bin/sh 來使用

而 dash 沒有 source 這種 shell command,bash 才有。

這是第一個問題

第二個問題:當我rm /bin/sh;sudo ln -s /bin/bash /bin/sh

看起來應該可以讓我的 php 的 exec() 使用 bash 作為執行環境了

但是為什麼動作的結果和我自己在登入電腦之後在 bash 環境下執行的結果不一樣?

例如 source .bashrc ,顯示 file not found

在 sh 的情況下是因為在 PATH 裡面搜尋不到檔案的話, sh 不會到當前目錄再進行搜尋

但是 bash 在 PATH 裡面搜尋不到檔案的話, bash 是會到當前目錄進行搜尋的

BUT,當我把 /bin/sh -> /bin/bash 之後,用 exec 呼叫,他似乎不是這樣運作…what the fuxx …

解法:

exec("/bin/bash -c"source .bashrc" ");

這樣,我解決了我的問題。但我依然不知道 exec 執行的時候用 bash 跟我帳號登入機器執行 bash 為什麼會不一樣!

php 傳遞 complextype 參數給 web service ,使用 nusoap lib

下面是我貼在 stackoverflow.com 發問的問題,後來我自己解掉了。

http://stackoverflow.com/questions/8737234/how-php-nusoap-client-transfer-complextype-to-net-soap-server

不想翻譯了…

總之,如果你有「使用 php 傳送 ComplexType 型態的參數到 web service 的需求」的話,那就花個半小時看一下這篇吧

I am trying to create a client soap call using php nusoap to an existing SOAP server build on .NET that contains complexTypes.

I want to call QueryTestCategory(see belows)

And it has param searchTag and Call_Result that are string

But param LGI is type of LoginInfo(struct LoginInfo { string User_Name; string Password; })

So,how do I setup the parameters for QueryTestCategory?

I perform a getTypes I receive the following

struct HelloWorld { }
struct HelloWorldResponse { string HelloWorldResult; }
struct QueryTestSuite { LoginInfo LGI; string searchTag; string Call_Result; }
struct LoginInfo { string User_Name; string Password; }
struct QueryTestSuiteResponse { ArrayOfSuiteInfo QueryTestSuiteResult; string Call_Result; }
struct ArrayOfSuiteInfo { SuiteInfo SuiteInfo; }
struct SuiteInfo { string Test_Suite_ID; string Test_Suite_Name; string Tag; string Owner; string Remark; }
struct QueryTestCategory { LoginInfo LGI; string searchTag; string Call_Result; }
struct QueryTestCategoryResponse { ArrayOfCategoryInfo QueryTestCategoryResult; string Call_Result; }
struct ArrayOfCategoryInfo { CategoryInfo CategoryInfo; }
struct CategoryInfo { string Test_Category_ID; string Test_Category_Name; string Tag; string Owner; string Remark; }
struct QueryTestCase { LoginInfo LGI; string searchTag; string Call_Result; }
struct QueryTestCaseResponse { ArrayOfTestCaseInfo QueryTestCaseResult; string Call_Result; }
struct ArrayOfTestCaseInfo { TestCaseInfo TestCaseInfo; }
struct TestCaseInfo { string Test_Case_ID; string Test_Case_Name; string Test_Case_Version; string Tag; string Entry_Point; string Test_Kit_Name; string Test_Kit_Version; string Extra_Parameter; string Estimate_Time; string Function_Team; string Remark; string Test_Log_Parser_Name; string Test_Log_Parser_Version; string Test_Log_Parser_Entry_Point; string Test_Log_Parser_Extra_Parameter; string Owner; }
struct QueryTestResult { LoginInfo LGI; string TaskID; string Call_Result; }
struct QueryTestResultResponse { string QueryTestResultResult; string Call_Result; }

I try this, but it fails

/*
 *  $Id: wsdlclient1.php,v 1.3 2007/11/06 14:48:48 snichol Exp $
 *
 *  WSDL client sample.
 *
 *  Service: WSDL
 *  Payload: document/literal
 *  Transport: http
 *  Authentication: none
 */
require_once('lib/nusoap.php');
$proxyhost = isset($_POST['proxyhost']) ? $_POST['proxyhost'] : '';
$proxyport = isset($_POST['proxyport']) ? $_POST['proxyport'] : '';
$proxyusername = isset($_POST['proxyusername']) ? $_POST['proxyusername'] : '';
$proxypassword = isset($_POST['proxypassword']) ? $_POST['proxypassword'] : '';
$client = new nusoap_client('http://www.example.com/Query.asmx?WSDL', 'wsdl',
                        $proxyhost, $proxyport, $proxyusername, $proxypassword);
$err = $client->getError();
if ($err) {
    echo "Constructor error";
}
// Doc/lit parameters get wrapped
$param = array('User_Name' => 'SSD_WS_01', 'Password' => 'SSD_WS_01', 'searchTag' => 'test', 'Call_Result' => '');
$result = $client->call('QueryTestCategory', array('parameter' => $param), '', '', false, true);
// Check for a fault
if ($client->fault) {

    print_r($result);

} else {
    // Check for errors
    $err = $client->getError();
    if ($err) {
        // Display the error
        echo 'Error;
    } else {
        // Display the result
        echo 'Result';
        print_r($result);

    }
}

I found ANSWER!
After reading this web page and many many other information

http://gznofeng.iteye.com/blog/951762

My finally code is like below

To sum up…

create $param1 = new stdClass,set parameter via $param1->varName = ‘something’

That all!

 23 // Doc/lit parameters get wrapped
 24 //$param1 = array('User_Name' => 'SSD_WS_01', 'Password' => 'SSD_WS_01');
 25 $param1 = new stdClass();
 26 $param1->User_Name = 'SSD_WS_01';
 27 $param1->Password = 'SSD_WS_01';
 28
 29 $result = $client->call('QueryTestCategory', array('LGI' => $param1, 'searchTag' => '' ), '', '', false, true);
 30 // Check for a fault
 31 if ($client->fault) {
 32     echo '

Fault

'; 33 print_r($result); 34 35 } else { 36 // Check for errors 37 $err = $client->getError(); 38 if ($err) { 39 // Display the error 40 echo '

Error

' . $err; 41 } else { 42 // Display the result 43 echo '

WoW,Result

'; 44 print_r($result); 45 46 } 47 }

補充傳遞 array 的方法,看起來似乎 nusoap 本身沒有支援 arrayType

所以要自己額外把下面這段代碼加入才能支援 arrayType

$server->wsdl->addComplexType("ArrayOfString",
                 "complexType",
                 "array",
                 "",
                 "SOAP-ENC:Array",
                 array(),
                 array(array("ref"=>"SOAP-ENC:arrayType","wsdl:arrayType"=>"xsd:string[]")),
                 "xsd:string");

REF: http://www.linuxquestions.org/questions/programming-9/php-soap-server-returns-array-using-nusoap-library-600470/

然後就可以用下面這樣的陣列了

array("return" => "tns:ArrayOfString"),