繁体   English   中英

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

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

实际上,我一直在使用angularjs和restfullapis.i进行忘记身份验证的部分。我已将密钥发送到丢失的用户邮件中,以获取重置密码面板。 用户从邮件( 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);
        }

    }        

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

如果我对它的理解比您要实现的要正确,那就是:

用户单击电子邮件中的链接(即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();
 }

暂无
暂无

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

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