简体   繁体   中英

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. 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.

    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 /" . what was the problem?? please tell me. i thought problem is request post from http get method,is it? 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. Right? I would be surprised if that was achievable via server-side redirect. You can workaround it on client via javascript.

but: Do you really need a POST request for it? You already have a verification_id in the url. So when user clicks it, server can reset password or redirect user to "password reset form" and invalidate the verification_id . So verification_id can be used max 1 time.

would that be ok?

But if you REALLY need to perform that POST request, here is how. Once user clicks that link, it will lead him to custom web page with something like this

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();
 }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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