Curl\Curl::post PHP Method

post() public method

Post
public post ( $url, $data = [], $follow_303_with_post = false ) : string
$url
$data
$follow_303_with_post If true, will cause 303 redirections to be followed using a POST request (default: false). Notes: - Redirections are only followed if the CURLOPT_FOLLOWLOCATION option is set to true. - According to the HTTP specs (see [1]), a 303 redirection should be followed using the GET method. 301 and 302 must not. - In order to force a 303 redirection to be performed using the same method, the underlying cURL object must be set in a special state (the CURLOPT_CURSTOMREQUEST option must be set to the method to use after the redirection). Due to a limitation of the cURL extension of PHP < 5.5.11 ([2], [3]) and of HHVM, it is not possible to reset this option. Using these PHP engines, it is therefore impossible to restore this behavior on an existing php-curl-class Curl object.
return string [1] https://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.3.2 [2] https://github.com/php/php-src/pull/531 [3] http://php.net/ChangeLog-5.php#5.5.11
    public function post($url, $data = array(), $follow_303_with_post = false)
    {
        if (is_array($url)) {
            $follow_303_with_post = (bool) $data;
            $data = $url;
            $url = $this->baseUrl;
        }
        $this->setUrl($url);
        if ($follow_303_with_post) {
            $this->setOpt(CURLOPT_CUSTOMREQUEST, 'POST');
        } else {
            if (isset($this->options[CURLOPT_CUSTOMREQUEST])) {
                if (version_compare(PHP_VERSION, '5.5.11') < 0 || defined('HHVM_VERSION')) {
                    trigger_error('Due to technical limitations of PHP <= 5.5.11 and HHVM, it is not possible to ' . 'perform a post-redirect-get request using a php-curl-class Curl object that ' . 'has already been used to perform other types of requests. Either use a new ' . 'php-curl-class Curl object or upgrade your PHP engine.', E_USER_ERROR);
                } else {
                    $this->setOpt(CURLOPT_CUSTOMREQUEST, null);
                }
            }
        }
        $this->setOpt(CURLOPT_POST, true);
        $this->setOpt(CURLOPT_POSTFIELDS, $this->buildPostData($data));
        return $this->exec();
    }

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::post