PHP with Kafka - rdkafka - php

Iam dealing with kafka and php, Im beginner the kafka has been set upped correctly on the server,
now am on local
I have installed:
librdkafka.dll on xamp/php
and php_rdkafka.dll on xamp/php/ext
and i added this on php.ini:
extension=php_rdkafka.dll
then, i have created a php page contains this code:
$topic_name = '???';
$conf = new RdKafka\Conf();
$conf->set('metadata.broker.list', $broker_list);
$conf->set('enable.idempotence', 'true');
$conf->set('group.id', $topic_name);
$conf->set('sasl.username', "???");
$conf->set('sasl.mechanism', "PLAIN");
$conf->set('sasl.password', "???");
$conf->set('log_level', (string) LOG_DEBUG);
$conf->set('debug', 'all');
$producer = new RdKafka\Producer($conf);
$producer->addBrokers($broker_list);
$topic = $producer->newTopic($topic_name);
$topic->produce(RD_KAFKA_PARTITION_UA, 0, "Message Test");
$producer->poll(0);
$result = $producer->flush(10000);
but nothing returns on stream

Are you trying to connect to confluent cloud?
I would add to your conf your security protocol, as you indicate sasl. eg $conf->set('security.protocol','sasl_ssl');
I would think you would not need to indicate the brokers in the producer, as you have already done this in the config. I am presuming your broker list is a csv formatted file that include host and port number?

Related

Azure File Storage using PHP

I want to run a single webpage to display files, which are stored in an Azure File Storage. It must be Azure File storage because these files came from a Docker container, which is mounted, to that file storage.
I have a Azure Container Instance witch stores PDF files in an Azure File Storage. Now I run a WebApp (PHP) that shall read all the PDFs.
I’ve installed the Microsoft Azure Storage File PHP SDK but have no clue how to use it. Even the sample.php did not help me coming forward. It would be very helpful if someone could help me a bit a simple sniped.
I see you want to directly display a PDF file from Azure File Storage in a web page. Generally, the best practice is to generate the url with sas token of a file in Azure File Storage.
So I followed the GitHub repo Azure/azure-storage-php to install Azure File Storage SDK for PHP in my sample project, here is my sample code and its dependencies.
The file structure of my sample project is as the figure below.
The content of my composer.json file is as below.
{
"require": {
"microsoft/azure-storage-file": "*"
}
}
My sample PHP file demo.php is as below, that's inspired by the function generateFileDownloadLinkWithSAS of the offical sample azure-storage-php/samples/FileSamples.php.
<?php
require_once "vendor/autoload.php";
use MicrosoftAzure\Storage\Common\Internal\Resources;
use MicrosoftAzure\Storage\File\FileSharedAccessSignatureHelper;
$accountName = "<your account name>";
$accountKey = "<your account key>";
$shareName = "<your share name>";
$fileName = "<your pdf file name>";
$now = date(DATE_ISO8601);
$date = date_create($now);
date_add($date, date_interval_create_from_date_string("1 hour"));
$expiry = str_replace("+0000", "Z", date_format($date, DATE_ISO8601));
$helper = new FileSharedAccessSignatureHelper($accountName, $accountKey);
$sas = $helper->generateFileServiceSharedAccessSignatureToken(
Resources::RESOURCE_TYPE_FILE,
"$shareName/$fileName",
'r', // Read
$expiry // A valid ISO 8601 format expiry time, such as '2020-01-01T08:30:00Z'
);
$fileUrlWithSAS = "https://$accountName.file.core.windows.net/$shareName/$fileName?$sas";
echo "<h1>Demo to display PDF from Azure File Storage</h1>";
echo "<iframe src='$fileUrlWithSAS' width='800' height='500' allowfullscreen webkitallowfullscreen></iframe>";
?>
The result of my web page is as the figures below in Chrome and Firefox.
The result in Chrome:
The result in Firefox:
Update: The code to list files in a file share, as below.
<?php
require_once "vendor/autoload.php";
use MicrosoftAzure\Storage\File\FileRestProxy;
$accountName = "<your account name>";
$accountKey = "<your account key>";
$shareName = "<your share name>";
$connectionString = "DefaultEndpointsProtocol=https;AccountName=$accountName;AccountKey=$accountKey";
$fileClient = FileRestProxy::createFileService($connectionString);
$list = $fileClient->listDirectoriesAndFiles($shareName);
function endsWith( $str, $sub ) {
return ( substr( $str, strlen( $str ) - strlen( $sub ) ) === $sub );
}
foreach($list->getFiles() as &$file) {
$fileName = $file->getName();
if(endsWith($fileName, ".pdf")) {
echo $fileName."\n";
}
};
?>

Symfony-Finder cant open dir

I try to read the dir and wanna get the last files from the current date.
When I put the url in the browser I got a result of all the files, which are in the ftp-directory.
So I have the proof, that the ftp-connectionparameter still works.
When I try to start the following function, then I get the error
RecursiveDirectoryIterator::__construct(ftp://...#example.com:4242): failed to open dir: operation failed
Here is the Exception from symfony:
/**
* #Route("/download", name="getfile")
*/
public function getFileWithFtp()
{
$host = "example.com";
$username = "username";
$userpass = "userpass";
$port = 4242;
$url = 'ftp://' . $username . ':' . $userpass . '#' . $host . ':' . $port .'/';
$datum = date('Y-m-d');
$finder = new Finder();
$iterator = $finder
->files()
->in($url)
->name('*BEHWN.TXT')
->date($datum);
$anzahl = count($iterator);
return $this->render('ftp/index.html.twig', [
'controller_name' => 'FtpController',
'url' => $url,
'anzahl' => $anzahl
]);
}
WHen i open the url with the file with file_gets_content($url."filename.txt"), then i get the content without error.
Only it seems that I dont use the Finder from Symfony not correct.
My current Symfony is 4.1.4 and I had cleared the caches and also i deleted the cache-files manually.
Thanks for every tipp
Here is the link to the symfony-finder-component:
https://symfony.com/doc/current/components/finder.html
As the Finder uses PHP iterators, you can pass any URL with a supported protocol:
Here is the part of the FTP code from the documentation:
// always add a trailing slash when looking for in the FTP root dir
$finder->in('ftp://example.com/');
// you can also look for in a FTP directory
$finder->in('ftp://example.com/pub/');
As stated in the docs, what you have in $iterator is a PHP iterator,
so you need to use iterator_count:
$anzahl = iterator_count($iterator);
This WILL not work anymore IF you are using symfony v3.4.7 and above..
There is the issue:
https://github.com/symfony/symfony/issues/27423
I did a fix but it will take a while till it gets merged i think
https://github.com/symfony/symfony/pull/28604
Summary:
"In Finder[...]was released which should remove duplicate slashes from path names.
However, this results in an error when used to find files in an FTP root dir."
"As a result, a working code before v3.4.7 result in breaking code after v3.4.7"
This is the method messing things up, including my fix
https://github.com/symfony/symfony/blob/0670c48477b3d88787b6fe0dec168c5c8ae49c66/src/Symfony/Component/Finder/Finder.php#L741

SSRS SDK For PHP : Not working from PHP 5.5

I'm using the SSRS SDK For PHP which has not been updated since years surprisingly (08.04.2010) .
It works fine under PHP 5.4 (5.4.44) but not under 5.5+ (5.5.37, 5.6.28, 7.x)
The error is :
array ( )Failed to connect to Reporting Service Make sure that the url
(https://myHost:60004/ReportServer/) and credentials are
correct!
The exception is thrown in line 207 because the var $content is equals to FALSE (so can't load the content).
It's perfectly fine with PHP 5.4.44.
I can't find the "SSRS SDK For PHP" documentation regarding the PHP version supported...
I'm not using any framework such as Symfony2, CakePHP, Zend Framework,... except Bootstrap.
Does anyone know or have figured out this issue?
EDIT
As asked in a comment, here is my code :
function buildSSRSReport($companyId)
{
require_once '../tools/SSRSReport/SSRSReport.php';
try {
// Create a connection to the SSRS Server
$rs = new SSRSReport(new Credentials(SSRS_USER_ID, SSRS_PASSWORD), SSRS_REPORT_SERVER_URL);
$sqlConnection = self::getSqlConnection();
// Load the report and specify the params required for its execution
$executionInfo = $rs->LoadReport2(self::getSSRSEnvironment(), NULL);
$parameters = array();
$parameters[0] = new ParameterValue();
$parameters[0]->Name = "CompanyId";
$parameters[0]->Value = $companyId;
$rs->SetExecutionParameters2($parameters);
// Require the Report to be rendered in HTML format
$renderAsHTML = new RenderAsPDF();
// Set the links in the reports to point to the php app
//$renderAsHTML->ReplacementRoot = getPageURL();
// Set the root path on the server for any image included in the report
//$renderAsHTML->StreamRoot = './images/';
// Execute the Report
$result_html = $rs->Render2($renderAsHTML,
PageCountModeEnum::$Actual,
$Extension,
$MimeType,
$Encoding,
$Warnings,
$StreamIds);
$pdfFileName = self::getCompanyAlias($sqlConnection, $companyId) . ".pdf";
header('Content-Type: application/x-download');
header('Content-Disposition: attachment; filename="' . $pdfFileName . '"');
header('Cache-Control: private, max-age=0, must-revalidate');
header('Pragma: public');
return $result_html;
} catch (SSRSReportException $serviceException) {
echo $serviceException->GetErrorMessage();
}
}
function getSSRSEnvironment()
{
return "/DEV/CompanyESGReportingServices-DEV/Main";
}
private function getSqlConnection()
{
require_once("../inc/eth_connexion.php");
/* These 4 variables are contained within the eth_connexion.php file */
$serverName = SQL_SERVERNAME;
$userName = SQL_USERNAME;
$dbName = SQL_DATABASENAME;
$password = SQL_PASSWORD;
// SQLSRV : Connection array used when calling SQL via sqlsrv_query
$connectionInfo = array("Database"=> SQL_DATABASENAME,
"UID"=> SQL_USERNAME,
"PWD"=> SQL_PASSWORD,
"ReturnDatesAsStrings" => true);
$sqlConnection = sqlsrv_connect($serverName, $connectionInfo);
if (!$sqlConnection) {
die('Can't log to the database');
}
return $sqlConnection;
}
And here is the source code of the SSRSReport constructor (original from the SSRS SDK For PHP) :
public function SSRSReport($credentials, $url, $proxy = null)
{
$this->_BaseUrl = ($url[strlen($url) - 1] == '/')? $url : $url . '/';
$executionServiceUrl = $this->_BaseUrl . self::ExecutionService;
$managementServiceUrl = $this->_BaseUrl . self::ManagementService;
$options = $credentials->getCredentails();
$stream_conext_params = array( 'http' =>
array('header' =>
array($credentials->getBase64Auth())));
if(isset($proxy))
{
$options = array_merge($options, $proxy->getProxy());
$stream_conext_params['http']['proxy'] = 'tcp://' .
$proxy->getHost() .
':' .
$proxy->getPort();
if($proxy->getLogin() != null)
{
$stream_conext_params['http']['header'][1] = $proxy->getBase64Auth();
}
}
/**
* If the SoapClient call fails, we cannot catch exception or supress warning
* since it throws php fatal exception.
* http://bugs.php.net/bug.php?id=34657
* So try to load the wsdl by
* calling file_get_contents (with warning supressed i.e. using # symbol
* infront of the function call)
* http://stackoverflow.com/questions/272361/how-can-i-handle-the-warning-of-filegetcontents-function-in-php
*/
$context = stream_context_create($stream_conext_params);
$content = #file_get_contents($executionServiceUrl, false, $context);
if ($content === FALSE) // I'M GOING HERE WITH PHP 5.5+
{
throw new SSRSReportException("",
"Failed to connect to Reporting Service <br/> Make sure " .
"that the url ($this->_BaseUrl) and credentials are correct!");
}
$this->_soapHandle_Exe = new SoapClient ($executionServiceUrl, $options);
$this->_soapHandle_Mgt = new SoapClient ($managementServiceUrl, $options);
$this->ClearRequest();
}
The exact error message is :
https://myPublicHostProvider:60004/ReportServer/ReportExecution2005.asmx?wsdl
Authorization: Basic = array(1) {
["http"]=> array(1) { ["header"]=> array(1) { [0]=> string(57)
"Authorization: Basic =" } } }
Failed to connect to Reporting Service
Make sure that the url (https://myPublicHostProvider:60004/ReportServer/) and credentials are correct!
SSRS SDK for PHP Configuration Pointers
Make sure your Report Server instance is accessible. Test it by going to the Web Portal or the Service URL.
Provide your credentials. If you are authenticated, then the credentials that you used are valid.
After that, go to the directory \Program Files\Microsoft SQL Server\<INSTANCE_NAME>\Reporting Services\ReportServer and look for the file named rsreportserver.config
Find this snippet
<Authentication>
<AuthenticationTypes>
<RSWindowsNTLM/>
</AuthenticationTypes>
<Authentication>
Add <RSWindowsBasic/> below the <RSWindowsNTLM/> entry.
For your reference, this was the snippet I used to test my instance during configuration.
require_once( 'SSRSReport\bin\SSRSReport.php' );
define( 'UID', 'DOMAIN\Username' );
define( 'PASWD', 'Password' );
define( 'SERVICE_URL', 'http://127.0.0.1/SERVICE_URL/' );
try {
$ssrs_report = new SSRSReport( new Credentials( UID, PASWD ), SERVICE_URL );
$ssrs_report->LoadReport2( '/test', NULL );
$renderAsHTML = new RenderAsHTML();
$result_html = $ssrs_report->Render2(
$renderAsHTML,
PageCountModeEnum::$Estimate,
$Extension,
$MimeType,
$Encoding,
$Warnings,
$StreamIds
);
echo $result_html;
} catch ( SSRSReportException $serviceException ) {
echo $serviceException->GetErrorMessage();
}
It can successfully load the RDL that is located on the load path.
Thanks for your help MiSAKACHi. It pointed me on the right direction. Actually, I'm using a remote dedicated server on a hosting provider on which I don't have access. But the <RSWindowsBasic/> is there as it can load the report with PHP 5.4. Everything is fine under this version.
Thanks to you, I found the issue : I'm using HTTPS and with HTTPS it doesn't work whereas it works when I'm using HTTP !
I'm check with my hosting provider if they can do something.
EDIT - Solution
My hosting provider fixed the issue last year (forgot to post it here). During the time it wasn't fixed, I was still under PHP 5.4.
I paste their original message :
These issues that you describe is because the installed Certificate is a self signed Certificate. New Web Browsers and PHP Version don't accept these Certificates because of Security Risks.
We have changed the Certificate with a official issued Certificate
I just want to add that I was facing issue using the codeplex ssrsphp, therefore I googled and found another sdk, which works perfectly. I'm using php 7+. Here is the link and I'm available if there're any questions related to this.

Connect to azure-blob through azure-website with php

I have set up an windows azure website (php), and I want to connect to the azure storage (blob) environment. I walked through the How to use the Blob service from PHP tutorial, but that only mentions the case when the website is stored localy.
I tried to set up a few cases, but i'm constantly getting a http 500 error.
<?php
require_once 'WindowsAzure/WindowsAzure.php';
use WindowsAzure\Common\ServicesBuilder;
use WindowsAzure\Common\ServiceException;
//$connectionString = "\WindowsAzure\Blob\Internal\IBlob";
// Create blob REST proxy.
$blobRestProxy = ServicesBuilder::getInstance()->createBlobService($connectionString); // the code gets stuck at this line, result is a HTTP 500 error
$content = fopen("C:\Users\Public\Pictures\Sample%20Pictures\Woestijn.jpg", "r");
$blob_name = "newBlob";
try {
//Upload blob
$blobRestProxy->createBlockBlob("default", $blob_name, $content);
}
catch(ServiceException $e){
// Handle exception based on error codes and messages.
// Error codes and messages are here:
// http://msdn.microsoft.com/en-us/library/windowsazure/dd179439.aspx
$code = $e->getCode();
$error_message = $e->getMessage();
echo $code.": ".$error_message."<br />";
}?>
Is there anyone who had a similar problem and managed to figure it out?
EDIT:
I now narrowed down the error search. I went into the ServicesBuilder.php file, and commented out line by line, until the page stopped to work. The line it went wrong at is $httpClient, as shown below:
public function createBlobService($connectionString)
{
$settings = StorageServiceSettings::createFromConnectionString(
$connectionString
);
$httpClient = $this->httpClient();
$serializer = $this->serializer();
$uri = Utilities::tryAddUrlScheme(
$settings->getBlobEndpointUri()
);
From what I'm seeing you're filling up the $connectionString variable with this value: "\WindowsAzure\Blob\Internal\IBlob" (even though it's commented - so probably you're passing it from somewhere else). If that's the case you'll need to change it.
The connection string should be a reference to your storage account containing the protocol, the name of the account and the key (you can find the name and the key in the portal):
$connectionString = "DefaultEndpointsProtocol=https;AccountName=jeroensaccount;AccountKey=fjmezfjmIOFJEZIOPAFJAZOPIFJAIMO"

Transfer Files from Rackspace CloudSite to Cloud Files

Just wondering if anyone knows of a prewritten php script to to transfer files from rackspace cloud site to rackspace cloud files.
Rackspace does provide a script to backup files to the cloud but not transfer. (and I only realized that after spending a couple of hours and finally getting the script working).
http://www.rackspace.com/knowledge_center/article/how-to-use-cron-to-backup-cloudsites-to-cloudfiles
I don't know PHP (which is required for Rackspace cron jobs), so if there's a script that would help me with this it would be great.
Thanks.
Below is the script I use when I backup to rackspace. It uses the php cloud files master library from racker labs. I set it up as a simple cron. Simply replace the emai
php 'path/to/backup.php'
<?
require_once('php-cloudfiles-master/cloudfiles.php');
$server_name = 'YOUR_SERVER_NAME'; //Name of the Current Server
$curr_date_time = date("Y-m-d--H:i:s"); //DON' CHANGE - Date
$curr_day = date("Yd"); //DON' CHANGE - Date
$sites = array("ENTERDATABASES HERE IN ARRAY FORMAT"); //Array of Databases to be Backed up
$site_root = "/var/www/";
$temp_dir = "/bak/mysql/"; //temp directory
$site_temp_dir = "/bak/site/"; //temp directory
$rscfUsername = 'YOUR RACKSPACE USERNAME'; // the username for your rackspace account
$rscfApiKey = 'YOUR RACKSPACE API KEY'; // the api key for your account
$rscfContainer = 'YOUR RACKSPACE CONTAINER'; //rackspace containr
foreach($sites as $site) {
try{
exec("tar -czpf {$site_temp_dir}{$site}{$curr_date_time}_site.tar.gz {$site_root}{$site}");
// check if the file is created
if(file_exists("{$site_temp_dir}{$site}{$curr_date_time}_site.tar.gz")) {
$auth = new CF_Authentication($rscfUsername, $rscfApiKey);
$auth->ssl_use_cabundle();
$auth->authenticate();
$conn = new CF_Connection($auth);
$conn->ssl_use_cabundle();
// I allready created a container with this name otherwise user create_container
$container = $conn->get_container($rscfContainer);
// make a unique name with date in it so you know when it was created
$objectName = $site.$curr_date_time.'.tar.gz';
$store = $container->create_object($objectName);
$store->load_from_filename("{$site_temp_dir}{$site}{$curr_date_time}_site.tar.gz"); // send to rackspace
}
} catch (Exception $e) {
print_r($e, true);
}
}
?>

Categories