當前位置:首頁 » 編程語言 » 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
}

熱點內容
best把槍密碼多少 發布:2025-05-15 20:13:42 瀏覽:547
android安裝程序 發布:2025-05-15 20:13:20 瀏覽:558
c語言跳出死循環 發布:2025-05-15 20:06:04 瀏覽:823
a19處理器相當於安卓哪個水平 發布:2025-05-15 20:05:29 瀏覽:638
榮耀9i安卓強行關機按哪個鍵 發布:2025-05-15 20:00:32 瀏覽:750
密碼鎖寫什麼最好 發布:2025-05-15 19:05:31 瀏覽:782
5的源碼是 發布:2025-05-15 19:04:07 瀏覽:719
c語言創建的源文件 發布:2025-05-15 18:54:08 瀏覽:611
3個數字密碼鎖有多少種 發布:2025-05-15 18:49:48 瀏覽:684
壓縮包手機打開 發布:2025-05-15 18:37:34 瀏覽:217