MySQLデータベースをホストするGoogle Cloud PHPサーバーに接続できません。私のPHPサーバに通知を送る私のコードはここにあります。AndroidアプリからREST APIを呼び出してPHPサーバーをデプロイすることはできませんか?
NotificationInstanceService.java
public class NotificationInstanceService extends FirebaseInstanceIdService {
private static final String TAG = "NotificationInstance";
@Override
public void onTokenRefresh() {
//Getting registration token
String refreshedToken = FirebaseInstanceId.getInstance().getToken();
//Displaying token on logcat
Log.d(TAG, "Refreshed token: " + refreshedToken);
sendRegistrationToServer(refreshedToken);
}
private void sendRegistrationToServer(String token) {
//You can implement this method to store the token on your server
//Not required for current project
OkHttpClient client = new OkHttpClient();
//Create the request body
RequestBody body = new FormBody.Builder().add("Token", token).build();
//Know where to send the request to
Request request = new Request.Builder().url("<app server url>/register.php")
.post(body)
.build();
//Create
try {
client.newCall(request).execute();
} catch (IOException e) {
e.printStackTrace();
}
}
}
これは正しく通過すると、任意のスタックトレースがスローされないようですようです。私は私のPHPサーバーを展開する場合次に、私が作成した以下のファイル:
app.yamlを:
application: <app server url>
service: default
runtime: php55
api_version: 1
version: alpha-001
handlers:
- url: /(.+\.(ico|jpg|png|gif))$
static_files: \1
upload: (.+\.(ico|jpg|png|gif))$
application_readable: true
- url: /(.+\.(htm|html|css|js))$
static_files: \1
upload: (.+\.(htm|html|css|js))$
application_readable: true
- url: /(.+\.php)$
script: \1
login: admin
- url: /.*
script: index.php
login: admin
- url: /.*
script: register.php
login: admin
のconfig.inc.php:
<?php
$cfg['blowfish_secret'] = '<Secret>'; /* YOU MUST FILL IN THIS FOR COOKIE AUTH! */
/*
* Servers configuration
*/
$i = 0;
// Change this to use the project and instance that you've created.
$host = '/cloudsql/<app server url>:us-central1:<database name>-app-php';
$type = 'socket';
/*
* First server
*/
$i++;
/* Authentication type */
$cfg['Servers'][$i]['auth_type'] = 'cookie';
/* Server parameters */
$cfg['Servers'][$i]['socket'] = $host;
$cfg['Servers'][$i]['connect_type'] = $type;
$cfg['Servers'][$i]['compress'] = false;
/* Select mysql if your server does not have mysqli */
$cfg['Servers'][$i]['extension'] = 'mysqli';
$cfg['Servers'][$i]['AllowNoPassword'] = true;
/*
* End of servers configuration
*/
/*
* Directories for saving/loading files from server
*/
$cfg['UploadDir'] = '';
$cfg['SaveDir'] = '';
$cfg['PmaNoRelation_DisableWarning'] = true;
$cfg['ExecTimeLimit'] = 60;
$cfg['CheckConfigurationPermissions'] = false;
// [END all]
のphp.ini:
google_app_engine.enable_functions = "php_uname, getmypid"
最後に、私のPHPスクリプトであるregister.php
は、これらのすべてのファイルの現在のディレクトリにあります:
register.php:私はこれで間違ったつもりどこ見つけることができないよう
<?php
function dbg($data){
file_put_contents(__DIR__.'/log.txt',$data.PHP_EOL,FILE_APPEND);
}
$conn = mysql_connect(':/cloudsql/<app server url>:us-central1:<database name>',
'root', // username
'' // password
);
if (isset($conn) && isset($_POST["Token"])) {
$_uv_Token=$_POST["Token"];
echo $conn;
$q="INSERT INTO users (Token) VALUES ('$_uv_Token') "
." ON DUPLICATE KEY UPDATE Token = '$_uv_Token';";
$result = mysqli_query($conn,$q) or die(mysqli_error($conn));
// check if row inserted or not
if ($result) {
// successfully inserted into database
$response["success"] = 1;
$response["message"] = "Inserted successfully created.";
// echoing JSON response
echo json_encode($response);
} else {
// failed to insert row
$response["success"] = 0;
$response["message"] = "Oops! An error occurred.";
// echoing JSON response
echo json_encode($response);
}
dbg($q); /* where $q is the sql */
dbg(print_r($result,true)); /* config */
mysqli_close($conn);
}
?>
。間違ったデータベースに接続されているかどうか、またはREST呼び出しが何らかの理由でどこかで傍受されているかどうかについては、デバッグしたりエラーログを見つけることができないようです。クライアント側では、NotificationInstanceService.java
で登録トークンがサーバーに送信されているようですが、サーバーはIDまたはトークンを実際に格納しません。アプリケーションサーバーのすべてのURLが正しく設定されていることは確かです。私は$echo
にすべての応答を試みましたが、そのようになっていますが、どこにこれらの$echo
文を見つけることができないようです。どんな助けでも大歓迎です。ありがとう!
あなたの提案を試して、あなたのコードを私の 'register.php'ファイルに入れましたが、ローカルディレクトリにlog.txtが作成されていません。これをどのようにデバッグすることができるか、私が見ることができる他の事柄? – user1871869
PHPエラーログ(アクセス権がある場合)にエラーが表示されることがありますか?私が気付いたのは 'echo $ conn 'です。'〜$ conn'はエラーの原因となるオブジェクトです!また、 'mysql'と' mysqli'関数を混在させています – RamRaider
私はあなたの提案に従おうとしましたが、まだlog.txtに出力がないようです。それは奇妙です、私はindex.phpのようなスクリプトを展開することができるように思える、それらとの相互作用、などserver_databases.phpのようなものと私は自分のPHPスクリプトを取得することはできません... – user1871869