2011-08-24 36 views
0

我想構成像這樣的一個鏈接:PHP撰寫鏈路動態

https://123reduceri.ro/api/v2/bucuresti.json?token=f65c060dc913a75e446cffb53653ba89b39b4852 & SIDS%5B%5D = & SIDS [] = 1個& SIDS [] = 2個& SIDS [] = 4

其中SIDS [] = 1等,這IDS被動態地取作爲SQL結果。 我的問題是:我如何讓他們在網址?我試過:

$apiCategoryUrl = 'https://123reduceri.ro/api/v2/bucuresti.json?'. 
        'token=f65c060dc913a75e446cffb53653ba89b39b4852&sids%5B%5D&'. 
        foreach ($pageposts as $pp): 
         echo 'sids[]='.$pp.'&'; 
        endforeach; ?>; 

但它不好..任何想法?

+0

請告訴我你的輸出獲得?對$ pageposts的內容執行print_r操作,以確保您正在檢索正常。 – JonB

+0

沒有輸出,只是語法錯誤 – dana

回答

4

可以使用http_build_query功能

$url_root = "https://123reduceri.ro/api/v2/bucuresti.json?"; 

$params = array(
    "token" => "f65c060dc913a75e446cffb53653ba89b39b4852", 
    "sids" => array(1,2,3,4,5) 
); 

$url = $url_root . http_build_query($params); 
+0

謝謝,但我必須保持不在數組中的sid []字符串 – dana

+0

你是什麼意思? – marvin

3

你echo'ing的SID的,而不是將它們添加到字符串...

$apiCategoryUrl = "https://https://123reduceri.ro/api/v2/bucuresti.json?token=f65c060dc913a75e446cffb53653ba89b39b4852&sids%5B%5D&"; 

foreach ($pageposts as $pp) { 
    $apiCategoryUrl .= "sids[]=".$pp."&"; 
} 

echo $apiCategoryUrl; 
+0

哈哈,你是對的。謝謝! – dana

+0

沒問題 - 在調試時很容易被忽視。你介意接受嗎? :) – user410932