Curl\Curl::setCookie PHP Method

setCookie() public method

Set Cookie
public setCookie ( $key, $value )
$key
$value
    public function setCookie($key, $value)
    {
        $name_chars = array();
        foreach (str_split($key) as $name_char) {
            if (!isset($this->rfc2616[$name_char])) {
                $name_chars[] = rawurlencode($name_char);
            } else {
                $name_chars[] = $name_char;
            }
        }
        $value_chars = array();
        foreach (str_split($value) as $value_char) {
            if (!isset($this->rfc6265[$value_char])) {
                $value_chars[] = rawurlencode($value_char);
            } else {
                $value_chars[] = $value_char;
            }
        }
        $this->cookies[implode('', $name_chars)] = implode('', $value_chars);
        $this->setOpt(CURLOPT_COOKIE, implode('; ', array_map(function ($k, $v) {
            return $k . '=' . $v;
        }, array_keys($this->cookies), array_values($this->cookies))));
    }

Usage Example

 /**
  * @author mohuishou<*****@*****.**>
  * @return $this
  * @throws \Exception
  */
 protected function login()
 {
     //判断是否已经登录
     if (!empty($this->_login_cookie)) {
         return $this;
     }
     //设置header伪造来源以及ip
     $ip = rand(1, 233) . '.' . rand(1, 233) . '.' . rand(1, 233) . '.' . rand(1, 233);
     $this->_curl->setHeader("X-Forwarded-For", $ip);
     $this->_curl->setHeader("Referer", 'http://202.115.47.141/login.jsp');
     $param = ["zjh" => $this->_uid, "mm" => $this->_password];
     $this->_curl->post('http://202.115.47.141/loginAction.do', $param);
     if ($this->_curl->error) {
         throw new \Exception('Error: ' . $this->_curl->errorCode . ': ' . $this->_curl->errorMessage, 5001);
     }
     //判断是否登录成功
     $page = $this->_curl->response;
     $page = iconv('GBK', 'UTF-8//IGNORE', $page);
     $rule = ['err' => ['.errorTop', 'text']];
     $err = QueryList::Query($page, $rule)->data;
     if (!empty($err)) {
         throw new \Exception('Error:' . $err[0]['err'], 4011);
     }
     //登录成功之后设置cookie
     $this->_login_cookie = $this->_curl->getResponseCookie("JSESSIONID");
     $this->_curl->setCookie('JSESSIONID', $this->_login_cookie);
     return $this;
 }
All Usage Examples Of Curl\Curl::setCookie