我正在編寫一個PHP腳本來檢索遠程網站上的音頻文件。該腳本如下:無法使curl_init()與動態生成的URL一起工作
<?php
require_once 'table_access/simplehtmldom_1_5/simple_html_dom.php';
function getsound($wrd){
$html = file_get_html('http://www.spanishdict.com/translate/' . rawurlencode($wrd));
$colentry = $html->find('div.source');
if($colentry != null){
$ahref = $colentry[0]->find('a.audio-start');
$audiolink = trim($ahref[0]->getAttribute('href'));
$testval = 'http://audio1.spanishdict.com/audio?lang=es&text=hombre&key=84be08a91b32dc61e45f8f78970b206b';
echo '<p> T: ' . $testval . '</p>';
echo '<p> A: ' . $audiolink . '</p>';
// $testval = $audiolink;
$ch = curl_init($audiolink);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_NOBODY, 0);
curl_setopt($ch, CURLOPT_TIMEOUT, 5);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
$output = curl_exec($ch);
$status = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
if ($status == 200) { file_put_contents(dirname(__FILE__) . '/audiotest.mp3', $output); }
else { echo $status; }
return($audiolink);
}
}
echo '<p>' . getsound('hombre') . '</p>';
?>
我正在使用simple html DOM script來刮取遠程訪問的頁面。出於測試目的,我有URL分配到兩個不同的變量:
- $ audiolink:正在動態生成關閉頁面被刮掉這一個。
- $ testval:這是一個非動態變量,手動爲其分配了一個示例URL。
測試用例我使用返回的精確相同的值或者變量是在的回波()命令的輸出明顯。但是,僅當我將$ testval(硬編碼值)傳遞給curl_init()函數時纔會下載目標文件。如果我通過了$ audiolink(動態生成的值),腳本無任何錯誤地運行,但無法下載文件。我重複一次,在我的測試運行中的任何變量的值是相同的。
有什麼我可以忽略的嗎?如上所述,腳本在傳遞時不會引發任何錯誤$ audiolink; error.log文件爲空。
更新:只是回聲()「編狀態碼和原來這是400當我使用$ audiolink和200,當我用名爲testVal $。
我會如果我可以。正如我在問題中所說的,腳本沒有錯誤。 * error.log *文件是空的。 – TheLearner
400是一個不好的要求,我認爲$ audiolink無效。你可以發佈$ audiolink的內容嗎? – Sepultura
$ audiolink和$ testval中的值完全相同,這就是我難倒了。唯一的區別是,當$ audiolink動態分配一個URL時,$ testval被分配一個靜態值。你可以在http://www.peppyburro.com/sandboxassets/engines/pronunciation1.php查看這個代碼的輸出: – TheLearner