首页 > 解决方案 > 如何使用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);
});

标签: javascriptphpapipost

解决方案


在 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'

推荐阅读