调用PHP函数时出现PHP 500内部服务器错误
我最近遇到了一个问题,我在我的php文件中创建了一个函数,我试图从同一个文件调用该函数,但当我调用它时,我只得到500个内部服务器错误调用PHP函数时出现PHP 500内部服务器错误,php,chrome-gcm,Php,Chrome Gcm,我最近遇到了一个问题,我在我的php文件中创建了一个函数,我试图从同一个文件调用该函数,但当我调用它时,我只得到500个内部服务器错误 function messageThem($match, $message) { if(isset($match) & isset($message)) { $query = "SELECT gcm FROM gcm_users"; $result = $con->query($query, MYSQLI_USE_RESULT); $regI
function messageThem($match, $message) {
if(isset($match) & isset($message)) {
$query = "SELECT gcm FROM gcm_users";
$result = $con->query($query, MYSQLI_USE_RESULT);
$regID = array();
while($r = mysqli_fetch_array($result)) {
$regID[] = $r["gcm"];
}
$url = "https://gcm-http.googleapis.com/gcm/send";
$api = "REMOVED. API KEY MAY NOT BE SHARED";
$data = array('match' => $_POST['match'], 'message' => $_POST['message']);
$fields = array('registration_ids' => $regID,'data' => $data);
$headers = array('Authorization: key=' . $api,
'Content-Type: application/json');
printf("np bro");
//curl connection
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true );
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($fields));
$resultCurl = curl_exec($ch);
if ( curl_errno( $ch ))
{
echo 'GCM error: ' . curl_error( $ch );
}
curl_close($ch);
echo $resultCurl;
}
}
messageThem("1200","message goes here");
我从中得到的唯一结果是内部服务器错误。但是如果我删除了这个函数,只运行代码,它就可以完美地工作。因此,当我想调用它时,它会给我和内部服务器错误。引发错误,因为未定义
$con
变量,并且您对其调用查询
方法-这肯定是一个范围问题。
function messageThem($match, $message) {
if(isset($match) & isset($message)) {
$query = "SELECT gcm FROM gcm_users";
$result = $con->query($query, MYSQLI_USE_RESULT);
$regID = array();
while($r = mysqli_fetch_array($result)) {
$regID[] = $r["gcm"];
}
$url = "https://gcm-http.googleapis.com/gcm/send";
$api = "REMOVED. API KEY MAY NOT BE SHARED";
$data = array('match' => $_POST['match'], 'message' => $_POST['message']);
$fields = array('registration_ids' => $regID,'data' => $data);
$headers = array('Authorization: key=' . $api,
'Content-Type: application/json');
printf("np bro");
//curl connection
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true );
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($fields));
$resultCurl = curl_exec($ch);
if ( curl_errno( $ch ))
{
echo 'GCM error: ' . curl_error( $ch );
}
curl_close($ch);
echo $resultCurl;
}
}
messageThem("1200","message goes here");
可能是您出错了:
致命错误:对非对象调用成员函数query()
因此有两种解决方案:
global$con代码>
或者必须将$con
作为第三个参数传递给函数
我假设,$con
是这里db连接的对象。您可能需要在函数中访问$con
,这是一个范围问题。peek httpd的错误日志