尝试使用 php 发送 POST 请求,无论我做什么我都会得到"HTTP ERROR 500"



为了发出HTTP请求,有人建议我尝试使用PHP,并给了我一段代码:

$url = 'https://example.com/dashboard/api';
$data = array('to' => PHONE_NUMBER, 'from' => SENDER_ID, 'message' => TEXT, 'email' => EMAIL, 'api_secret' => SECRET, 'unicode' => BOOLEAN, 'id' => IDENTIFIER);
$options = array(
'http' => array(
'header'  => "Content-type: application/x-www-form-urlencodedrn",
'method'  => 'POST',
'content' => http_build_query($data)
)
);
$context  = stream_context_create($options);
$result = file_get_contents($url, false, $context);
if ($result === FALSE) { /* Handle error */ }
var_dump($result);

因此,我获取了代码,编辑了需要的字段,将其粘贴到.php文件中,上传到我的web服务器上(运行PHP 5.6(,然后在尝试运行.php文件时,我得到了HTTP ERROR 500。

我对这一切都是个新手,我甚至不确定自己是否做对了每件事。

$url = 'https://domainname.com/dashboard/api';
$params = array('to' => PHONE_NUMBER, 'from' => SENDER_ID, 'message' => TEXT, 'email' => EMAIL, 'api_secret' => SECRET, 'unicode' => BOOLEAN, 'id' => IDENTIFIER);      
$query_content = http_build_query($params);
$context = stream_context_create([
'http' => [
'header'  => [
'Content-type: application/x-www-form-urlencoded',
'Content-Length: ' . strlen($query_content)
],
'method'  => 'POST',
'content' => $query_content
]
]);
$result = file_get_contents($url, false, $context);
$url = 'https://domainname.com/dashboard/api';
$header = [
'Content-type: application/x-www-form-urlencoded',
];
$params = array('to' => PHONE_NUMBER, 'from' => SENDER_ID, 'message' => TEXT, 'email' => EMAIL, 'api_secret' => SECRET, 'unicode' => BOOLEAN, 'id' => IDENTIFIER);
$c = curl_init();
curl_setopt($c, CURLOPT_URL,$url);
curl_setopt($c, CURLOPT_POST, true);
curl_setopt($c, CURLOPT_POSTFIELDS, $params);
curl_setopt($c, CURLOPT_RETURNTRANSFER, true);
curl_setopt($c, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($c, CURLOPT_HTTPHEADER, $header);
curl_setopt($c, CURLOPT_SSL_VERIFYPEER, false);
$res = curl_exec($c);
var_dump($res);

最新更新