我有这个执行两个功能的 PHP 脚本:
- 将 prayer_warriors 的 XML 标记中的计数增加 1。
- 向适当的用户发送推送通知。
在我的应用程序中,我可以 POST,脚本的所有功能都得到执行。脚本代码为:
<?php
$first_name = $_POST['first_name'];
$last_name = $_POST['last_name'];
$title = $_POST['title'];
$deviceToken = $_POST['iostoken'];
$xml = simplexml_load_file("/Test.xml") or die("Not loaded!\n");
$responses = $xml->xpath("//channel/item[title = '$title' and first_name = '$first_name' and last_name = '$last_name']/prayer_warriors");
$responses[0][0] = $responses[0] + 1;
echo($responses);
$xml->asXML("Test.xml");
// Put your device token here (without spaces):
// Put your private key's passphrase here:
$passphrase = 'Passphrase';
// Put your alert message here:
$message = 'Someone just pledged to pray for you!';
////////////////////////////////////////////////////////////////////////////////
$ctx = stream_context_create();
stream_context_set_option($ctx, 'ssl', 'local_cert', 'ck.pem');
stream_context_set_option($ctx, 'ssl', 'passphrase', $passphrase);
// Open a connection to the APNS server
$fp = stream_socket_client(
'ssl://gateway.push.apple.com:2195', $err,
$errstr, 60, STREAM_CLIENT_CONNECT|STREAM_CLIENT_PERSISTENT, $ctx);
if (!$fp)
exit("Failed to connect: $err $errstr" . PHP_EOL);
echo 'Connected to APNS' . PHP_EOL;
// Create the payload body
$body['aps'] = array(
'alert' => $message,
'sound' => 'default'
);
// Encode the payload as JSON
$payload = json_encode($body);
// Build the binary notification
$msg = chr(0) . pack('n', 32) . pack('H*', $deviceToken) . pack('n', strlen($payload)) . $payload;
// Send it to the server
$result = fwrite($fp, $msg, strlen($msg));
if (!$result)
echo 'Message not delivered' . PHP_EOL;
else
echo 'Message successfully delivered' . PHP_EOL;
// Close the connection to the server
fclose($fp);
?>
我使用以下代码从网页发布到此 PHP。它会触发发送推送通知的部分,但不会将 prayer_warrior 计数增加 1。
$title = $item->title;
$first_name = $item->first_name;
$last_name = $item->last_name;
$iostoken = $item->iostoken;
Echo "<form action=\"http://Url.php\" method=\"post\">";
Echo "<input type=\"hidden\" name=\"first_name\" value=".$first_name.">";
Echo "<input type=\"hidden\" name=\"last_name\" value=".$last_name.">";
Echo "<input type=\"hidden\" name=\"title\" value=".$title.">";
Echo "<input type=\"hidden\" name=\"iostoken\" value=".$iostoken.">";
Echo "<input type=\"submit\" name=\"submit\" value=\"Pledge to pray\" />";
Echo "</form>";
知道为什么要这样做吗?
最佳答案
您写入的文件似乎与您正在读取的文件不同。
$xml = simplexml_load_file("/Test.xml") or die("Not loaded!\n");
^
尝试改变
$xml->asXML("Test.xml");
到
$xml->asXML("/Test.xml");
在权限阻止您更新文件的情况下使用错误日志记录
if(false === $xml->asXML("/Test.xml")) {
die('Could not update XML!');
}
关于php - 通过 PHP POST 执行 2 个操作的按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23957396/