Ayrı ayrı php kodları birleştirme
Dosya : wiki-image-proxy.php
<?php /* 165 Lines */
/**
* Privacy proxy for images
*
* USE: https://yourdomain.top/proxy.php?url=
*
* Why should a proxy be used for the output of the images?
* To comply with the DSGVO,
* images from other servers should be outputed via a data protection proxy
* As is well known, the images provided by the Wikipeda API are delivered
* directly from the Wikipedia servers.
* In view of the data protection regulation, however, this is problematic,
* as it gives Wikipedia access to the IP addresses of visitors to the site.
* To prevent this, this privacy proxy can be used,
* which retrieves the images through this PHP script.
* This only gives Wikipedia access to the IP of the requesting
* web server, and not the IP of the page visitor viewing the image.
*
* @see https://github.com/gaffling/PHP-Wiki-API/blob/master/wiki-image-proxy.php
*
*/
// Check for url parameter
$url = isset( $_GET['url'] ) ? $_GET['url'] : null;
if (!isset($url) or preg_match('#^https?://#', $url) != 1)
{
header('HTTP/1.1 404 Not Found');
exit;
}
// Check if the client already has the requested item
if (isset($_SERVER['HTTP_IF_MODIFIED_SINCE']) or
isset($_SERVER['HTTP_IF_NONE_MATCH']))
{
header('HTTP/1.1 304 Not Modified');
exit;
}
// Get File Extention from URL
$ext = pathinfo(basename($url), PATHINFO_EXTENSION);
// Check if cURL exists, and if so: use it
if (function_exists('curl_version'))
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_USERAGENT, 'ImageProxy/1.0 (+http://'.$_SERVER['SERVER_NAME'].'/');
curl_setopt($ch, CURLOPT_BINARYTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 4);
curl_setopt($ch, CURLOPT_BUFFERSIZE, 12800);
curl_setopt($ch, CURLOPT_NOPROGRESS, false);
curl_setopt($ch, CURLOPT_PROGRESSFUNCTION,
function($DownloadSize, $Downloaded, $UploadSize, $Uploaded)
{
return ($Downloaded > 1024 * 512) ? 1 : 0; /* max 512kb */
}
);
$out = curl_exec ($ch);
curl_close ($ch);
// Read all headers
$file_array = explode("\r\n\r\n", $out, 2);
$header_array = explode("\r\n", $file_array[0]);
foreach($header_array as $header_value)
{
$header_pieces = explode(': ', $header_value);
if(count($header_pieces) == 2)
{
$headers[$header_pieces[0]] = trim($header_pieces[1]);
}
}
// Check if location moved, and if so: redirect
if (array_key_exists('Location', $headers)) {
$newurl = urlencode($headers['Location']);
header("HTTP/1.1 301 Moved Permanently");
if ((isset($_SERVER['HTTP_X_FORWARDED_PROTO']) and
$_SERVER['HTTP_X_FORWARDED_PROTO'] == 'https') or
(isset($_SERVER['HTTPS']) and $_SERVER['HTTPS'] == 'on') or
(isset($_SERVER['HTTPS']) and $_SERVER['SERVER_PORT'] == 443))
{
$protocol = 'https://';
}
else
{
$protocol = 'http://';
}
$PROXY = $protocol.$_SERVER['HTTP_HOST'].$_SERVER['SCRIPT_NAME'].'?url=';
header('Location: ' . $PROXY . $newurl);
}
else // Check if it's an image and output all headers
{
if (array_key_exists('Content-Type', $headers))
{
$ct = $headers['Content-Type'];
if (preg_match('#image/png|image/.*icon|image/jpe?g|image/gif#', $ct) !== 1)
{
header('HTTP/1.1 404 Not Found');
exit;
}
header('Content-Type: ' . $ct);
}
else
{
header('Content-Type: image/' . $ext);
}
if (array_key_exists('Content-Length', $headers))
{
header('Content-Length: ' . $headers['Content-Length']);
}
if (array_key_exists('Expires', $headers))
{
header('Expires: ' . $headers['Expires']);
}
if (array_key_exists('Cache-Control', $headers))
{
header('Cache-Control: ' . $headers['Cache-Control']);
}
if (array_key_exists('Last-Modified', $headers))
{
header('Last-Modified: ' . $headers['Last-Modified']);
}
// Output Image
echo $file_array[1];
}
}
else // No cURL so use readfile()
{
// Check if it's an image
if ($imgInfo = @getimagesize( $url ))
{
// Check mime
if (preg_match('#image/png|image/.*icon|image/jpe?g|image/gif#', $imgInfo['mime']) !== 1)
{
header('HTTP/1.1 404 Not Found');
exit;
}
// Output simple header
if (isset($imgInfo['mime']) and !empty($imgInfo['mime']) )
{
header( 'Content-Type: ' . $imgInfo['mime'] );
}
else
{
header('Content-Type: image/' . $ext);
}
// Output Image
readfile( $url );
}
else
{
// No Image Found
header('HTTP/1.1 404 Not Found');
exit;
}
}
Dosya : wiki2api.php
<?php /* 311 Lines */
/* PHP-Wiki-API: This is a simple class to get short Wikipedia info boxes from a given Keyword.
*
* @package PHP-Wiki-API
* @copyright Copyright (c) 2019 Igor Gaffling <[email protected]>
* @license https://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt LGPL License
* @version Release: @1.1@
* @link https://github.com/gaffling/PHP-Wiki-API
* @since Class available since Release 1.0
*
* @example <php>
* require_once __DIR__.'/wiki2api.php'; // Include the Wikipedia API Class
* $wiki = new wiki(); // Start the Wikipedia API Class
* echo $wiki->api($_GET['q']); // Output the API Response
* </php>
*/
class wiki
{
// Read and set Parameters
public function __construct($params=array())
{
// Default Values
$defaults = array(
'language' => 'tr',
'userAgent' => 'WikiBot/1.0 (+http://'.$_SERVER['SERVER_NAME'].'/)',
'betterResults' => true,
'proxy' => '',
'imageProxy' => true,
'DEBUG' => '',
);
// Merge Parameters and Defaults
$this->params = array_merge($defaults, $params);
}
// Helper Function to get the Content from the API URL
private function getContent($url, $user_agent, $proxy='')
{
// Hopfully we run PHP 4 >= 4.3.0
if (function_exists('file_get_contents'))
{
// Set User-Agent and Proxy
$context = array (
'http' => array (
'user_agent' => $user_agent,
'proxy' => $proxy,
'request_fulluri' => true,
),
);
// Build Stream Context
$context = stream_context_create ($context);
// Use file_get_contents() Function and hide Error with @
$content = @file_get_contents($url, NULL, $context);
}
else // We run PHP < 4.3.0 - OMG :-o
{
// Ini Var
$content = '';
// Open URL and hide Error with @
if($handle = @fopen($url, 'r'))
{
// While there is Data
while (!feof($handle))
{
// Read the Data-Line
$line = fgets($handle, 4096);
// Add the Data-Line to the Content Var
$content .= $line;
}
// Better Close the FileHandle after the fgets()
fclose($handle);
}
}
// The Function returns the Content
return $content;
}
// Call the API Main Function
public function api($query)
{
// Ini Vars
$text = $image = $description = '';
// Convert Query to Lowercase for Headline
$strtolower = mb_strtolower($query);
// Convert Headlie to UTF-8 Uppercase Words
$headline = mb_convert_case($strtolower, MB_CASE_TITLE, 'UTF-8');
// If Query is complete Uppercase make also complete Uppercase Headline
if ($query === strtoupper($query))
{
$headline = mb_strtoupper($query);
}
// Replace spaces in Query to Underscore and use Uppercase Words from Headline
$query = str_replace(' ', '_', $headline);
// In DEBUG Mode print Query
if ($this->params['DEBUG']=='KEY' || $this->params['DEBUG']=='ALL')
{
echo '<tt><b>Search-Keyword </b><xmp>#'.$query.'#</xmp></tt>';
}
// First search the API if betterResults==true
if ($this->params['betterResults'] == true)
{
// Wikipedia API URL 1 - https://en.wikipedia.org/w/api.php
$url = 'https://'.$this->params['language'].'.wikipedia.org/w/api.php'.
'?action=query&format=json&list=search&srsearch=intitle:'.$query.
'&maxlag=1'; /* stop if wiki server is busy */
// If API Call 1 could be reached
if ($api = $this->getContent($url, $this->params['userAgent'], $this->params['proxy']))
{
// Decode the 1 Response
$data = json_decode($api, TRUE);
// In DEBUG Mode print 1 Response
if ($this->params['DEBUG']=='API1' || $this->params['DEBUG']=='ALL')
{
echo '<pre><b>Search API-Call (1) Response</b> ';
echo var_dump($data);
echo '</pre>';
}
// If there is a search Result
if (isset($data['query']['search'][0]['title']))
{
// Set Headline
$headline = $data['query']['search'][0]['title'];
// Set the Query to the first Search Result (and replace Spaces with Underscores)
$query = str_replace(' ', '_', $data['query']['search'][0]['title']);
// In DEBUG Mode print Found Keyword
if ($this->params['DEBUG']=='KEY' || $this->params['DEBUG']=='ALL')
{
echo '<tt><b>Found Search-Keyword </b><xmp>#'.$query.'#</xmp></tt>';
}
}
// If Search Result is a 'Did you mean:' Hint
if ( isset($data['query']['searchinfo']['suggestion']))
{
// Set Text Hints depending on selected Language
if ($this->params['language'] == 'de')
{
$suggestionText = 'Meinten Sie: ';
}
else
{
$suggestionText = 'Olabilir: ';
}
// Remove 'q=' Variable=Value Pair from Querystring
$QUERY_STRING = preg_replace('/'.('q'?'(\&|)q(\=(.*?)((?=&(?!amp\;))|$)|(.*?)\b)':'(\?.*)').'/i','',$_SERVER['QUERY_STRING']);
// Delete 'intitle:' from Suggestion Keyword
$suggestion = str_replace('intitle:','',$data['query']['searchinfo']['suggestion']);
// Make Suggestion UTF-8 Uppercase Words
$suggestion = mb_convert_case($suggestion, MB_CASE_TITLE, 'UTF-8');
// Make HTML Link for Suggestion
$description = $suggestionText.'<a href="?q='.
str_replace(' ', '_', $suggestion).$QUERY_STRING.'">'.$suggestion.'</a>';
}
}
}
// Wikipedia API URL 2 - https://en.wikipedia.org/w/api.php
$url = 'https://'.$this->params['language'].
'.wikipedia.org/api/rest_v1/page/summary/'.$query.
'?maxlag=1'; /* stop if wiki server is busy */
// If API Call 2 could be reached
if ($api = $this->getContent($url, $this->params['userAgent'], $this->params['proxy']))
{
// Decode the 2 Response
$data = json_decode($api, TRUE);
// In DEBUG Mode print 2 Response
if ($this->params['DEBUG']=='API2' || $this->params['DEBUG']=='ALL')
{
echo '<pre><b>Main API-Call (2) Response</b> ';
echo var_dump($data);
echo '</pre>';
}
// If there is an Image in the Search Result
if (isset($data['originalimage']['source']))
{
// If the DSGVO imageProxy should be use define it
$proxy = '';
if ($this->params['imageProxy']==TRUE)
{
$proxy = 'wiki-image-proxy.php?url=';
}
// Build HTML for Image
$image = '<img src="'.$proxy.$data['thumbnail']['source'].'" />';
}
// Correct the Text
$text = str_replace('#', ': ', $data['extract_html']);
// If there is a Description
if (isset($data['description']))
{
// Correct the Description depending on selected Language
$description = str_replace(
array(
'Wikimedia-Begriffsklärungsseite',
'Disambiguation page providing links to topics that could be referred to by the same search term'
),
array(
'kann sich auf Folgendes beziehen',
'may refer to the following'
),
$data['description']
);
// Set Keyword to UTF-8 Uppercase Words of Query
$keyword = mb_convert_case($strtolower, MB_CASE_TITLE, 'UTF-8');
// Highlight the Query in the Text and Delete some Text
$text = str_replace(
array($keyword, ' may refer to', ' steht für:'),
array('<b class="hint">'.$keyword.'</b>', '', ''),
$text
);
}
// If there is no Article Text set a Default depending on selected Language
// e.g. q=Leonardo%20di%20caprio&language=de OR q=100&language=de
if ($text == '')
{
$description = $image = '';
if ($this->params['language'] == 'de')
{
$text = 'Zu diesem Stichwort ist kein Artikel vorhanden.';
}
else if($text == '')
{
$text = 'There is no article available for this keyword.';
}
return; // ONLY IF YOU WHANT NO OUTPUT !!
}
}
// Build the HTML Output
if ($this->params['language']=='de')
{
$moreAbout = 'Mehr über';
$from = 'bei';
}
else
{
$moreAbout = 'More about';
$from = 'from';
}
// Without any Search Result return nothing
if ($text == '' && $description == '')
{
return '';
}
// With a Search Resuld build a Footer Link
if ($text != '')
{
$footer = $moreAbout.' »'.$headline.'« '.$from;
$url = 'https://'.$this->params['language'].'.wikipedia.org/wiki/'.$query;
}
else if ($description != '')
{
// Footer Link for Suggestion-Link
$footer = '';
$url = 'https://'.$this->params['language'].'.wikipedia.org/';
}
// Use the Template
ob_start();
include 'wiki2tpl.phtm';
// Return the HTML
return ob_get_clean();
}
}
Dosya : wiki2tpl.phtm (Burası kısaca index.html. Api sonuçları burada.)
<?PHP /* 99 Lines */
/* http://myacquisitions.blogspot.com/2013/03/googles-favorite-colors-by-hex-codes.html */
?>
<title><?=$headline?></title>
<style>
.wiki {
-webkit-box-shadow: 0 0 0 1px rgba(0,0,0,.01), 0 2px 3px 0 rgba(0,0,0,.1);
box-shadow: 0 0 0 1px rgba(0,0,0,.01), 0 2px 3px 0 rgba(0,0,0,.1);
padding: 2.5rem 2.5rem 2.4rem;
border: 1px solid #CCCCCC; /* COLOR */
max-width: 400px;
}
.wiki h1 {
-webkit-font-smoothing: antialiased;
text-shadow: 0 0 1px rgba(0,0,0,.1);
font-family: Arial,sans-serif;
letter-spacing: -.08rem;
font-weight: normal;
font-size: 1.7rem;
line-height: 1.2;
color: #222222; /* COLOR */
padding: 0;
margin: 0;
}
.wiki p {
-webkit-font-smoothing: antialiased;
text-shadow: 0 0 1px rgba(0,0,0,.1);
font-family: Arial,sans-serif;
margin: 1rem 0 0;
color: #222222; /* COLOR */
padding: 0;
}
.wiki ul li {
-webkit-font-smoothing: antialiased;
text-shadow: 0 0 1px rgba(0,0,0,.1);
font-family: Arial,sans-serif;
color: #222222; /* COLOR */
padding: 0;
}
.wiki .hint {
-webkit-font-smoothing: antialiased;
text-shadow: 0 0 1px rgba(0,0,0,.1);
color: #DD4B39 ; /* COLOR */
}
.wiki hr {
-webkit-box-sizing: content-box;
box-sizing: content-box;
border-width: 0;
border-top: 1px solid WhiteSmoke;
margin: 1rem 0;
height: 0;
}
.wiki img {
margin: 0 0 .5rem 1rem;
max-height: 200px;
max-width: 200px;
float: right;
}
.wiki p small {
font-size: small;
}
.wiki a {
color: #1155cc; /* COLOR */
}
.wiki a:link {
color: #1155cc; /* COLOR */
}
.wiki a:hover {
text-decoration: none;
}
.wiki a:active {
color: #33aaff; /* COLOR */
}
.wiki a:visited {
color: #6611CC; /* COLOR */
}
.wiki a:focus {
outline: 0;
}
</style>
<div class="wiki">
<h1><?=$headline?></h1>
<p><?=$description?></p>
<?=$footer!=''?'<hr>':''?>
<?=$image?>
<p><?=$text?></p>
</div>
Dosya : wikipedia.php
<?php /* 70 Lines */
/* ⣠⣾⣿⣿⣷⣄ Simple PHP Wiki Info Box ⣠⣾⣿⣿⣷⣄ */
/* https://github.com/gaffling/PHP-Wiki-API */
// Get the Parameter from the URL
if ( isset($_GET['language']) ) {
$language = $_GET['language'];
} else {
$language = 'tr';
}
if ( isset($_GET['userAgent']) ) {
$userAgent = $_GET['userAgent'];
} else {
$userAgent = 'WikiBot/1.0 (+http://'.$_SERVER['SERVER_NAME'].'/)';
}
if ( isset($_GET['betterResults']) and
($_GET['betterResults'] == 'false' or $_GET['betterResults'] == 0) ) {
$betterResults = false;
} else {
$betterResults = true;
}
if ( isset($_GET['proxy']) ) {
$proxy = $_GET['proxy'];
} else {
$proxy = null;
}
if ( isset($_GET['imageProxy']) and
($_GET['imageProxy'] == 'false' or $_GET['imageProxy']== 0) ) {
$imageProxy = false;
} else {
$imageProxy = true;
}
if ( isset($_GET['DEBUG']) ) {
$DEBUG = $_GET['DEBUG'];
} else {
$DEBUG = null;
}
// Set the Parameter
$options = array(
'language' => $language,
'userAgent' => $userAgent,
'betterResults' => $betterResults,
'proxy' => $proxy,
'imageProxy' => $imageProxy,
'DEBUG' => $DEBUG,
);
// Include the Wikipedia API Class
require_once __DIR__.'/wiki2api.php';
// Start the Wikipedia API Class
$wiki = new wiki($options);
// Output the API Response
echo $wiki->api($_GET['q']);
// Print the Script Runtime in DEBUG Mode
if ( isset($DEBUG) ) {
echo "<pre>\n\n\tRuntime: ".number_format((microtime(true)-$_SERVER['REQUEST_TIME_FLOAT']),3);
}
/* ⣠⣾⣿ EOF - END OF FILE ⣿⣷⣄ */
Merhabalar,
bu apilerin ayrı ayrı dosya türleri var. Ancak bunları birleştirmemiz gerekiyor. Yaparken 500 html döngüsüne giriyor. Nerede hata yapıyoruz ? Yardımlarınızı beklemekteyim :)
Soru hatalı mı? 👎
Eğer sorunun kurallara aykırı olduğunu düşünüyorsanız lütfen bize bildirin!