如何使用curl登录网站

时间:2015-06-12 07:14:52

标签: php curl yii remote-login

我已经阅读了很多关于此的文章和SO问题,但我没有成功做到这一点。这里有一些参考refer1 refer2我想要的是通过curl登录网站。我在下面给出了什么。它在本地系统上

此网址为public function actionLoginnew() { $fields = array( 'User'=>array( 'email' => 'xxxxxxx@zzzzzzzzzzz.com', 'password' => 'yyyyyy' ) ); $urltopost="http://192.168.1.31/Eb/projectmanager/login"; $ckfile = tempnam ("/tmp", 'cookiename'); $fields_string = http_build_query($fields); $ch = curl_init ($urltopost); curl_setopt ($ch, CURLOPT_COOKIEJAR, $ckfile); curl_setopt ($ch, CURLOPT_COOKIEFILE, $ckfile); curl_setopt ($ch, CURLOPT_POST, true); curl_setopt ($ch, CURLOPT_POSTFIELDS, $fields_string); curl_setopt ($ch, CURLOPT_RETURNTRANSFER, false); $returndata = curl_exec ($ch); $headers = curl_getinfo($ch, CURLINFO_HEADER_OUT); curl_close ($ch); print_r($headers);die; }

http://192.168.1.31/Eb/projectmanager/

这是我的yii功能,我想登录的网站是蛋糕php。

它向我显示登录已完成。但是,当我点击网址http://192.168.1.31/Eb/projectmanager/login时,它再次将我重定向到登录页面。

public function admin_login() { $this->__login(); $this->render('admin_login'); } private function __login() { $this->layout = 'default'; if (AuthComponent::user()) $this->redirect(array('controller' => 'projects', 'action' => 'index')); if ($this->request->is('post')) { $this->User->set($this->request->data); if ($this->Auth->login()) { echo "logged in"; if ($this->request->data['User']['remember_me'] == 1) { unset($this->request->data['User']['remember_me']); $this->request->data['User']['password'] = $this->Auth->password($this->request->data['User']['password']); $this->Cookie->write('cepp_pauth', $this->request->data['User'], true, '2 weeks'); } if (!AppAuth::is(UserRoles::Admin) && AppConfig::read('System.maintenance') === true) { $this->Session->setFlash(__('The system is in maintenance mode.'), Flash::Error); $this->redirect($this->Auth->logout()); } $this->Session->setFlash(__('You successfully logged in.'), Flash::Success); if (!empty($this->request->params['admin'])) $this->redirect(array('controller' => 'projects', 'action' => 'index')); else $this->redirect($this->Auth->redirectUrl()); } else { unset($this->request->data['User']['password']); $this->Session->setFlash(__('The username/password you provided were incorrect.')); } } $this->set('title_for_layout', __('Login')); }

上的登录脚本
# Netscape HTTP Cookie File
# http://curl.haxx.se/rfc/cookie_spec.html
# This file was generated by libcurl! Edit at your own risk.

#HttpOnly_192.168.1.31  FALSE   /   FALSE   1434106233  CLIENTENGAGE    in7nhb9gd2l24fioso3cq0ol16

生成cookie文件,内容为

import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RequestHeader;

@RequestMapping(method = RequestMethod.POST, produces = "application/json")
public @ResponseBody String getMethod(@RequestHeader(value="json") String headerStr) {
    System.out.println("POST");
    System.out.println(s);
    return "hello";
}

请帮我解决这个问题。谢谢

1 个答案:

答案 0 :(得分:0)

您必须写入的登录脚本 http://192.168.1.31/Eb/projectmanager/login

与传递的凭据匹配,并在成功时设置会话。