今回、Unity内でInputFieldからidを取得し、そのidをlocalhostserverに送信します。
そののち既にjsonファイルをphpファイル内で読み込んでおいて、送られてきたidを参照してデータをUnity側に返す、という処理を行おうとしています。
ですが、phpからデータが帰ってこず、またUnity側ではエラーメッセージが出てこないので困っています。
mysqlを使わずにデータの送受信を行おうとしています。
使用versionは
xampp 3.2.2.0
Unity 2017.1f
localhostserver ポート番号80,443
またこのサイトを参考にしました。
https://qiita.com/kemmimilk/items/9da751e88e0b6aefaa62
DBを使わないのでそこを自分なりに変えています
ちなみにphpだけで処理を行うと(idをunityから受け取っていないので)
Notice:Undefined index: id in C:\xampp\htdocs\php\qiita.php on line 6
0
01
と出てきます。
jsondata.json
1{"res": 2 {"blogData":[ 3 { 4 "id":"0001", 5 "title":"サンプル01", 6 "day":[ 7 { 8 "year":"2015", 9 "month":"03", 10 "_day":"31" 11 } 12 ], 13 "author":"fantmsite", 14 "tag":[ 15 "ブログ" 16 ], 17 "report":"ブログ内容サンプル01" 18 }, 19 { 20 "id":"0002", 21 "title":"サンプル02", 22 "day":[ 23 { 24 "year":"2015", 25 "month":"04", 26 "_day":"01" 27 } 28 ], 29 "author":"fantmsite", 30 "tag":[ 31 "ブログ" 32 ], 33 "report":"ブログ内容サンプル02" 34 } 35 ]} 36}
qiita.php
1<?php 2 $url = "http://localhost/php/jsondata.json"; 3 $json = file_get_contents($url); 4 $json = mb_convert_encoding($json,'UTF8','ASCII,JIS,UTF=8,EUC-JP,SJIS-WIN'); 5 $arr = json_decode($json,true); 6 $id = $_POST["id"]; 7 $id_using = (int) $id; 8 9 if($arr === NULL){ 10 return; 11 }else{ 12 $json_count = count($arr["res"]["blogData"]); 13 $bc_id = array(); 14 $bc_title = array(); 15 $bc_write_day_year = array(); 16 $bc_write_day_month = array(); 17 $bc_write_day_day = array(); 18 $bc_author = array(); 19 $bc_tag = array(); 20 $bc_repport = array(); 21 for($i=$json_count-1;$i>=0;$i--){ 22 $bc_id[] = $arr["res"]["blogData"][$i]["id"]; 23 $bc_title[] = $arr["res"]["blogData"][$i]["title"]; 24 $bc_write_day_year[] = $arr["res"]["blogData"][$i]["day"][0]["year"]; 25 $bc_write_day_month[] = $arr["res"]["blogData"][$i]["day"][0]["month"]; 26 $bc_write_day_day[] = $arr["res"]["blogData"][$i]["day"][0]["_day"]; 27 $bc_author[] = $arr["res"]["blogData"][$i]["author"]; 28 $bc_tag[] = $arr["res"]["blogData"][$i]["tag"]; 29 $bc_repport = $arr["res"]["blogData"][$i]["report"]; 30 } 31 $result = $bc_write_day_day[$id_using]; 32 echo $id_using; 33 echo "<br/>"; 34 echo $result; 35 } 36?>
Catch.cs
1using System.Collections; 2using System.Collections.Generic; 3using UnityEngine; 4using UnityEngine.UI; 5public class Catch : MonoBehaviour { 6 public Text ResultText_; 7 public Text InputText_; 8 public string ServerAddress = "http://localhost/php/qiita.php"; 9 // Use this for initialization 10 public void SendSignal_Button_Push() 11 { 12 StartCoroutine("Access"); 13 } 14 15 private IEnumerator Access() 16 { 17 Dictionary<string, string> dic = new Dictionary<string, string>(); 18 dic.Add("id", InputText_.GetComponent<Text>().text); 19 StartCoroutine(Post(ServerAddress, dic)); 20 yield return 0; 21 } 22 23 private IEnumerator Post(string url, Dictionary<string,string> post) 24 { 25 WWWForm form = new WWWForm(); 26 foreach (KeyValuePair<string, string> post_arg in post) 27 { 28 form.AddField(post_arg.Key, post_arg.Value); 29 } 30 WWW www = new WWW(url, form); 31 32 yield return StartCoroutine(CheckTimeOut(www, 3f)); 33 34 if (www.error != null) 35 { 36 Debug.Log("HttpPost NG:" + www.error); 37 }else if(www.isDone){ 38 ResultText_.GetComponent<Text>().text = www.text; 39 } 40 } 41 42 private IEnumerator CheckTimeOut(WWW www,float timeout) 43 { 44 float resultTime = Time.time; 45 while (!www.isDone) 46 { 47 if (Time.time - resultTime < timeout) yield return null; 48 else 49 { 50 Debug.Log("TimeOut"); 51 break; 52 } 53 } 54 yield return null; 55 } 56 void Start () { 57 58 } 59 60 // Update is called once per frame 61 void Update () { 62 63 } 64} 65
追記1
投稿時点からUnityを最新版にアップデートしました。
Unity側のConsoleWindowにはDebugLogが全く表示されません。
よってDebugLog(www.text);でエラーの確認が出来ません。
サーバー側のアクセスログには全く何も表示されず
エラーログには
PHP Warning: Module 'mbstring' already loaded in Unknown on line 0
[Mon Dec 03 11:30:06.549514 2018] [mpm_winnt:notice] [pid 46064:tid 664] AH00354: Child: Starting 150 worker threads.
[Mon Dec 03 22:27:41.598903 2018] [ssl:warn] [pid 42188:tid 680] AH01909: www.example.com:443:0 server certificate does NOT include an ID which matches the server name
[Mon Dec 03 22:27:41.815474 2018] [core:warn] [pid 42188:tid 680] AH00098: pid file C:/xampp/apache/logs/httpd.pid overwritten -- Unclean shutdown of previous Apache run?
[Mon Dec 03 22:27:41.838263 2018] [ssl:warn] [pid 42188:tid 680] AH01909: www.example.com:443:0 server certificate does NOT include an ID which matches the server name
PHP Warning: Module 'mbstring' already loaded in Unknown on line 0
[Mon Dec 03 22:27:42.763834 2018] [mpm_winnt:notice] [pid 42188:tid 680] AH00455: Apache/2.4.37 (Win32) OpenSSL/1.1.1 PHP/7.2.12 configured -- resuming normal operations
[Mon Dec 03 22:27:42.763834 2018] [mpm_winnt:notice] [pid 42188:tid 680] AH00456: Apache Lounge VC15 Server built: Oct 18 2018 17:16:04
[Mon Dec 03 22:27:42.763834 2018] [core:notice] [pid 42188:tid 680] AH00094: Command line: 'c:\xampp\apache\bin\httpd.exe -d C:/xampp/apache'
[Mon Dec 03 22:27:42.797714 2018] [mpm_winnt:notice] [pid 42188:tid 680] AH00418: Parent: Created child process 32152
[Mon Dec 03 22:27:43.798991 2018] [ssl:warn] [pid 32152:tid 736] AH01909: www.example.com:443:0 server certificate does NOT include an ID which matches the server name
[Mon Dec 03 22:27:43.914713 2018] [ssl:warn] [pid 32152:tid 736] AH01909: www.example.com:443:0 server certificate does NOT include an ID which matches the server name
PHP Warning: Module 'mbstring' already loaded in Unknown on line 0
[Mon Dec 03 22:27:44.031367 2018] [mpm_winnt:notice] [pid 32152:tid 736] AH00354: Child: Starting 150 worker threads.
と表示されました。
追記2
Unity側のButtonに異常があるようです。Buttonにスクリプトを付けようとしても選択できません。
具体的にはButtonScriptComponent→NoFunction→MonoBehavior→stringname
と選択することしかできません。
以下はその画像です。(一度 Unity2dで作り治したためcsファイル名が変わっていますが、内容自体は全く一緒です)
追記3
OnClick部分に間違ってCSファイルそのものを選択していたことが原因でした。
正しくはButtonObjectを選択すべきでした。

回答1件
あなたの回答
tips
プレビュー