当前位置:首页 » 编程语言 » phpcurlcode

phpcurlcode

发布时间: 2022-04-28 23:28:53

php使用CURL请求https的微信统一下单接口时报错,同样的代码我在另一台机器上运行是正常的

我也遇到了同样的问题,只要是走微信,偶尔都会请求不到,原来是正常的。今天排查了一天,终于找到了原因所在。

centos原生用的NSS,而不是OpenSSL,curl调用NSS库请求https时偶尔会出现请求不到的情况。

解决方案:
参考网址:网页链接

按步骤完成后记得重启 php-fpm和nginx

② 怎样用php中的curl模拟登陆

/**
* 模拟登录
*/

//初始化变量
$cookie_file = "tmp.cookie";
$login_url = "http://xxx.com/logon.php";
$verify_code_url = "http://xxx.com/verifyCode.php";

echo "正在获取COOKIE...\n";
$curlj = curl_init();
$timeout = 5;
curl_setopt($curl, CURLOPT_URL, $login_url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, $timeout);
curl_setopt($curl,CURLOPT_COOKIEJAR,$cookie_file); //获取COOKIE并存储
$contents = curl_exec($curl);
curl_close($curl);

echo "COOKIE获取完成,正在取验证码...\n";
//取出验证码
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $verify_code_url);
curl_setopt($curl, CURLOPT_COOKIEFILE, $cookie_file);
curl_setopt($curl, CURLOPT_HEADER, 0);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
$img = curl_exec($curl);
curl_close($curl);

$fp = fopen("verifyCode.jpg","w");
fwrite($fp,$img);
fclose($fp);
echo "验证码取出完成,正在休眠,20秒内请把验证码填入code.txt并保存\n";
//停止运行20秒
sleep(20);

echo "休眠完成,开始取验证码...\n";
$code = file_get_contents("code.txt");
echo "验证码成功取出:$code\n";
echo "正在准备模拟登录...\n";

$post = "username=maben&pwd=hahahaha&verifycode=$code";
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_HEADER, false);
curl_setopt($curl, CURLOPT_RETURNTRANSFER,1);
curl_setopt($curl, CURLOPT_POSTFIELDS, $post);
curl_setopt($curl, CURLOPT_COOKIEFILE, $cookie_file);
$result=curl_exec($curl);
curl_close($curl);

//这一块根据自己抓包获取到的网站上的数据来做判断
if(substr_count($result,"登录成功")){
echo "登录成功\n";
}else{
echo "登录失败\n";
exit;
}

③ 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;
}

④ php curl 返回httpcode 为100是什么意思

查看你的$header里的Content-Length,如没数据,应设为0

curl_setopt($ch, CURLOPT_HTTPHEADER, $header);

$header = array('Content-Length: 0',

⑤ php curl的几种用法

总结一下项目中用到curl的几种方式 1. php curl的默认调用方法,get方式访问url $ch = curl_init(); curl_setopt($ch, CURLOPT_HTTPHEADER, $header); //设置http头 curl_setopt($ch, CURLOPT_ENCODING, "gzip" ); //设置为客户端支持gzip压缩 curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 30 ); //设置连接等待时间 curl_setopt($ch, CURLOPT_URL, $url ); curl_exec( $ch ); if ($error = curl_error($ch) ) {//出错处理return -1;}fclose($fp); $curl_code = curl_getinfo($ch, CURLINFO_HTTP_CODE); //获取http返回值 if( $curl_code == 200 ) { //正常访问url}//异常 2. 设置http header支持curl访问lighttpd服务器Java代码$header[]= 'Expect:'; $header[]= 'Expect:'; 3. 设置curl,只获取http header,不获取body:Java代码curl_setopt($ch, CURLOPT_HEADER, 1); curl_setopt($ch, CURLOPT_NOBODY, 1); curl_setopt($ch, CURLOPT_HEADER, 1); curl_setopt($ch, CURLOPT_NOBODY, 1); 或者只获取body:Java代码curl_setopt($ch, CURLOPT_HEADER, 0); // make sure we get the body curl_setopt($ch, CURLOPT_NOBODY, 0); curl_setopt($ch, CURLOPT_HEADER, 0); // make sure we get the body curl_setopt($ch, CURLOPT_NOBODY, 0); 4. 访问虚拟主机,需设置Host $header[]= 'Host: '.$host; 5. 使用post, put, delete等REStful方式访问urlpost:curl_setopt($ch, CURLOPT_POST, 1 ); put, delete: curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "DELETE"); //或者PUT,需要服务器支持这些方法。6. 保存下载内容为文件

⑥ php+curl获取的表单源码,以字符串转换为数组

function sendcheck($url,$code)
{
global $logger;
$ch = curl_init();
if(!$ch)return -1; //设置适当的参数
curl_setopt($ch, CURLOPT_URL , $url);//连接
if(!curl_setopt($ch, CURLOPT_HEADER, 0)) return -2; //发送,设置curl_exec执行结果返回,成功返回获得内容,否则false
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1); //返回值为空
if(!curl_setopt($ch,CURLOPT_TIMEOUT ,30))return -3; //执行curl操作最大时间为 10 s
if(!curl_setopt($ch,CURLOPT_CONNECTTIMEOUT,30))return -4 ; //curl对外连接大时间为 10 s
$result = curl_exec($ch); //访问资源;

//服务器无响应或者网络连接错误处理,重新发送请求信息,最多10次,每次 10 s 间隔
for($i =0 ; $i <= 9; $i++ ){
if(!$result){ //上一次未得到数据
$result = curl_exec($ch); //下一次的数据发送;
}else{
$logger->info("已成功通知");
break;
}
}
if(!$result){
$logger->info("通知失败");
}
curl_close($ch); //关闭curl资源
}

⑦ php curl连接失败后怎样可以重试

//从header中读取httpstatuscode,如果不为200
//假设头部信息存储在$headers中
if($headers['statusCode']!=200){
//retry,andcontinue
}
//从header中读取content-length
//读取整个httpresponse的body部分,判断其长度,假设为$body
$contentLength=$headers['Content-Length'];
$bodyLength=strlen($bodyLength);
if($contentLength!=$bodyLength){
//retryrequest
}

热点内容
编程找点 发布:2025-05-15 20:43:10 浏览:587
php上传临时文件夹 发布:2025-05-15 20:43:00 浏览:657
impala数据库 发布:2025-05-15 20:42:12 浏览:649
android安装插件 发布:2025-05-15 20:41:31 浏览:241
神秘顾客访问 发布:2025-05-15 20:33:39 浏览:298
安卓市场手机版从哪里下载 发布:2025-05-15 20:17:28 浏览:815
幼儿速算法 发布:2025-05-15 20:15:08 浏览:87
best把枪密码多少 发布:2025-05-15 20:13:42 浏览:549
android安装程序 发布:2025-05-15 20:13:20 浏览:560
c语言跳出死循环 发布:2025-05-15 20:06:04 浏览:825