繁体   English   中英

如何通过简单的HTML表单使用php发送CURL代码

[英]how to send CURL code using php via simple html form

我有此CURL代码,我需要使用php将此代码与输入数据一起发送。

CURL代码:

curl -H "Content-Type: application/json" -d '{"apiKey": "43wrxxxxxxxxxxxxxxx", "message":"MessageValue", "title":"TitleValue", "platform":"android", "publicKey": "wxxxx", "live": "false"}' https://push.xxxxx.io/api/send

我需要在代码中添加消息标题 ,然后使用php发送代码。

范例表格:

<html>
<head><title>Test Form</title></head>

<body>
<form action="server.php" method="post">
  Message: <input type="text" name="message"><br>
  Title: <input type="text" name="title"><br>
  <input type="submit" value="Submit">
</form>

</body>

</html>

我如何使用PHP做到这一点?

谢谢!

<?php
if(isset($_POST['Submit']){
$ch = curl_init( 'https://push.xxxxx.io/api/send');
$data=array('api_key'=>'43wrxxxxxxxxxxxxxxx','message'=>$_POST['message']);//You can enter more data
$payload = json_encode( $data );//encode data to json
curl_setopt( $ch, CURLOPT_POSTFIELDS, $payload );
curl_setopt( $ch, CURLOPT_HTTPHEADER, array('Content-Type:application/json'));
# Return response instead of printing.
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );
# Send request.
$result = curl_exec($ch);
curl_close($ch);
# Print response.
}else{
die('error'); // invalid entry
}
?>

这可能是您系统中的server.php文件。您可以通过$ _POST [{name}]访问表单数据。 将其附加所需的API值,然后进行编码并通过curl发送。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM