後面的輸出。在url中使用查詢srting向api發送請求,並使用php
- 我在我的網站腳本,將前綴所有外部鏈接 像
mywebsite.com/redirect_to/?url=externallink.com
- 因此,假設你正在瀏覽的網頁
mywebsite.com/redirect_to/?url=gooogle.com
我創建.htaccess
規則來處理請求redirect_to.php
包含
<a href="<?php $url = $_GET["url"]; echo htmlspecialchars($url); ?>">External Link</a>
- 整個過程都很好。
現在,我想我們在redirect_to.php看到,原來的外部鏈接轉換爲短網址
短URL API的工作原理是這樣的:
,當我們瀏覽someurlshortener.com/api.php?url=http://google.com
我們得到http://someurlshortener.com/6421
同樣地,我提出<a href="<?php $var = file_get_contents('http://someurlshortener.com/api.php?url=https://google.com'); $output = preg_replace('/<script\b[^>]*>(.*?)<\/script>/is', "", $var); print $output; ?>">Shortened Link</a>
這給了輸出http://someurlshortener.com/34566
- 此API請求也正常工作。
然後我試着將這兩個代碼結合起來。像
<a href="<?php $var = file_get_contents('http://someurlshortener.com/api.php?url=<?php $url = $_GET["url"]; echo htmlspecialchars($url); ?>'); $output = preg_replace('/<script\b[^>]*>(.*?)<\/script>/is', "", $var); print $output; ?>">Is it shortened?</a>
這給出了一個鏈接到http://mywebsite.com/redirect_to/?url=https://google.com
這是頁面本身的輸出。
- 這不符合我的要求。
所以,請幫我解決這個問題。
作品,像魅力, 順便說一句,不要介意。我是新來的PHP。 –
@SatishNetha不用擔心。每個人都在某個方面對它有所瞭解。只要堅持下去,不要猶豫,問問你是否卡住了。這就是爲什麼這個網站存在。 :-) –