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

问题描述 投票:0回答:1

我今天早些时候问过一个有关如何发出HTTP请求的问题,有人建议我尝试php并给我一段代码进行工作。

$url = 'https://domainname.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-urlencoded\r\n",
        '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文件中,并上传到我的网络服务器上(运行php 5.6),然后尝试运行.php时,我得到了HTTP错误500。我是所有这一切的新手,我什至不确定自己是否做得正确。非常感谢您的帮助。

谢谢。

php http web post
1个回答
0
投票
      $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);
© www.soinside.com 2019 - 2024. All rights reserved.