PHPプログラムに関する各種メモ書き

guzzle を使った URL 存在チェック

● guzzle を使った URL 存在チェック

ただし、楽天のようにリダイレクトしている場合はコンテンツを取得して redirect があるかどうかをチェックする必要があります。

	/**
	 * URLの存在チェック
	 *
	 */
	public function check_url_exists( string $url )
	{
		// $url = "https://item.rakuten.co.jp/iloiloislandimport/rb084myjgdf/?201604";
		$url = "https://item.rakuten.co.jp/iloiloislandimport/rb01m63hrzm/?832467";
	    $client = new GuzzleHttp\Client();
	    try {
	        $request = $client->head($url);
	        $this->mydump->dump( $request->getStatusCode()  );
	        $this->mydump->dump( "OK URL exists : {$url}" );
	        return true;
	    } catch (GuzzleHttp\Exception\ClientException $e) {
	        return false;
	        $this->mydump->dump( "NG URL not exists : {$url}" );
	    }
	}
No.1828
08/01 00:28

edit