我无法连接到托管MySQL数据库的Google Cloud PHP服务器。这是我向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"
最后,register.php
这是我的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
语句的位置。任何帮助将非常感激。谢谢!
答案 0 :(得分:0)
你可以做的一件事就是帮助调试(除了检查php错误日志之外)是编写一个写入文本文件的小函数。
function dbg($data){
file_put_contents(__DIR__.'/log.txt',$data.PHP_EOL,FILE_APPEND );
}
/* then call it like: */
dbg($q); /* where $q is the sql */
dbg(print_r($cfg,true)); /* config */
然后通过php代码使用它来查看您在各个阶段获得的数据 - 通过ftp下载或使用浏览器浏览到该文件位置。只是一个想法...
<?php
function dbg($data){
file_put_contents( __DIR__.'/log.txt', $data.PHP_EOL, FILE_APPEND );
}
/* !! assuming `config.inc.php` is available in `register.php` !! */
dbg( print_r( $cfg, true ) );
$conn = mysql_connect(':/cloudsql/<app server url>:us-central1:<database name>',
'root',
''
);
dbg( 'errors: '.mysql_error( $conn ) );
if ( $conn && isset( $_POST["Token"] ) ) {
$_uv_Token=$_POST["Token"];
dbg('POST-Token: '.$_uv_Token);
$q="INSERT INTO users (Token) VALUES ( '$_uv_Token') ON DUPLICATE KEY UPDATE Token = '$_uv_Token';";
dbg('sql: '.$q);
$result = mysqli_query($conn,$q) or die(mysqli_error($conn));
dbg('Query succeeded: '.$result);
if ($result) {
$response["success"] = 1;
$response["message"] = "Inserted successfully created.";
echo json_encode($response);
} else {
$response["success"] = 0;
$response["message"] = "Oops! An error occurred.";
echo json_encode($response);
}
mysql_close( $conn );
}
?>
config.inc.php
中是否包含register.php
?我问,因为您在配置文件中定义了各种设置,然后继续,在register.php
中再次硬化它们。