本文目录一览:
- [能否给我一段curl 豆瓣电影的代码 php](#能否给我一段curl 豆瓣电影的代码 php)
- php,用curl写个post登陆并取回cookies的代码
- 帮看一段PHP代码是否书写正确?
能否给我一段curl 豆瓣电影的代码 php
<?php
$ch = curl_init();
curl_setopt_array($ch, [
CURLOPT_URL => '',
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_SSL_VERIFYPEER => 0
]);
$res = curl_exec($ch);
curl_close($ch);
var_dump($res);
php,用curl写个post登陆并取回cookies的代码
要在文件中保存COOKIE的信息,你的 curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie_file);
中的 $cookie_file
必须是要保存cookie信息的文件名。最关键的一点是,这个文件名必须带绝对路径,否则是不行的。如果文件带上绝对路径的话,会在文件中以Netscape格式保存所有的cookie信息。
还有一个需要说明的点:COOKIE必须指定有效期,如果没有指定有效期,默认的浏览器关闭COOKIE就失效。这种COOKIE信息在内存中存放,不会写入硬盘。
这两个方面你都需要考虑。使用绝对路径,保存一个长效的COOKIE,肯定可以成功!
帮看一段PHP代码是否书写正确?
基本上只需:
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_URL, $url);
第4和第5点不必。
<?php
class HttpCurl {
private $_info, $_body, $_error;
public function __construct() {
if (!function_exists('curl_init')) {
throw new Exception('cURL not enabled!');
}
}
public function get($url) {
$this->request($url);
}
protected function request($url) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_URL, $url);
$this->_body = curl_exec($ch);
$this->_info = curl_getinfo($ch);
$this->_error = curl_error($ch);
curl_close($ch);
}
public function getStatus() {
return $this->_info['http_code'];
}
public function getHeader() {
return $this->_info;
}
public function getBody() {
return $this->_body;
}
public function __destruct() {
}
}
?>