もとのページは下記のようになっています。
http://example.com/moto.html?fm=woman
【no】1【name】Yamada【】<BR>
【no】2【name】Tanaka【】<BR>
これと同列の位置にphp (http://example.com/api_1.php) を置き、上記の文字をアプリに渡すAPIとしたいと考えています。
まずは、moto.html のデータをjson方式に変換するため、str_replaceを利用して変換します。
【PHP その1】
PHP
1<?php 2 3$crawler="http://example.com/moto.html?fm=woman"; 4 5$homepage = file_get_contents($crawler); 6 7$moto = array("【no】", "【name】", "【】<BR>"); 8$api_1 = array("['no'=>'", "','name'=>'", "'],"); 9 10$newphrase = str_replace($moto, $api_1, $homepage); 11 12echo $newphrase; 13 14?> 15
これで無事 echo $newphrase; の部分が
['no'=>'1','name'=>'Yamada'],['no'=>'2','name'=>'Tanaka'],
とHTML上で表示されました。
一方、下記のようなjson方式で吐き出すPHPがあります。
【PHP その2】
PHP
1<?php 2 3function returnJson($resultArray){ 4 if(array_key_exists('callback', $_GET)){ 5 $json = $_GET['callback'] . "(" . json_encode($resultArray) . ");"; 6 }else{ 7 $json = json_encode($resultArray); 8 } 9 header('Content-Type: text/html; charset=utf-8'); 10 echo $json; 11 exit(0); 12} 13 14$type = $_REQUEST['user_type']; 15 16// ユーザリストの初期化 17$user_list = []; 18// 返却値の初期化 19$result = []; 20 21try { 22 // 値の検証 23 if (empty($type)) { 24 throw new Exception("no type..."); 25 } 26 27 // ユーザリストの作成 28 switch ($type) { 29 case 'a': 30 $user_list = 31[ 32['no'=>'10','name'=>'Yoshida'], 33['no'=>'11','name'=>'Mori'] 34 ]; 35 break; 36 37 default: 38 // 不正な値 39 throw new Exception("Invalid value..."); 40 break; 41 } 42 43 // 返却値の作成 44 $result = [ 45 'result' => 'OK', 46 'users' => $user_list 47 ]; 48} catch (Exception $e) { 49 $result = [ 50 'result' => 'NG', 51 'message' => $e->getMessage() 52 ]; 53} 54 55// JSONでレスポンスを返す 56returnJson($result); 57?>
上記のwebAPIを$type=aで叩くと
[{"no":"11","name":"Yoshida"},{"no":"12","name":"Mori"}]
が返ってきます。
【やりたいこと】
上記の2つを組み合わせて、
http://example.com/moto.html?fm=woman" にある
【no】1【name】Yamada【】<BR>
【no】2【name】Tanaka【】<BR>
を
[{"no":"1","name":"Yamada"},{"no":"2","name":"Tanaka"}]
のJSON方式で吐き出させたいと考えました。
【やってみたこと】
【PHPその2】にある
['no'=>'10','name'=>'Yoshida'],
['no'=>'11','name'=>'Mori']
の部分を
【PHPその1】にある
$newphrase に差し替えるだけで出来そうに感じました。
そこでPHPその1とその2を下記のように合体させました。
PHP
1<?php 2 3$crawler="http://example.com/moto.html"; 4 5$homepage = file_get_contents($crawler); 6 7$moto = array("【no】", "【name】", "【】<BR>"); 8$api_1 = array("['no'=>'", "','name'=>'", "'],"); 9 10$newphrase = str_replace($moto, $api_1, $homepage); 11 12 13function returnJson($resultArray){ 14 if(array_key_exists('callback', $_GET)){ 15 $json = $_GET['callback'] . "(" . json_encode($resultArray) . ");"; 16 }else{ 17 $json = json_encode($resultArray); 18 } 19 header('Content-Type: text/html; charset=utf-8'); 20 echo $json; 21 exit(0); 22} 23 24$type = $_REQUEST['user_type']; 25 26// ユーザリストの初期化 27$user_list = []; 28// 返却値の初期化 29$result = []; 30 31try { 32 // 値の検証 33 if (empty($type)) { 34 throw new Exception("no type..."); 35 } 36 37 // ユーザリストの作成 38 switch ($type) { 39 case 'a': 40 $user_list = 41[ 42$newphrase 43 ]; 44 break; 45 46 default: 47 // 不正な値 48 throw new Exception("Invalid value..."); 49 break; 50 } 51 52 // 返却値の作成 53 $result = [ 54 'result' => 'OK', 55 'users' => $user_list 56 ]; 57} catch (Exception $e) { 58 $result = [ 59 'result' => 'NG', 60 'message' => $e->getMessage() 61 ]; 62} 63 64// JSONでレスポンスを返す 65returnJson($result); 66?>
結果
{"result":"OK","users":[[""]]}
と、"result":"OK"は出るものの中身が吐き出されません。
echo $newphraseだとHTML上に吐き出すことができるのにです。
何が問題なのでしょうか?
どうすれば、$newphrase を 変数
['no'=>'1','name'=>'Yamada'],['no'=>'2','name'=>'Tanaka'],
として読んでくれるようになりますでしょうか?
よろしくお願いいたします。
追記
$newphrase に代入したすぐ後の var_dump を採って追記いただけますか?
kei344様
有難うございます。
$_REQUEST['user_type']の部分を$type = 'a';とリクエストを受け取った形に変形してテストしました。
php
1 2//$type = $_REQUEST['user_type']; 3 4$type = 'a'; 5 6// ユーザリストの初期化 7$user_list = []; 8// 返却値の初期化 9$result = []; 10 11try { 12 // 値の検証 13 if (empty($type)) { 14 throw new Exception("no type..."); 15 } 16 17 18 19 // ユーザリストの作成 20 switch ($type) { 21 case 'a': 22 case 'admin': 23 $user_list = 24 25[ 26$newphrase 27 ]; 28 break; 29 30var_dump($newphrase); 31 32以下同文 33
と記載してみました。
結果
{"result":"OK","users":[{"no":"1","name":"Yamada"},{"no":"2","name":"Tanaka"}]}
となるべきところが
{"result":"OK","users":["\ufeff[{"no":"1","name":"Yamada"},{"no":"2","name":"Tanaka"],"]}
と ["\ufeff 及び "] のへんてこな文字が混じっていることがわかりました。
\ufeffをググるとBOM付でこれが出ると書かれていたのですが、確認したところBOM付で記載してはいません。改行コードも自動判定からLFに変更しましたがやはり変化がありません。
(遅くなりましたが環境はCent OS 6.8です)
当方は今回初めてAPIを作ることになるため、下記をベースに開発しています。
http://qiita.com/busyoumono99/items/9b5ffd35dd521bafce47
ここのサンプルの標準で用意されている下記のcace 'g'を追記し
$_REQUEST['user_type']の部分を$type = 'g';としてテストしました。
php
1 case 'g': 2 case 'guest': 3 $user_list = [ 4 ['name'=>'香取','age'=>14], 5 ['name'=>'草薙','age'=>15], 6 ['name'=>'稲垣','age'=>15], 7 ['name'=>'岡田','age'=>15], 8 ['name'=>'森田','age'=>15], 9 ['name'=>'三宅','age'=>15], 10 ['name'=>'長野','age'=>15], 11 ['name'=>'坂本','age'=>15], 12 ['name'=>'井ノ原','age'=>15] 13 ]; 14 break; 15 default: 16 17
結果
{"result":"OK","users":[{"name":"\u9999\u53d6","age":14},{"name":"\u8349\u8599","age":15},{"name":"\u7a32\u57a3","age":15},{"name":"\u5ca1\u7530","age":15},{"name":"\u68ee\u7530","age":15},{"name":"\u4e09\u5b85","age":15},{"name":"\u9577\u91ce","age":15},{"name":"\u5742\u672c","age":15},{"name":"\u4e95\u30ce\u539f","age":15}]}
と普通にdump出来ます。
上記のテストから$newphraseを代入するとゴミ文字が入ってくることで表示出来なくなっていることがわかりました。
もう一歩だと思いますので何かアドバイスできることがございましたらよろしくお願いいたします。
回答2件
あなたの回答
tips
プレビュー