2017-01-02 34 views
0

我正在編寫一個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 $

+0

我會如果我可以。正如我在問題中所說的,腳本沒有錯誤。 * error.log *文件是空的。 – TheLearner

+0

400是一個不好的要求,我認爲$ audiolink無效。你可以發佈$ audiolink的內容嗎? – Sepultura

+0

$ audiolink和$ testval中的值完全相同,這就是我難倒了。唯一的區別是,當$ audiolink動態分配一個URL時,$ testval被分配一個靜態值。你可以在http://www.peppyburro.com/sandboxassets/engines/pronunciation1.php查看這個代碼的輸出: – TheLearner

回答

2

,也許這是一個編碼問題:

http://audio1.spanishdict.com/audio?lang=es&text=hombre&key=84be08a91b32dc61e45f8f78970b206b 

http://audio1.spanishdict.com/audio?lang=es&amp;text=hombre&amp;key=84be08a91b32dc61e45f8f78970b206b 

嘗試編碼&和?正確的,也許htmlspecialchars_decode將幫助

+0

當我把在htmlspecialchars_decode()中分配給'$ audiolink'的值包裝下載:) –

+0

Perfect !有效!真是一個愚蠢的錯誤。萬分感謝。 :) – TheLearner