簡體   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