post方法到远程url不能在php中工作

时间:2015-09-29 07:49:51

标签: php http-post

我正在尝试向网址发送帖子请求,但是它说这不是帖子请求。请帮助,这是我的代码。

<?php 

$url = "myurl";
$api = '{"Project-Key" : "string","MailFrom" : "email","From" : "email","To" : "email","Subject" : "Mail Subject","HtmlBody" : "<html><body> <img src="cid:naz.jpg"> </body> </html>"}';

$data = array(
        "authtoken" => "token",
        "scope" => "mail",
        "mailDetails" => $api
);

$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);
$result = json_decode($result, true);

print_r($result);
exit(0);

?>

1 个答案:

答案 0 :(得分:0)

尝试使用cURL:

$curlConfig = [
    CURLOPT_URL            => $url,
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_POST           => true,
    CURLOPT_POSTFIELDS     => $data
];

curl_setopt_array($ch, $curlConfig);
$result = curl_exec($ch);
curl_close($ch);