PHP Conference Japan 2024

cURL 函式

目錄

新增註記

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

Mr.KTO
17 年前
別忘了 curl_close($ch); 即使 curl_errno($ch) != 0

因為如果您不這麼做,在 Windows 上會產生 Windows 錯誤報告(程式意外終止)
helmizz at yahoo dot com
10 年前
這是使用 curl 的範例程式碼,只需輸入 curl_setopt,
例如
curlsetop[0] ==> 名稱:CURLOPT_URL;值:http://amazon.com
curlsetop[1] ==> 名稱:CURLOPT_RETURNTRANSFER;值:true
curlsetop[2] ==> 名稱:CURLOPT_FOLLOWLOCATION;值:true

您可以新增表單輸入。

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>新文件</title>
<meta name="Generator" content="">
<meta name="Author" content="Helmi Anwar">
<meta name="Keywords" content="">
<meta name="Description" content="">
</head>

<body>

<form method="post" action="">
<table>
<tr class="rowid_add">
<td>curl_setopt [0]</td>
<td>名稱:<input type="text" size="50" name="setopt_name[]"></td>
<td>值:<input type="text" size="50" name="setopt_value[]"></td>
</tr>
<tr class="rowid_add">
<td>curl_setopt [0]</td>
<td>名稱:<input type="text" size="50" name="setopt_name[]"></td>
<td>值:<input type="text" size="50" name="setopt_value[]"></td>
</tr>
<tr class="rowid_add">
<td>curl_setopt [0]</td>
<td>名稱:<input type="text" size="50" name="setopt_name[]"></td>
<td>值:<input type="text" size="50" name="setopt_value[]"></td>
</tr>
<tr>
<td><input type="submit" name="submit_yes" value="執行"></td>
</tr>
</table>
</form>


<?php

function curl_test($setopt_content)
{
$ch = curl_init();
curl_setopt_array($ch, $setopt_content);
$result_data = curl_exec($ch);
curl_close($ch);
return
$result_data;
}

if(
$_REQUEST['submit_yes']=="EXECUTE")
{

foreach (
$_REQUEST['setopt_name'] as $k => $index_content)
{
$value_content=$_REQUEST['setopt_value'][$k];
$index_content =strtoupper($index_content);
eval(
'$index_content = '.$index_content.';');
//echo ($index_content);
if($index_content!='')
{
if(
strtoupper($value_content)=='TRUE')
{
$setopt_content[$index_content]=TRUE;}
elseif(
strtoupper($value_content)=='FALSE')
{
$setopt_content[$index_content]=FALSE;}
else
{
$setopt_content[$index_content]=$value_content;}
}
}

$info=curl_test($setopt_content);

}

?>
<textarea name="result" rows="25" cols="100"><?php echo htmlspecialchars($info);?></textarea>
</body>
</html>
匿名
3 年前
注意:如果您需要獲取/設定 Curl 物件的唯一控制代碼,您可能需要為每個實例使用 CURL_PRIVATE 屬性。
https://php.dev.org.tw/manual/en/function.curl-setopt.php
simon [at] vhostdirect [dot] co [dot] uk
20 年前
我花了相當長的時間才弄清楚如何讓 Curl(使用 SSL)、OpenSSL 和 PHP 能夠順利地一起運作。

在重新安裝 MS-VC7 並編譯 OpenSSL 之後,我才終於意識到這並非必要。

如果您像我一樣,比起 Windows 更喜歡 *Nix 系統,那麼您很可能會遇到類似的問題。

我透過使用正確的關鍵字在 Google 上搜尋,偶然發現了這個。

http://www.tonyspencer.com/journal/00000037.htm

我仔細閱讀了那篇文章,並找到了我的錯誤。

這只是一些簡短的筆記,但我發現它們是關於這個主題最好也最簡單的說明。

別忘了在您的腳本中加入一行簡單的程式碼,讓它們能在 Win32 上運作。

<?php
if($WINDIR) curl_setopt($curl, CURLOPT_CAINFO, "c:\\windows\\ca-bundle.crt");
?>

最後一點:ca-bundle.crt 檔案位於 Curl 下載中。我將我的檔案儲存在 Windows 目錄中,Apache/PHP 可以正常存取它。

祝一切順利,希望這能有所幫助。

Simon Lightfoot
vHost Direct Limited
mikeb[at]xamo[dot]com
20 年前
給 PHP 5 使用者的警告:如果您在連線錯誤時嘗試使用 curl_getinfo 取得 CURLINFO_CONTENT_TYPE,PHP 會發生核心傾印。我已將此問題告知 Curl 團隊,希望很快就能修復。請務必在查找此資料前先檢查是否有錯誤。
killermonk at REMOVE dot killermonk dot com
17 年前
給任何嘗試使用 cURL 提交到使用圖片作為提交按鈕的 ASP/ASPX 頁面的人:

請確保在 post 欄位中有 'button_name.x' 和 'button_name.y'。PHP 將這些欄位命名為 'button_name_x' 和 'button_name_y',而 ASP 使用點號。

此外,如上所述,請務必在您的 post 請求中包含 '__VIEWSTATE' 輸入欄位。
Peter X.
16 年前
雖然 cURL 在透過 HTTP 連結取得檔案方面的效能優於 file_get_contents 和 fopen,但 cURL 的缺點是它無法一次只讀取頁面的一部分。

例如,以下程式碼可能會產生記憶體限制錯誤

<?php
$ch
= curl_init("http://www.example.com/reallybigfile.tar.gz");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_BINARYTRANSFER, true);
$output = curl_exec($ch);

$fh = fopen("out.tar.gz", 'w');
fwrite($fh, $output);
fclose($fh);
?>

而另一方面,這樣做則不會

<?php
$hostfile
= fopen("http://www.example.com/reallybigfile.tar.gz", 'r');
$fh = fopen("out.tar.gz", 'w');

while (!
feof($hostfile)) {
$output = fread($hostfile, 8192);
fwrite($fh, $output);
}

fclose($hostfile);
fclose($fh);
?>
alidrus at langkah dot com
20 年前
在較新版本的 PHP 中,CURLOPT_MUTE 可能已被棄用。任何嘗試使用 curl_setopt() 設定 CURLOPT_MUTE 都會產生如下警告:

PHP 注意:使用未定義的常數 CURLOPT_MUTE - 假設為 'CURLOPT_MUTE' in ....

如果您希望讓 curl 輸出靜默,請改用以下方法:

<?php
curl_setopt
($ch, CURLOPT_RETURNTRANSFER, true);
?>

然後,

<?php
$curl_output
=curl_exec($ch);
?>

curl 操作的輸出將以字串形式儲存在 $curl_output 中,同時操作保持完全靜默。
richardkmiller AT gmail
18 年前
注意 URL 中的任何額外空格。URL 中的尾隨空格導致我的腳本失敗,並顯示「伺服器回覆為空」的訊息。
ciaoandriana8 at gmail dot com
3 年前
這是使用 curl 的範例程式碼,只需輸入 curl_setopt,
例如
curlsetop[0] ==> 名稱:CURLOPT_URL;值:https://amzn.to/3njlWW6
curlsetop[1] ==> 名稱:CURLOPT_RETURNTRANSFER;值:true
curlsetop[2] ==> 名稱:CURLOPT_FOLLOWLOCATION;值:true

您可以新增表單輸入。

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>新文件</title>
<meta name="Generator" content="">
<meta name="Author" content="Helmi Anwar">
<meta name="Keywords" content="">
<meta name="Description" content="">
</head>

<body>

<form method="post" action="">
<table>
<tr class="rowid_add">
<td>curl_setopt [0]</td>
<td>名稱:<input type="text" size="50" name="setopt_name[]"></td>
<td>值:<input type="text" size="50" name="setopt_value[]"></td>
</tr>
<tr class="rowid_add">
<td>curl_setopt [0]</td>
<td>名稱:<input type="text" size="50" name="setopt_name[]"></td>
<td>值:<input type="text" size="50" name="setopt_value[]"></td>
</tr>
<tr class="rowid_add">
<td>curl_setopt [0]</td>
<td>名稱:<input type="text" size="50" name="setopt_name[]"></td>
<td>值:<input type="text" size="50" name="setopt_value[]"></td>
</tr>
<tr>
<td><input type="submit" name="submit_yes" value="執行"></td>
</tr>
</table>
</form>

<?php

函式 curl_test($setopt_content)
{
$ch = curl_init();
curl_setopt_array($ch, $setopt_content);
$result_data = curl_exec($ch);
curl_close($ch);
return
$result_data;
}

if(
$_REQUEST['submit_yes']=="EXECUTE")
{

foreach (
$_REQUEST['setopt_name'] as $k => $index_content)
{
$value_content=$_REQUEST['setopt_value'][$k];
$index_content =strtoupper($index_content);
eval(
'$index_content = '.$index_content.';');
//echo ($index_content);
if($index_content!='')
{
if(
strtoupper($value_content)=='TRUE')
{
$setopt_content[$index_content]=TRUE;}
elseif(
strtoupper($value_content)=='FALSE')
{
$setopt_content[$index_content]=FALSE;}
else
{
$setopt_content[$index_content]=$value_content;}
}
}

$info=curl_test($setopt_content);

}

?>
<textarea name="result" rows="25" cols="100"><?php echo htmlspecialchars($info);?></textarea>
</body>
</html>
To Top