<?php
const APPID = '';
$text = '6日の東京株式市場で日経平均株価は前日比の下げ幅が一時1000円を超えた。1039円安い2万1642円まで下落する場面があった。';
$to = 'en';
$ch = curl_init('https://api.datamarket.azure.com/Bing/MicrosoftTranslator/v1/Translate?Text=%27'.urlencode($text).'%27&To=%27'.$to.'%27');
curl_setopt($ch, CURLOPT_USERPWD, APPID. ':'.APPID);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($ch);
$result = explode('<d:Text m:type="Edm.String">', $result);
$result = explode('</d:Text>', $result[1]);
$result = $result[0];
echo $text."->".$result;
?>
渋谷109周辺(0.2km)のtweetを取得する
すごいね、テクノロジー
<?php
require 'TwistOAuth/build/TwistOAuth.phar';
$consumer_key = '';
$consumer_secret = '';
$access_token = '';
$access_token_secret = '';
$connection = new TwistOAuth($consumer_key, $consumer_secret, $access_token, $access_token_secret);
$tweets_params = ['q' => 'ラクオリア' ,'count' => '10'];
$tweets = $connection->get('search/tweets', $tweets_params)->statuses;
/* var_dump($tweets); */
$hash_params =['q' => '#6758', 'count' => '10', 'lang'=>'ja'];
$hash = $connection->get('search/tweets', $tweets_params)->statuses;
/* var_dump($hash); */
$users_params = ['screen_name' => '*'];
$users = $connection->get('users/show', $users_params);
/* var_dump($hash); */
$geo_params = ['geocode' => '35.658034,139.701636,0.2km' ,'count' => '10'];
$geo = $connection->get('search/tweets', $geo_params)->statuses;
var_dump($geo);

phpファイルの生成
<?php
$files = file_get_contents('test.php');
$fileName = "kabu".rand(1000000,9999999);
$files = mb_convert_encoding($files, "UTF-8", "AUTO");
$fileName = $fileName. ".php";
$handle = fopen($fileName, 'w');
fwrite($handle, $files);
fclose($handle);
print $fileName. "を生成しました。<br>\n";
?>
kabu6512964.phpを生成しました。
<?php
require_once('query/phpquery/phpQuery/phpQuery.php');
$code = 3928;
$url = 'https://hogehoge.jp?code='.$code;
$html = file_get_contents($url);
$doc = phpQuery::newDocument($html);
$message = $doc[".hogehoge"]->text();
$price = $doc[".hoge"]->text();
?>
<!DOCTYPE html>
<html lang="ja">
<head>
<title><?php echo $code."|".$message;?></title>
</head>
<body>
<b><?php echo "【".$code."】".$message;?></b>
<p>現在の株価:<?php echo $price;?></p>
</body>
</html>
empty()
0か空の時、Trueを返し、それ以外の時はFalseを返す。
<?php
$shortsale = 0;
if(empty($shortsale)){
print "空売り残はありません。";
} else {
print "機関投資家の空売り残があります。";
}
?>
空売り残はありません。
ページング
<?php
function paging($limit,$page,$disp=5){
$page = empty($_GET["page"])? 1: $_GET["page"];
$next = $page + 1;
$prev = $page - 1;
if($page != 1){
print '<a href="?page=' .$prev. '">« 前へ</a>';
}
if($page < $limit){
print '<a href="?page='.$next.'">次へ »</a>';
}
}
$limit = 10;
$page = empty($_GET["page"])? 1:$_GET["page"];
?>
csvデータからPHP・chart.jsで、日経平均株価のチャートを作ってみた
簡単すぎるぞ~
<?php
$data = file('nikkei225.csv', FILE_IGNORE_NEW_LINES);
unset($data[0],$data[-1]);
function cut($item){
return explode(',', $item);
}
$data = array_map("cut", $data);
foreach ($data as $value){
$days[] = $value[0];
$high[] = $value[3];
$low[] = $value[4];
}
/*
$php_day = json_encode($days);
$php_json = json_encode($low);*/
?>
<html>
<head>
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.1.4/Chart.min.js"></script>
</head>
<body>
<p><?php echo $php_json; ?></p>
<canvas id="stage"></canvas>
</body>
<script type="text/javascript">
var array = <?php echo json_encode($days, JSON_HEX_TAG | JSON_HEX_AMP | JSON_HEX_APOS | JSON_HEX_QUOT); ?>;
console.log(array);
var array2 = <?php echo json_encode($high, JSON_HEX_TAG | JSON_HEX_AMP | JSON_HEX_APOS | JSON_HEX_QUOT); ?>;
console.log(array2);
var ctx = document.getElementById("stage");
var myLineChart = new Chart(ctx, {
//グラフの種類
type: 'line',
//データの設定
data: {
//データ項目のラベル
labels: array,
//データセット
datasets: [{
//凡例
label: "日経平均株価",
//背景色
backgroundColor: "rgba(75,192,192,0.4)",
//枠線の色
borderColor: "rgba(75,192,192,1)",
//グラフのデータ
data: array2
}]
},
//オプションの設定
options: {
scales: {
//縦軸の設定
yAxes: [{
ticks: {
//最小値を0にする
beginAtZero: true
}
}]
}
}
});
</script>
</html>

トレーダー関数
“;
foreach ($atrs as $idx => $atr){
$price = $data[idx];
$highest = max($highest, $price[2]);
$losscutLine = round($highest – $atr * ATR_N, 2);
echo implode(“,”,[
$price[0],
$highest,
$price[4],
$losscutLine,
$price[4] < $losscutLine ? '売却':'保持'
]) . "\n";
}
[/php]
phpQueryとexplode
<?php
require_once('phpquery/phpQuery/phpQuery.php');
$html = file_get_contents('');
$doc = phpQuery::newDocument($html);
$message = $doc[".message"]->text();
echo substr_count($message, '下げ');
$text = explode(" ",$message);
print_r($text);
?>

array_map()関数
array_mapは、arr1の各要素にcallback関数を適用した後、その全ての要素を含む配列を返す。
所得税の累進課税でみてみましょう。
<?php
function tax($income){
if($income < 195){
return $income *0.05;
} elseif ($income < 330){
return $income * 0.1 + 9.75;
} elseif ($income< 695) {
return $income * 0.2 + 42.75;
} elseif ($income < 900) {
return $income * 0.23 + 63.6;
} elseif ($income < 1800) {
return $income * 0.33 + 153.6;
} elseif ($income < 4000) {
return $income * 0.40 + 279.6;
} else {
return $income * 0.45 + 479.6;
}
}
$salary = array(603, 577, 404, 401, 661, 1126, 350, 2350, 210);
$pay = array_map("tax", $salary);
print_r($pay)
?>
Array ( [0] => 163.35 [1] => 158.15 [2] => 123.55 [3] => 122.95 [4] => 174.95 [5] => 525.18 [6] => 112.75 [7] => 1219.6 [8] => 30.75 )
配列から要素を削除
array_splice():
-配列を切り取る機能を利用して対象から値を削除する
-1つだけの値の削除や連続する値の削除に向いている
<?php
$target = array('東証1部','東証2部','東証マザーズ','JASDAQ','名証','札証','福証');
$split = array_splice($target, 4, 3);
var_dump($target);
var_dump($split);
?>
array(4) { [0]=> string(10) “東証1部” [1]=> string(10) “東証2部” [2]=> string(18) “東証マザーズ” [3]=> string(6) “JASDAQ” }
array(3) { [0]=> string(6) “名証” [1]=> string(6) “札証” [2]=> string(6) “福証” }
unset();
array_values();
配列を指定して、unsetで削除する
複数の値の削除に向いている
unsetで削除するだけで、indexは変更されない。
<?php
$target = array('東証1部','東証2部','東証マザーズ','JASDAQ','名証','札証','福証');
unset($target[3]);
var_dump($target);
$target = array_values($target); //indexを詰める
var_dump($target);
?>
array(6) { [0]=> string(10) “東証1部” [1]=> string(10) “東証2部” [2]=> string(18) “東証マザーズ” [4]=> string(6) “名証” [5]=> string(6) “札証” [6]=> string(6) “福証” }
array(6) { [0]=> string(10) “東証1部” [1]=> string(10) “東証2部” [2]=> string(18) “東証マザーズ” [3]=> string(6) “名証” [4]=> string(6) “札証” [5]=> string(6) “福証” }
<?php
$portfolio = array(
'みずほフィナンシャルグループ'=> '-1.05',
'三菱UFJフィナンシャル・グループ' => '-1.35',
'野村ホールディングス' => '-2.89',
'富士通' => '-2.77',
'エー・ディー・ワークス' => '+2.27',
'日経ダブルインバース上場投信' => '+1.67',
'東芝' => '-1.54'
);
foreach ($portfolio as $key => $val){
if ($val < 0){
unset($portfolio[$key]);
}
}
array_values($portfolio);
var_dump($portfolio);
?>
array(2) { [“エー・ディー・ワークス”]=> string(5) “+2.27” [“日経ダブルインバース上場投信”]=> string(5) “+1.67” }