405方法不允许json数据不发布

时间:2017-10-06 11:49:53

标签: php http tcp request

我正在简单地在php中发布详细信息但在将数据从表单发布到API时遇到困难,但它显示405方法不允许错误。请检查下面的代码,让我知道它的问题。我可能会犯一些愚蠢的错误,对不起,如果是这样的话。

<form method="POST" action="#" name="checkout">

First Name <input type="text" name="firstName"/><br/>
Last name <input type="text" name="lastName"/><br/>
Email <input type="text" name="phone"/><br/>
Phone <input type="text" name="gender"/><br/>

<input type="submit" name="submit" value="submit"/>

</form>

</body>

</html>

<?php

if(isset($_POST['submit']))
{
    $name = $_POST['firstName'];
    $lastname = $_POST['lastName'];
    $email = $_POST['email'];
    $phone = $_POST['phone'];

    $url11 = "https://api.rezdy.com/v1/categories/124095/products?apiKey=bb42962c78b34e02b879cc5463cb4648";
    $data['customer'] = array(
    'firstName'=>$name,
    'lastName'=>$lastname,
    'email'=>$phone,
    'phone'=>$gender
    );
    echo "<pre>";
    $json = json_encode($data);

    echo $json;
    echo "<br/>";
    function senddatat($url,$post)
    {
        $ch = curl_init();
        curl_setopt($ch,CURLOPT_CUSTOMREQUEST,"POST");
        curl_setopt($ch,CURLOPT_URL,$url);
        curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
        curl_setopt($ch,CURLOPT_POSTFIELDS,$post);
        curl_setopt($ch,CURLOPT_FOLLOWLOCATION,1);
        curl_setopt($ch, CURLOPT_POST,true); 
        curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json','charset=UTF-8 '));
        $result = curl_exec($ch);
/*echo "<pre>";
var_dump( curl_getinfo($ch) ) . '<br/>';
echo curl_errno($ch) . '<br/>';
echo curl_error($ch) . '<br/>';
        curl_close($ch);*/
    return $result;
    }

    echo " ".senddatat($url11,$json);


}
?>

2 个答案:

答案 0 :(得分:0)

您的表单和发布请求中存在一些错误。当你打电话给ssl验证api时。你需要传递ssl证书。或者您可以暂时将其CURLOPT_SSL_VERIFYPEER停用到FALSE

试试这段代码:

<form method="POST" action="#" name="checkout">

First Name <input type="text" name="firstName"/><br/>
Last name <input type="text" name="lastName"/><br/>
Email <input type="text" name="email"/><br/>
Phone <input type="text" name="phone"/><br/>

<input type="submit" name="submit" value="submit"/>

</form>

</body>

</html>

<?php

if(isset($_POST['submit']))
{
    $name = $_POST['firstName'];
    $lastname = $_POST['lastName'];
    $email = $_POST['email'];
    $phone = $_POST['phone'];

    $url11 = "https://api.rezdy.com/v1/categories/124095/products?apiKey=bb42962c78b34e02b879cc5463cb4648";
    $data['customer'] = array(
    'firstName'=>$name,
    'lastName'=>$lastname,
    'email'=>$email,
    'phone'=>$phone
    );
    echo "<pre>";
    $json = json_encode($data);

    echo $json;
    echo "<br/>";
    function senddatat($url,$post)
    {
        $ch = curl_init();
        curl_setopt($ch,CURLOPT_CUSTOMREQUEST,"POST");
        curl_setopt($ch,CURLOPT_URL,$url);
        curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
        curl_setopt($ch,CURLOPT_POSTFIELDS,$post);
        curl_setopt($ch,CURLOPT_FOLLOWLOCATION,1);
        curl_setopt($ch, CURLOPT_POST,true); 
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json','charset=UTF-8 '));
        $result = curl_exec($ch);
/* echo "<pre>";
var_dump( curl_getinfo($ch) ) . '<br/>';
echo curl_errno($ch) . '<br/>';
echo curl_error($ch) . '<br/>';*/
        curl_close($ch);
    return $result;
    }

    echo " ".senddatat($url11,$json);


}

修改

注意:错误明确告诉Method Not Allowed it表示POST api不允许使用https://api.rezdy.com/v1/categories/124095/products方法,需要参考api doc

答案 1 :(得分:0)

首先检查愚蠢的错误:这是你的领域: firstName,lastName,phone,gender

<input type="text" name="firstName"/><br/>
<input type="text" name="lastName"/><br/>
<input type="text" name="phone"/><br/>
<input type="text" name="gender"/><br/>

以及您在帖子后请求中收到的内容: firstName,lastName,email,phone我不知道您如何在帖子请求中获得email ????

$name = $_POST['firstName'];
$lastname = $_POST['lastName'];
$email = $_POST['email'];
$phone = $_POST['phone'];

更新

<form method="POST" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>" name="checkout">

First Name <input type="text" name="firstName"/><br/>
Last name <input type="text" name="lastName"/><br/>
phone <input type="text" name="phone"/><br/>
gender <input type="text" name="gender"/><br/>

<input type="submit" name="submit" value="submit"/>

</form>

</body>

</html>

<?php

if(isset($_POST['submit']) && !empty($_POST['firstName']) && !empty($_POST['lastName']) && !empty($_POST['phone']) && !empty($_POST['gender']) ){

    $name = $_POST['firstName'];
    $lastname = $_POST['lastName'];
    $phone= $_POST['phone'];
    $gender= $_POST['gender'];

    $url11 = "https://api.rezdy.com/v1/categories/124095/products?apiKey=bb42962c78b34e02b879cc5463cb4648";

    $data['customer'] = array('firstName'=>$name,'lastName'=>$lastname,'email'=>$phone,'phone'=>$gender);

    echo "<pre>";
    $json = json_encode($data);
    echo $json;
    echo "<br/>";

    function senddatat($url,$post)
    {
        $ch = curl_init();
        curl_setopt($ch,CURLOPT_CUSTOMREQUEST,"POST");
        curl_setopt($ch,CURLOPT_URL,$url);
        curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
        curl_setopt($ch,CURLOPT_POSTFIELDS,$post);
        curl_setopt($ch,CURLOPT_FOLLOWLOCATION,1);
        curl_setopt($ch, CURLOPT_POST,true); 
        curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json','charset=UTF-8 '));
        $result = curl_exec($ch);
/*echo "<pre>";
var_dump( curl_getinfo($ch) ) . '<br/>';
echo curl_errno($ch) . '<br/>';
echo curl_error($ch) . '<br/>';
        curl_close($ch);*/
    return $result;
    }

    echo " ".senddatat($url11,$json);


}
?>