簡體   English   中英

從file_get_contents清除奇怪的參數

[英]Clear strange parameters from file_get_contents

我的客戶通過服務與數據庫聯系。 當我執行一些操作時,我使用file_get_contents調用服務URL,然后處理響應。

我認為,當用戶從Google搜索結果進入該網站時,我認為Google會將一些參數添加到屬於file_get_contents我的服務URL中。

例如,應該如此

file_get_contents(service.domain/service_name?param1=0) 

但是google在url的末尾添加了一些奇怪的參數。 像這樣:

file_get_contents(service.domain/service_name?param1=0&force=1)

我看到了兩個或三個奇怪的參數,

force = 1
gclid=CNC-jvTapbcCFaaj

我不知道如何處理和刪除這些參數。

在使用file_get_contents調用URL之前,如何刪除這些奇怪的參數?

這是我的功能:

public function getWhere($keyword)
{
    $locale = session('locale');
    if($locale != "en" && $locale != "")
    {
        $locale = "_".session('locale');
    }else{
        $locale = "";
    }

    $page = file_get_contents(getenv('API_URL')."station_info?search.url=".$keyword);
    $page = json_decode($page);
    $page = (array) $page->rows;
    $station = $page[0];

    if($station->search->status == 1)
    {
        return redirect('/');
    }
    return view('station', compact('station', 'locale'));
}

奇怪的參數會將其自身添加到URL的末尾。

編輯

我認為有人正在嘗試某事。 $ keyword必須使用一個字符串,該字符串是站點名稱,例如London。 但是有人試圖發送London&force = 1,所以我必須檢查$ keyword變量。

您可以通過parse_url函數解析網址,刪除不需要的參數或僅保留所需的參數並生成網址

使用parse_url函數后,您可能需要通過parse_str函數來解析query ,因此代碼如下所示:

$urlParsed = parse_url("service.domain/service_name?param1=0&force=1");
$params = parse_str ($urlParsed['query']);
$newParams = [];
$neededParams = ['param1', 'param2'];
foreach ($neededParams as $p) {
    if (isset($params[$p])) {
        $newParams[$p] = $params[$p];
    }
}

$newUrl = $urlParsed['scheme'] . '://' . $urlParsed['host'] . $urlParsed['path'] . '?' . http_build_query($newParams);

file_get_contents($newUrl);

我沒有測試該代碼,但是我希望這個想法很明確,您可以根據需要進行修改。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM