简体   繁体   English

如何使用php curl从api重定向与发布数据?

[英]how to redirect with post data from api using php curl?

Actually i have been doing forgot authentication part using angularjs and restfullapis.i sent the key to the lost user mail for getting reset password panel. 实际上,我一直在使用angularjs和restfullapis.i进行忘记身份验证的部分。我已将密钥发送到丢失的用户邮件中,以获取重置密码面板。 The user click on the verification url from mail( http://workless/services/user/reset_pwd_confirm?verification_id=72e03c6aa3268cd37067243945ac69aa&user_id=5 ) the following apis calling. 用户从邮件( http:// workless / services / user / reset_pwd_confirm?verification_id = 72e03c6aa3268cd37067243945ac69aa&user_id = 5 )上单击以下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);
        }

    }        

here i need to move the angular page called as http://localhost:3000/#/reset with post user_id, for this purpose i used curl as i mentioned above But it return error as "Cannot POST /" . 在这里,我需要使用post user_id移动名为http:// localhost:3000 /#/ reset的角度页面,为此目的,我使用了我上面提到的curl,但是它返回错误为“ Cannot POST /” what was the problem?? 有什么问题吗? please tell me. 请告诉我。 i thought problem is request post from http get method,is it? 我认为问题是来自http get方法的请求发布,是吗? please give me the solutions... 请给我解决方案...

If I understood it correctly than what you want to achieve is this: 如果我对它的理解比您要实现的要正确,那就是:

User clicks a link in email (that is GET request), and it should trigger another POST request. 用户单击电子邮件中的链接(即GET请求),它将触发另一个POST请求。 Right? 对? I would be surprised if that was achievable via server-side redirect. 如果通过服务器端重定向可以实现这一点,我会感到惊讶。 You can workaround it on client via javascript. 您可以通过javascript在客户端上解决它。

but: Do you really need a POST request for it? 但是:您是否真的需要POST请求? You already have a verification_id in the url. 网址中已经有一个verification_id So when user clicks it, server can reset password or redirect user to "password reset form" and invalidate the verification_id . 因此,当用户单击它时,服务器可以重设密码或将用户重定向到“密码重设表单”,并使verification_id无效。 So verification_id can be used max 1 time. 因此, verification_id最多可以使用1次。

would that be ok? 可以吗?

But if you REALLY need to perform that POST request, here is how. 但是,如果您确实需要执行该POST请求,则方法如下。 Once user clicks that link, it will lead him to custom web page with something like this 用户单击该链接后,它将带他到这样的自定义网页

html: 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: JavaScript的:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM