使用PHP CURL的POST資料

2021-09-06 11:33:36 字數 3593 閱讀 4628

curl 是使用url語法的傳送檔案工具,支援ftp、ftps、http htpps scp sftp tftp telnet dict file和ldap。curl 支援ssl證書、http post、http put 、ftp 上傳,kerberos、基於htt格式的上傳、**、cookie、使用者+口令證明、檔案傳送恢復、http**通道和大量其他有用的技巧。

原來php預設並不進行此項功能的擴充套件,但還是有的,只是沒有讓它生效罷了。開啟php安裝目錄,搜尋以下三個檔案 ssleay32.dll、libeay32.dll和 php_curl.dll,一一拷貝到系統目錄下的system32資料夾下,修改php.ini檔案,找到;extension= php_curl.dll行,去掉前面的;號,儲存,重啟伺服器。

下面舉幾個例子。

短彩信傳送

$xml_data = '<?xml version="1.0" encoding="utf-8" standalone="yes"?>

'.$pns.'

'.$content.'

';$url = '';//接收xml位址

$header = "content-type: text/xml";//定義content-type為xml

$ch = curl_init(); //初始化curl

curl_setopt($ch, curlopt_url, $url);//設定鏈結

curl_setopt($ch, curlopt_returntransfer, 1);//設定是否返回資訊

curl_setopt($ch, curlopt_httpheader, $header);//設定http頭

curl_setopt($ch, curlopt_post, 1);//設定為post方式

curl_setopt($ch, curlopt_postfields, $xml_data);//post資料

$response = curl_exec($ch);//接收返回資訊

if(curl_errno($ch))

curl_close($ch); //關閉curl鏈結

echo $response;//顯示返回資訊

post資料飛信介面

$username = 13800138000;

$password = 123456;

$sendto = 13912345678;

$message = "測試乙個試試看!";

$curlpost = 'username='.urlencode($username).'&

password='.urlencode($password).'&

sendto='.urlencode($sendto).'&

message='.urlencode($message).'';

$ch = curl_init();//初始化curl

curl_setopt($ch,curlopt_url,'');//抓取指定網頁

curl_setopt($ch, curlopt_header, 0);//設定header

curl_setopt($ch, curlopt_returntransfer, 1);//要求結果為字串且輸出到螢幕上

curl_setopt($ch, curlopt_post, 1);//post提交方式

curl_setopt($ch, curlopt_postfields, $curlpost);

$data = curl_exec($ch);//執行curl

curl_close($ch);

print_r($data);//輸出結果

飛信介面模式:?username=您的移動飛信登入手機號,&password=您的移動飛信登入密碼,&sendto=接收簡訊的飛信好友手機號,&message=簡訊內容。

總結一下使用curl方法:

初始化curl

使用curl_setopt設定目標url,和其他選項

curl_exec,執行curl

執行後,關閉curl

最後一步就是輸出

cerl 多執行緒

curl一般用來抓取網頁,第二種就是get或者post資料,第三種應用就是實現php的多執行緒任務。下面來實現多執行緒的:

<?php

/*curl 多執行緒抓取

*//*

* * curl 多執行緒

* * @param array $array 並行**

* @param int $timeout 超時時間

//防止死迴圈耗死cpu 這段是根據網上的寫法

do while ($mrc == curlm_call_multi_perform);//

當正在接受資料時

while ($active and $mrc == curlm_ok)

while ($mrc ==curlm_call_multi_perform);}}

foreach ($array

as$k => $url

)

curl_multi_close(

$mh);

$endtime =getmicrotime();

$diff_time = $endtime - $startime

;

return

array('diff_time'=>$diff_time,

'return'=>$res,

'header'=>$header

); }

//計算當前時間

function

getmicrotime()

//測試一下,curl 三個**

呼叫var_dump($data);//

輸出

?>

因為$active要等全部url資料接受完畢才變成false,所以這裡用到了curl_multi_exec的返回值判斷是否還有資料,當有資料的時候就不停呼叫curl_multi_exec,暫時沒有資料就進入select階段,新資料一來就可以被喚醒繼續執行。這裡的好處就是cpu的無謂消耗沒有了。

這個多執行緒的寫法步驟:

呼叫curl_multi_init

迴圈呼叫curl_multi_add_handle,這一步需要注意的是,curl_multi_add_handle的第二個引數是由curl_init而來的子handle。

持續呼叫curl_multi_exec

根據需要迴圈呼叫curl_multi_getcontent獲取結果

呼叫curl_multi_remove_handle,並為每個字handle呼叫curl_close

呼叫curl_multi_close

撰於:

php curl 傳送post請求

php curl init函式 resource curl init string url null 初始化乙個新的會話,返回乙個curl控制代碼,供curl setopt curl exec 和curl close 函式使用 boolcurl setopt resource ch int opti...

php curl模擬post請求提交資料

摘要 最近在做校園圖書館圖書資訊的採集程式,既然是圖書館圖書的採集,肯定有提交搜尋的頁面,無非是post提交,讓我想到了curl模擬提交,首先通過firebug進行抓包查詢下post提交後的格式如下 txtwxlx cn hidwxlx spancnlx 最近在做校園圖書館圖書資訊的採集程式,既然是...

PHP CURL之模擬POST登陸

curl簡介 curl允許你與各種的伺服器使用各種型別的協議進行連線和通訊,目前支援的協議包括 http https ftp gopher telnet dict file ldap,同時也支援https認證 http post http put ftp 上傳 這個也能通過php的ftp擴充套件完成...