PHP Conference Japan 2024

客戶端 URL 函式庫

新增註解

使用者貢獻的註解 10 則註解

24
frank at interactinet dot com
13 年前
我撰寫了以下程式碼,以檢查提交的 URL 是否具有有效的 http 回應代碼,以及是否快速回應。

像這樣使用程式碼

<?php
$is_ok
= http_response($url); // 僅在 http 回應代碼 < 400 時傳回 true
?>

第二個參數是可選的,它允許您檢查特定的回應代碼

<?php
http_response
($url,'400'); // 如果 http 狀態為 400,則傳回 true
?>

第三個允許您指定願意等待回應的時間長度。

<?php
http_response
($url,'200',3); // 如果回應時間少於 3 秒且回應代碼為 200,則傳回 true
?>

<?php
function http_response($url, $status = null, $wait = 3)
{
$time = microtime(true);
$expire = $time + $wait;

// 我們 fork 這個程序,這樣就不需要等待逾時
$pid = pcntl_fork();
if (
$pid == -1) {
die(
'無法 fork');
} else if (
$pid) {
// 我們是父程序
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, TRUE);
curl_setopt($ch, CURLOPT_NOBODY, TRUE); // 移除 body
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
$head = curl_exec($ch);
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);

if(!
$head)
{
return
FALSE;
}

if(
$status === null)
{
if(
$httpCode < 400)
{
return
TRUE;
}
else
{
return
FALSE;
}
}
elseif(
$status == $httpCode)
{
return
TRUE;
}

return
FALSE;
pcntl_wait($status); // 防止殭屍子程序
} else {
// 我們是子程序
while(microtime(true) < $expire)
{
sleep(0.5);
}
return
FALSE;
}
}
?>

希望這個範例對您有幫助。它並非 100% 測試過,因此任何意見回饋(請直接寄電子郵件給我)都將不勝感激。
11
artem at zabsoft dot co dot in
15 年前
嘿,我修改了適用於 PHP 5 的腳本。此外,我還加入了伺服器驗證的支援,並修復了腳本中的一些小錯誤。

[由 danbrown AT php DOT net 編輯:原始碼由 (unlcuky13 AT gmail DOT com) 於 2009 年 4 月 19 日撰寫。以下為附註
以下是我透過 PHP 5 物件導向封裝來簡化操作的方式。]

<?php
class mycurl {
protected
$_useragent = 'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1';
protected
$_url;
protected
$_followlocation;
protected
$_timeout;
protected
$_maxRedirects;
protected
$_cookieFileLocation = './cookie.txt';
protected
$_post;
protected
$_postFields;
protected
$_referer ="http://www.google.com";

protected
$_session;
protected
$_webpage;
protected
$_includeHeader;
protected
$_noBody;
protected
$_status;
protected
$_binaryTransfer;
public
$authentication = 0;
public
$auth_name = '';
public
$auth_pass = '';

public function
useAuth($use){
$this->authentication = 0;
if(
$use == true) $this->authentication = 1;
}

public function
setName($name){
$this->auth_name = $name;
}
public function
setPass($pass){
$this->auth_pass = $pass;
}

public function
__construct($url,$followlocation = true,$timeOut = 30,$maxRedirecs = 4,$binaryTransfer = false,$includeHeader = false,$noBody = false)
{
$this->_url = $url;
$this->_followlocation = $followlocation;
$this->_timeout = $timeOut;
$this->_maxRedirects = $maxRedirecs;
$this->_noBody = $noBody;
$this->_includeHeader = $includeHeader;
$this->_binaryTransfer = $binaryTransfer;

$this->_cookieFileLocation = dirname(__FILE__).'/cookie.txt';

}

public function
setReferer($referer){
$this->_referer = $referer;
}

public function
setCookiFileLocation($path)
{
$this->_cookieFileLocation = $path;
}

public function
setPost ($postFields)
{
$this->_post = true;
$this->_postFields = $postFields;
}

public function
setUserAgent($userAgent)
{
$this->_useragent = $userAgent;
}

public function
createCurl($url = 'nul')
{
if(
$url != 'nul'){
$this->_url = $url;
}

$s = curl_init();

curl_setopt($s,CURLOPT_URL,$this->_url);
curl_setopt($s,CURLOPT_HTTPHEADER,array('Expect:'));
curl_setopt($s,CURLOPT_TIMEOUT,$this->_timeout);
curl_setopt($s,CURLOPT_MAXREDIRS,$this->_maxRedirects);
curl_setopt($s,CURLOPT_RETURNTRANSFER,true);
curl_setopt($s,CURLOPT_FOLLOWLOCATION,$this->_followlocation);
curl_setopt($s,CURLOPT_COOKIEJAR,$this->_cookieFileLocation);
curl_setopt($s,CURLOPT_COOKIEFILE,$this->_cookieFileLocation);

if(
$this->authentication == 1){
curl_setopt($s, CURLOPT_USERPWD, $this->auth_name.':'.$this->auth_pass);
}
if(
$this->_post)
{
curl_setopt($s,CURLOPT_POST,true);
curl_setopt($s,CURLOPT_POSTFIELDS,$this->_postFields);

}

if(
$this->_includeHeader)
{
curl_setopt($s,CURLOPT_HEADER,true);
}

if(
$this->_noBody)
{
curl_setopt($s,CURLOPT_NOBODY,true);
}
/*
if($this->_binary)
{
curl_setopt($s,CURLOPT_BINARYTRANSFER,true);
}
*/
curl_setopt($s,CURLOPT_USERAGENT,$this->_useragent);
curl_setopt($s,CURLOPT_REFERER,$this->_referer);

$this->_webpage = curl_exec($s);
$this->_status = curl_getinfo($s,CURLINFO_HTTP_CODE);
curl_close($s);

}

public function
getHttpStatus()
{
return
$this->_status;
}

public function
__tostring(){
return
$this->_webpage;
}
}
?>

[由 danbrown 在 php DOT net 編輯:包含由 "roetsch.beni at googlemail (dot) com" 於 09 年 8 月 2 日提供的錯誤修復,並附帶以下註解:「修復了錯誤修復:lighthttp 伺服器上的 417 錯誤。」]
8
gmail@asmqb7
8 年前
警告 警告

在這個範例中: https://php.dev.org.tw/manual/en/book.curl.php#102885,由 "frank at interactinet dot com" 提供

這裡有一個小錯誤

<?php

...

elseif(
$status == $httpCode)
{
return
TRUE;
}

return
FALSE;
pcntl_wait($status); //保護避免出現殭屍子程序
} else {
// 我們是子程序
while(microtime(true) < $expire)

...

?>

程式碼會在 `return` 處立即離開函式,無論如何 `pcntl_wait()` 永遠不會被執行。

然而,我看不出這個函式有其他問題。
9
artax_N_O_S_P_A_M_erxes2 at iname dot com
14 年前
我需要在 PHP 腳本中使用 cURL 下載資料,不僅要使用 SSL 進行伺服器身份驗證,還要進行客戶端身份驗證。
在 Fedora 的預設安裝中,設定好正確的 cURL 參數後,會出現錯誤

$ php curl.php
無法使用已知的 CA 憑證驗證同級憑證

http://curl.haxx.se/docs/sslcerts.html 上的資料最有用。實際上,在接近底部的地方,它告訴您要在 /etc/pki/nssdb 中新增一個遺失的連結,以使用 ca-bundle.crt 檔案。您可以這樣做

# cd /etc/pki/nssdb
# ln -s /usr/lib64/libnssckbi.so libnssckbi.so

現在您可以進行客戶端身份驗證,前提是您手邊有憑證

<?php
$data
= "<soap:Envelope>[...]</soap:Envelope>";
$tuCurl = curl_init();
curl_setopt($tuCurl, CURLOPT_URL, "https://example.com/path/for/soap/url/");
curl_setopt($tuCurl, CURLOPT_PORT , 443);
curl_setopt($tuCurl, CURLOPT_VERBOSE, 0);
curl_setopt($tuCurl, CURLOPT_HEADER, 0);
curl_setopt($tuCurl, CURLOPT_SSLVERSION, 3);
curl_setopt($tuCurl, CURLOPT_SSLCERT, getcwd() . "/client.pem");
curl_setopt($tuCurl, CURLOPT_SSLKEY, getcwd() . "/keyout.pem");
curl_setopt($tuCurl, CURLOPT_CAINFO, getcwd() . "/ca.pem");
curl_setopt($tuCurl, CURLOPT_POST, 1);
curl_setopt($tuCurl, CURLOPT_SSL_VERIFYPEER, 1);
curl_setopt($tuCurl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($tuCurl, CURLOPT_POSTFIELDS, $data);
curl_setopt($tuCurl, CURLOPT_HTTPHEADER, array("Content-Type: text/xml","SOAPAction: \"/soap/action/query\"", "Content-length: ".strlen($data)));

$tuData = curl_exec($tuCurl);
if(!
curl_errno($tuCurl)){
$info = curl_getinfo($tuCurl);
echo
'花費 ' . $info['total_time'] . ' 秒傳送請求至 ' . $info['url'];
} else {
echo
'Curl 錯誤: ' . curl_error($tuCurl);
}

curl_close($tuCurl);
echo
$tuData;
?>
1
zle.lc
3 年前
分享是關懷,包含處理程序。

<?php

$url_one
= "php.net";
$url_two = "";

$user_agent = 'Mozilla HotFox 1.0';

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url_one.$url_two);
curl_setopt($ch, CURLOPT_USERAGENT, $user_agent);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_NOBODY, 0);
curl_setopt($ch, CURLOPT_TIMEOUT, 30);
$res = curl_exec($ch);
curl_close($ch);

$url_two = "lazyphp.net";
$url_one = "";
$res_two = curl_exec($ch);
curl_close($ch);

?>
2
ramez at dot dontspan dot zegenie dot com
14 年前
在我的 Windows 7 電腦上,使用 PHP5.3 和 Apache2.2.X 時,cURL 失敗。

結果發現,僅將提到的兩個 dll(libeay32 和 sslea32)從 php 資料夾複製到您的 system32 資料夾是不夠的。您必須解除封鎖這兩個檔案。

在每個檔案上按一下滑鼠右鍵,選擇「解除封鎖」。然後重新啟動 Apache。

這是 Windows 中新增的另一個非常方便的安全功能。
0
qrworld.net
10 年前
這裡有一個我用來使用 cURL 取得 URL 內容的函式

function getUrlContent($url){
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; .NET CLR 1.1.4322)');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
curl_setopt($ch, CURLOPT_TIMEOUT, 5);
$data = curl_exec($ch);
$httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
return ($httpcode>=200 && $httpcode<300) ? $data : false;
}

此來源來自此網站

http://softontherocks.blogspot.com/2014/11/descargar-el-contenido-de-una-url.html
-1
jcmargentina at gmail dot com
5 年前
請注意,新版本的 curl 預設使用 http2,因此如果您遇到一些奇怪的錯誤、0 http 狀態碼等等,請在您的程式碼中明確指定 http 版本。
-2
fred dot knieper at gmail dot com
10 年前
在為搞清楚哪個 curl 命令列選項對應哪個程式庫函式而感到沮喪之後,我發現如果新增 `--libcurl foo.c`,curl 命令列會告訴您(以 C 程式的形式)。

如果您一直在努力想辦法讓您精美的 curl 命令列在 PHP 中運作,這會讓一切變得輕而易舉!
-2
eflash at gmx dot net
16 年前
為了在安全網站上使用 curl,您需要一個 ca-bundle.crt 檔案;這是我寫的一個 PHP 腳本,可以建立一個全新的 ca-bundle
http://www.gknw.net/php/phpscripts/mk-ca-bundle.php
我也用其他語言編寫了腳本,例如,現在隨 curl 發行的 Perl 腳本
http://curl.haxx.se/lxr/source/lib/mk-ca-bundle.pl
如果您喜歡,也可以使用 Win32 WSH 腳本
http://www.gknw.net/vb/scripts/mk-ca-bundle.vbs

希望能幫到您,Guenter。
To Top