A. php curl 无法将url中的内容获取到变量之中
functioncurl_get_contents($url){
$curl=curl_init();
//设置你需要抓取的URL
curl_setopt($curl,CURLOPT_URL,$url);
//设置header
curl_setopt($curl,CURLOPT_HEADER,1);
//设置cURL参数,要求结果保存到字符串中还是输出到屏幕上。
curl_setopt($curl,CURLOPT_RETURNTRANSFER,1);
//运行cURL,请求网页
$data=curl_exec($curl);
//关闭URL请求
curl_close($curl);
return$data
}
B. PHP的curl模块和python的pycurl模块的区别
C的curl:
#include <stdio.h>
#include <curl/curl.h>
int main(void)
{
CURL *curl;
CURLcode res;
curl = curl_easy_init();
if(curl) {
/* First set the URL that is about to receive our POST. This URL can
just as well be a https:// URL if that is what should receive the
data. */
curl_easy_setopt(curl, CURLOPT_URL, "http://postit.example.com/moo.cgi");
/* Now specify the POST data */
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, "name=daniel&project=curl");
/* Perform the request, res will get the return code */
res = curl_easy_perform(curl);
/* always cleanup */
curl_easy_cleanup(curl);
}
return ;
}
php的curl:
<?php
$c = curl_init();
curl_setopt($c, CURLOPT_URL, 'http://www..com');
$data = curl_exec($c);
curl_close($c);
echo $c;
?>
python的pycurl:
import pycurl
def body(buffer):
print buffer
c = pycurl.Curl()
c.setopt(pycurl.URL, "http://www..com/")
c.setopt(pycurl.WRITEFUNCTION, body)
c.perform()
主要原理:
C:
使用到的数据结构:
typedef void CURL; /*当初始化什么的时候只是一个void类型*/
struct SessionHandle {
struct Names dns;
struct Curl_multi *multi; /* 用于多线程处理*/
struct Curl_one_easy *multi_pos; /* if non-NULL, points to the its position
in multi controlling structure to assist
in removal. */
struct Curl_share *share; /* Share, handles global variable mutexing */
struct HandleData reqdata; /* Request-specific data */
struct UserDefined set; /* values set by the libcurl user ,用于setopt等*/
struct DynamicStatic change; /* possibly modified userdefined data */
struct CookieInfo *cookies; /* the cookies, read from files and servers */
struct Progress progress; /* for all the progress meter data */
struct UrlState state; /* struct for fields used for state info and
other dynamic purposes */
struct PureInfo info; /* stats, reports and info data */
#if defined(CURL_DOES_CONVERSIONS) && defined(HAVE_ICONV)
iconv_t outbound_cd; /* for translating to the network encoding */
iconv_t inbound_cd; /* for translating from the network encoding */
iconv_t utf8_cd; /* for translating to UTF8 */
#endif /* CURL_DOES_CONVERSIONS && HAVE_ICONV */
unsigned int magic; /* set to a CURLEASY_MAGIC_NUMBER */
};
struct UserDefined {
FILE *err; /* the stderr user data goes here */
void *debugdata; /* the data that will be passed to fdebug */
char *errorbuffer; /* (Static) store failure messages in here */
long proxyport; /* If non-zero, use this port number by default. If the
proxy string features a ":[port]" that one will override
this. */
/**一下省略10000行- -**/
};
使用的方法1:
.初始化curl,得到sessionhandler结构体空间
CURL *curl_easy_init(void)
{
CURLcode res;
struct SessionHandle *data;
/* Make sure we inited the global SSL stuff */
if (!initialized) {
res = curl_global_init(CURL_GLOBAL_DEFAULT);
if(res) {
/* something in the global init failed, return nothing */
DEBUGF(fprintf(stderr, "Error: curl_global_init failedn"));
return NULL;
}
}
/* We use curl_open() with undefined URL so far */
res = Curl_open(&data);
if(res != CURLE_OK) {
DEBUGF(fprintf(stderr, "Error: Curl_open failedn"));
return NULL;
}
return data;
}
C. curl命令在PHP中怎么用
这是我项目中一直在用的一个方法
<?php
functioncurl($url,$params=array(),$header=array(),$timeout=180){
if(empty($url))return$url;
$curl=curl_init();
curl_setopt($curl,CURLOPT_URL,$url);//请求url地址
curl_setopt($curl,CURLOPT_HTTPHEADER,$header);//构造IP
if(!empty($params)&&count($params)>0){
curl_setopt($curl,CURLOPT_POST,true);
//RequestPayload格式数据
if(isset($params['is_json'])&&$params['is_json']===true){
unset($params['is_json']);
$params=json_encode($params);
}else{
$params=http_build_query($params);
}
curl_setopt($curl,CURLOPT_POSTFIELDS,$params);
}
//curl_setopt($curl,CURLOPT_HEADER,true);//是否返回响应头信息
curl_setopt($curl,CURLOPT_RETURNTRANSFER,true);//是否将结果返回
curl_setopt($curl,CURLOPT_FOLLOWLOCATION,true);//是否重定向
//curl_setopt($curl,CURLOPT_USERAGENT,'Mozilla/5.0(WindowsNT6.1;WOW64)AppleWebKit/537.36(KHTML,likeGecko)Chrome/51.0.2704.106Safari/537.36');
curl_setopt($curl,CURLOPT_SSL_VERIFYPEER,false);//只信任CA颁布的证书
//curl_setopt($curl,CURLOPT_CAINFO,$cacert);//CA根证书(用来验证的网站证书是否是CA颁布)
//curl_setopt($curl,CURLOPT_SSL_VERIFYHOST,2);//检查证书中是否设置域名,并且是否与提供的主机名匹配
//从证书中检查SSL加密算法是否存在
curl_setopt($curl,CURLOPT_SSL_VERIFYHOST,FALSE);
curl_setopt($curl,CURLOPT_HTTP_VERSION,CURL_HTTP_VERSION_1_0);
//curl_setopt($curl,CURLOPT_HTTPHEADER,array("Expect:"));
curl_setopt($curl,CURLOPT_IPRESOLVE,CURL_IPRESOLVE_V4);
curl_setopt($curl,CURLOPT_CONNECTTIMEOUT,$timeout);//用来告诉PHP脚本在成功连接服务器前等待多久(连接成功之后就会开始缓冲输出),这个参数是为了应对目标服务器的过载,下线,或者崩溃等可能状况;
curl_setopt($curl,CURLOPT_TIMEOUT,$timeout);//用来告诉成功PHP脚本,从服务器接收缓冲完成前需要等待多长时间。如果目标是个巨大的文件,生成内容速度过慢或者链路速度过慢,这个参数就会很有用。
//自动设置Referer
curl_setopt($curl,CURLOPT_AUTOREFERER,1);
//curl_setopt($curl,CURLOPT_COOKIEJAR,"D:phpStudyWWWcjcooBE66.tmp");////写入cookie信息
//setcookie('cookie_jar',$cookie_jar);//保存cookie路径
$data=curl_exec($curl);//执行
curl_close($curl);
return$data;
}
?>
D. 使用php curl 模拟post请求,自动附加了data参数
$post_data_string=http_build_query($post_data,'&');
$ch=curl_init();
curl_setopt($ch,CURLOPT_URL,$get_session_url);
curl_setopt($ch,CURLOPT_POST,1);
curl_setopt($ch,CURLOPT_POSTFIELDS,$post_data_string);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
$xmloutput=curl_exec($ch);
一般这样写 你自己对比下
E. 请问php高手一个关于curl的问题
$szUrl = "url";
$UserAgent = 'Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.0; SLCC1; .NET CLR 2.0.50727; .NET CLR 3.0.04506; .NET CLR 3.5.21022; .NET CLR 1.0.3705; .NET CLR 1.1.4322)';
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $szUrl);
curl_setopt($curl, CURLOPT_HEADER, 0); //0表示不输出Header,1表示输出
curl_setopt($curl, CURLOPT_USERAGENT, $UserAgent);
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($curl,CURLOPT_RETURNTRANSFER,TRUE);
$data = curl_exec($curl);
echo htmlx($data);
function htmlx($str){ $str = str_replace(">", str_replace(' ','',"& gt;"), $str);
$str = str_replace("<", str_replace(' ','',"& lt;"), $str);
$str = str_replace("\"", str_replace(' ','',"& quot;"), $str);
$str = str_replace(" ", str_replace(' ','',"& nbsp;"), $str);return $str;}
exit();
F. php curl 如何接收
<?php
/*这是个curlget方法实例,$url是需要获取的地址*/
$url="http://www.hao123.com/";
$ch=curl_init();
curl_setopt($ch,CURLOPT_URL,$url);
curl_setopt($ch,CURLOPT_HEADER,0);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
$data=curl_exec($ch);//讲上面url页面的源代码获取并保存到$data这个变量中
curl_close($ch);
echo$data;
?>
G. php curl 取不到数据 帮我看下 是为什么
有以下几种可能:
1、服务器端确实没有数据返回;
2、curl写错了;
3、试试下面这个,我在用的
functioncurl($url,$post='POST',$data=array()){
$ch=curl_init();
$headers[]="Accept-Charset:utf-8";
curl_setopt($ch,CURLOPT_URL,$url);
curl_setopt($ch,CURLOPT_CUSTOMREQUEST,$post);
curl_setopt($ch,CURLOPT_POSTFIELDS,$data);
curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,FALSE);
curl_setopt($ch,CURLOPT_SSL_VERIFYHOST,FALSE);
curl_setopt($ch,CURLOPT_SSLVERSION,1);
curl_setopt($ch,CURLOPT_HTTPHEADER,$headers);
curl_setopt($ch,CURLOPT_USERAGENT,'Mozilla/5.0(compatible;MSIE5.01;WindowsNT5.0)');
curl_setopt($ch,CURLOPT_FOLLOWLOCATION,1);
curl_setopt($ch,CURLOPT_AUTOREFERER,1);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
$result=curl_exec($ch);
curl_close($ch);
returnjson_decode($result,1);
}
H. PHPcurl模拟登陆中的$data和$url应该如何找
chrome的话直接右键有复制curl内容
支予$data和$url两个变量以一边经验来说 $url就是POST请求的url地址,$data就是POST的内容。
I. php curl如何直接转发当前php接收的headersget请求如何直接转发get参数post请求如何直接转发post参数
本文实例讲述了php使用CURL模拟GET与POST向微信接口提交及获取数据的方法。分享给大家供大家参考,具体如下:
php CURL函数可以模仿用户进行一些操作,如我们可以模仿用户提交数据也可以模仿用户进行网站访问了,下面我们来介绍利用CURL模拟进行微信接口的GET与POST例子,例子非常的简单就两个:
Get提交获取数据
/**
* @desc 获取access_token
* @return String access_token
*/
function getAccessToken(){
$AppId = '1232assad13213123';
$AppSecret = '2312312321adss3123213';
$getUrl = 'htq.com/cgi-bin/token?grant_type=client_credential&appid='.$AppId.'&secret='.$AppSecret;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $getUrl);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURL_SSLVERSION_SSL, 2);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
$data = curl_exec($ch);
$response = json_decode($data);
return $response->access_token;
}
post提交获取数据
/**
* @desc 实现天气内容回复
*/
public function testWeixin(){
$access_token = $this->getAccessToken();
$customMessageSendUrl = 'ht.qq.com/cgi-bin/message/custom/send?access_token='.$access_token;
$description = '今天天气的详细信息(从第三方获取)。';
$url = ttpr.com/';
$picurl = 'her.com/';
$postDataArr = array(
'touser'=>'OPENID',
'msgtype'=>'news',
'news'=>array(
'articles'=>array(
'title'=>'当天天气',
'description'=>$description,
'url'=>$url,
'picurl'=>$picurl,
),
),
);
$postJosnData = json_encode($postDataArr);
$ch = curl_init($customMessageSendUrl);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postJosnData);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
$data = curl_exec($ch);
var_mp($data);
}
例子相对来说比较简单也没有什么好详细分析的了,大家照抄就可以实现我们想要的功能了.
J. PHP_CURL求开启办法!!!
配置php支持curl
curl是一个利用URL语法在命令行方式下工作的文件传输工具。它支持很多协议:FTP, FTPS, HTTP, HTTPS, GOPHER, TELNET, DICT, FILE 以及 LDAP。curl同样支持HTTPS认证,HTTP POST方法, HTTP PUT方法, FTP上传, kerberos认证, HTTP上传, 代理服务器, cookies, 用户名/密码认证, 下载文件断点续传, 上载文件断点续传, http代理服务器管道( proxy tunneling), 甚至它还支持IPv6, socks5代理服务器, 通过http代理服务器上传文件到FTP服务器等等,功能十分强大。Windows操作系统下的网络蚂蚁,网际快车(FlashGet)的功能它都可以做到。准确的说,curl支持文件的上传和下载,所以是一个综合传输工具,但是按照传统,用户习惯称curl为下载工具。
配置方法:
1、拷贝PHP目录中的libeay32.dll 和 ssleay32.dll 两个文件到 system32 目录。
2、修改php.ini:配置好 extension_dir ,去掉 extension = php_curl.dll 前面的分号。
---------------------------
php下扩展php_curl.dll的安装
---------------------------
已经内置有php_curl.dll,在ext目录下,此DLL用于支持SSL和zlib.
在php.ini中找到有extension=php_curl.dll, 去掉前面的注释.
设置extension_dir=c:phpext, 刷新PHP页面时报错, 说找不到模块php_curl.dll.
拷贝php_curl.dll 到windowssystem32,还是同样的错.
在网上找了一下,需要将:
libeay32.dll, ssleay32.dll, php5ts.dll, php_curl.dll
都拷贝到system32目录下,重启IIS即可.