javascript - 如何使用php在标头中发送带有参数的post请求
问题描述
我正在尝试使用我的网络应用程序发送短信。我发现 API 可以帮助我做到这一点,但它的文档很差。
我需要的是使用 PHP 发送请求。
最后,我使用 JavaScript 正确发送了请求。现在我想对 PHP 做同样的事情。
var Username="abcdefg"
var password="123456789"
var language="1"
var sender="mysenderid"
var Mobile= mobile
var message= "Dear name would you please give us your feedback about your last order click here "
$.post(`https://smsmisr.com/api/webapi/?Username=${Username}&password=${password}&language=${language}&sender=${sender}&Mobile=${Mobile}&message=${message}`, function (data){
console.log(data);
});
解决方案
在 php 中,您可以执行以下操作:
<?php
$request = new HttpRequest();
$request->setUrl('YOUR_END_POINT e.g. https://www.google.ca');
$request->setMethod(HTTP_METH_POST);
$request->setHeaders(array(
'cache-control' => 'no-cache',
'Content-Type' => 'application/json; charset=utf-8',
'PARAM1' => '990d16da-31d9-4961-a340-e21caf0a13d7',
'PARAM2' => 'no-cache',
'app_version' => '0.0.0.1',
'app_name' => 'YOUR_APP_NAME'
));
$request->setBody('{
"ID" : "80E1CB93-52de-4cd1-abd9-5786e4e616c8",
"PHONE": "+14574544460",
"SIN": "027899876557",
"Auth" : "A",
"other": "othervalue"
}');
try {
$response = $request->send();
echo $response->getBody();
} catch (HttpException $ex) {
echo $ex;
}
在您的情况下,请使用:
'Content-Type' => 'application/x-www-form-urlencoded; charset=utf-8'
推荐阅读
- git - Git + libsecret 抛出“没有 X11 $DISPLAY 无法自动启动 D-Bus”
- android - 媒体编解码器 - YUV_YV12 到 YUV420SP/NV21
- python - 如何与其他用户共享我的 Tkinter 应用程序?
- vba - VBA Powerpoint - 文本框 - 将 WordWrap 转换为回车
- gradle - 在自定义 gradle 插件中,如何添加任务取决于其他插件中定义的任务?
- java - Java 7 支持的 JavaFx 应用程序未在 Mojave 上运行
- javascript - 排名扑克手
- postgresql - Hive 创建数据库,无法实例化 org.apache.hadoop.hive.ql.metadata.SessionHiveMetaStoreClient
- json - 使用颠簸变换从json中提取值
- codenameone - 如何共享指针事件