如何使用 php curl 重定向来自 api 的发布数据?

Posted

技术标签:

【中文标题】如何使用 php curl 重定向来自 api 的发布数据?【英文标题】:how to redirect with post data from api using php curl? 【发布时间】:2015-10-01 06:00:01 【问题描述】:

实际上,我一直在使用 angularjs 和 restfullapis 进行忘记身份验证部分。我将密钥发送到丢失的用户邮件以获取重置密码面板。用户点击来自mail(http://workless/services/user/reset_pwd_confirm?verification_id=72e03c6aa3268cd37067243945ac69aa&user_id=5)的验证url,如下api调用。

    private function reset_pwd_confirm()
        if($this->get_request_method() != "GET")
            $this->response('',406);
        
        $verification_id = $this->_request['verification_id']; 
        $user_id = $this->_request['user_id']; 
        if(!empty($verification_id) && !empty($user_id) && $user_id > 0)
            $tm=time()-86400;
            $query = "SELECT user_id FROM user_pwd_reset WHERE user_id='".$user_id."' AND act_key='".$verification_id."' AND time > ".$tm."";
            $get_user = $this->db->get_row($query);                
            if($this->db->num_rows == 1)
                $curl = curl_init('http://localhost:3000/#/reset');
                curl_setopt($ch,CURLOPT_POST, 1);
                curl_setopt($ch,CURLOPT_POSTFIELDS, 'user_id='.$user_id);
                curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
                curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "POST");

                curl_exec($curl);               
            else
                $success = array('status' => "Failed", "msg" => "key and user are not match.");
                $this->response($this->json($success),404);
            
        else
            $success = array('status' => "Failed", "msg" => "Invaild verification or user id.");
            $this->response($this->json($success),404);
        

            

在这里我需要移动名为http://localhost:3000/#/reset 的角度页面,并带有post user_id,为此我使用了上面提到的curl,但它返回错误为“Cannot POST /”。 是什么问题?请告诉我。 我认为问题是来自http get方法的请求发布,是吗? 请给我解决方案...

【问题讨论】:

检查此***.com/questions/27670975/… 并发布您的 api 代码,以便我们查看问题所在。 【参考方案1】:

如果我理解正确的话,你想要达到的效果是这样的:

用户单击电子邮件中的链接(即 GET 请求),它应该触发另一个 POST 请求。正确的? 如果可以通过服务器端重定向实现,我会感到惊讶。您可以通过 javascript 在客户端上解决它。

但是:你真的需要一个 POST 请求吗? 您的网址中已经有一个verification_id。所以当用户点击它时,服务器可以重置密码或将用户重定向到“密码重置表单”并使verification_id无效。所以verification_id最多可以使用1次。

这样可以吗?

但如果你真的需要执行那个 POST 请求,这里是如何。 一旦用户单击该链接,它将引导他进入带有类似内容的自定义网页

html:

 <body onload="onLoad()">
  <form style="display:none" method="POST" action="some_url" id="myform">
   <input type="hidden" name="user_id" value="your_user_id">     
  </form>
 </body>

javascript:

 function onLoad() 
   var form = document.getElementById('myform');
   form.submit();
 

【讨论】:

为什么我需要localhost:3000/#/reset 的发布请求是我需要进一步附加带有该用户 ID 的密码并发送发布请求以重置密码。这就是为什么我尝试使用该 url 发送 post user_id 数据。 我不想通过 post 保护 user_id,我只是将 user_id 作为 post 类型发送,以便在 reset api 中获取 user_id 数据,因为我们只能在 reset api 中作为 post 类型数据发送,因为密码数据。 私有函数 reset_pwd() if($this->get_request_method() != "POST") $this->response('',406); $data = json_decode(file_get_contents("php://input"),true); if(!empty($data['user_id'])) $user_pwd = $data['user_pwd']; $user_id = $data['user_id']; if(!empty($user_pwd)) $query = "UPDATE user SET password = '".$data['user_pwd']."' WHERE user_id='".$user_id."'"; /*这是我的重置 api

以上是关于如何使用 php curl 重定向来自 api 的发布数据?的主要内容,如果未能解决你的问题,请参考以下文章

php – cURL从重定向获取url

PHP:CURL 可以跟随元重定向

php 用curl_exec 采集页面内容,结果 302重定向

php curl抓取网页内容在服务器上报302Found,怎么解决

如何确定 URL 是不是在 PHP 中重定向?

php 使用cURL和PHP(加上重定向)使用测量协议进行简单事件跟踪