fopen(): SSL: Connection reset by peer error in php - php

I'm trying to download a report from my bing ads account and i'm encountering the following errors:
Warning: fopen(): SSL: Connection reset by peer in xxxx...
Warning: fopen(): Failed to enable crypto in xxx...
function PollGenerateReport($proxy, $reportRequestId)
{
// Set the request information.
$request = new PollGenerateReportRequest();
$request->ReportRequestId = $reportRequestId;
return $proxy->GetService()->PollGenerateReport($request)->ReportRequestStatus;
return $proxy->GetService()->PollGenerateReport($request)>ReportRequestStatus;
}
// Using the URL that the PollGenerateReport operation returned,
// send an HTTP request to get the report and write it to the specified
// ZIP file.
function DownloadFile($reportDownloadUrl, $downloadPath)
{
if (!$reader = fopen($reportDownloadUrl, 'rb'))
{
throw new Exception("Failed to open URL " . $reportDownloadUrl . ".");
}
if (!$writer = fopen($downloadPath, 'wb'))
{
fclose($reader);
throw new Exception("Failed to create ZIP file " . $downloadPath . ".");
}
$bufferSize = 100 * 1024;
while (!feof($reader))
{
if (false === ($buffer = fread ($reader, $bufferSize)))
{
fclose($reader);
fclose($writer);
throw new Exception("Read operation from URL failed.");
}
if (fwrite($writer, $buffer) === false)
{
fclose($reader);
fclose($writer);
$exception = new Exception("Write operation to ZIP file failed.");
}
}
fclose($reader);
fflush($writer);
fclose($writer);
}
Since i'm a newbie to php, i'm asking for any assistance/tipps on how to convert the fopen() function (which from research seems to be the problem here) to curl. I'm using the bing API to download the report and running the script on a server.
Thanks.

My first idea is that the URL might be password protected?
If it is possible it would be better to export the report and then import it on your server.
Alternatively see if BING has documentation on how to access their reports externally, is there an API (Application Protocol Interface)?

Related

How to read csv file from zip file on remote server with php?

I would like to read a zip file from a remote server. I currently have to following code :
private $sftp;
public function __construct($username, $password, $serverName)
{
$this->sftp = new Net_SFTP($serverName);
if (!$this->sftp->login($username, $password)) {
exit('Login Failed');
}
}
public function buildRecords() {
$sftp = $this->sftp;
$sftp -> chdir(Constants::QUICKCHECK_OUTPUT_DIRECTORY);
echo $sftp->pwd(); // show that we're in the 'test' directory
// print_r($sftp->nlist());
foreach($sftp->nlist() as $zipFile) {
$handle = fopen("zip://" . $zipFile,'r');
while($line = fgetcsv($handle)) {
print_r($line);
}
}
}
When I run this code and call these methods I get the error
Warning: fopen(zip://test.zip): failed to open stream: operation failed in /var/www/html/update_alerts2.php on line 67
How do I fix this error? (I'm using the phpseclib to sftp)
fopen will not magically be able to access files on a remote server only because you have logged into the server using phpseclib before.
You have to use phpseclib functions to retrieve the file contents.
Unfortunately phpseclib does not offer a way to read remote file contents by lines/chunks. But as it is CSV file, it is probably OK to load from file to memory at once. For that you can use SFTP::get, if you do not specify the $local_file argument:
$contents = $sftp->get($zipFile);
$lines = explode("\n", $contents);
foreach ($lines as $line)
{
if (strlen($line) > 0)
{
$fields = str_getcsv($line);
print_r($fields);
}
}

How to get live streaming data of Server Sent Events using php?

Hi I'm trying server sent events(SSE) using php, I have a https url where I get the live streaming data. Below is my script where I'm trying in infinite loop.
PHP:
<?php
while(1)
{
$get_stream_data = fopen('https://api.xyz.com:8100/update-stream/connect', 'r');
if($get_stream_data)
{
$stream_data = stream_get_contents($get_stream_data);
$save_stream_data = getStreamingData($stream_data);
if($save_stream_data == true)
{
continue;
}
}
else
{
sleep(1);
continue;
}
}
function getStreamingData($stream_data)
{
$to = "accd#xyz.com";
$subject = "Stream Details";
$msg = "Stream Details : ".$stream_data;
$headers = "From:streamdetail#xyz.com";
$mailsent = mail($to,$subject,$msg,$headers);
if($mailsent){
return true;
}else {
return false;
}
}
?>
Error:
Warning: fopen(https://api.xyz.com:8100/update-stream/connect): failed to open stream: Connection timed out in /home/public_html/get_stream_data/index.php on line 4
I couldn't get the data by my end while it is giving an updates by the server in live.
I checked that live streaming in a command prompt using below command.
CURL
curl --get 'https://api.xyz.com:8100/update-stream/connect' --verbose
First, this is best done with PHP's curl functions. See the various answers to PHP file_get_contents() returns "failed to open stream: HTTP request failed!"
If you stick with fopen() you probably need to set up the context for SSL, and it may involve installing some certificates. See file_get_contents(): SSL operation failed with code 1. And more (and note the security warning about the accepted answer)
Finally, your while(1) loop is around the fopen() (which is okay for re-starts after relatively rare failures), but you actually want it inside. Here is your code with just the minimal changes to show that:
<?php
while(1)
{
$get_stream_data = fopen('https://api.xyz.com:8100/update-stream/connect', 'r');
if($get_stream_data)while(1)
{
$stream_data = stream_get_contents($get_stream_data);
$save_stream_data = getStreamingData($stream_data);
if($save_stream_data == true)
{
continue;
}
sleep(1);
}
else
{
sleep(1);
continue;
}
}
UPDATE: The above code still nags at me: I think you want me to using fread() instead of stream_get_contents(), and use blocking instead of the sleep(1) (in the inner loop).
BTW, I'd suggest changing the outer-loop sleep(1) to be sleep(3) or sleep(5) which are typical defaults in Chrome/Firefox/etc. (Really, you should be looking for the SSE server sending a "retry" header, and using that number as the sleep.)

How to read content of socket stream in PHP stream socket server

I'm looking for solution to read and process message from stream within created socket server.
I've come to this:
$server = stream_socket_server("tcp://0.0.0.0:8000", $errno, $errorMessage);
for (;;)
{
$client = #stream_socket_accept($server);
if ($client)
{
echo 'Connection accepted from '.stream_socket_get_name($client, false) . "n";
stream_copy_to_stream($client, $client);
fclose($client);
}
}
which simply echoes whatever is sent to stream. I cannot find out what do I need to do, say, between stream_socket_accept() and fclose() to get contents of message and act upon it. Any help? Thanks in advance.

Download Campaign Performance Reports using Bings Ads in PHP

I was struck in this since a weeks. Please tell me if any one can help me out from this.
I tried this samples they given. I'm trying to download only campaign performance reports, Where i'm able to download a zip file which has a csv file in it. Here is the another direct example i followed for keywords and did the same way for campaign performance. Which giving me a link to download the reports. When i'm trying to download the url manually i can download but I cannot download it through my code.
function DownloadFile($reportDownloadUrl, $downloadPath) {
if (!$reader = fopen($reportDownloadUrl, 'rb')) {
throw new Exception("Failed to open URL " . $reportDownloadUrl . ".");
}
if (!$writer = fopen($downloadPath, 'wb')){
fclose($reader);
throw new Exception("Failed to create ZIP file " . $downloadPath . ".");
}
$bufferSize = 100 * 1024;
while (!feof($reader)) {
if (false === ($buffer = fread($reader, $bufferSize))) {
fclose($reader);
fclose($writer);
throw new Exception("Read operation from URL failed.");
}
if (fwrite($writer, $buffer) === false) {
fclose($reader);
fclose($writer);
$exception = new Exception("Write operation to ZIP file failed.");
}
}
fclose($reader);
fflush($writer);
fclose($writer);
}
But I couldn't download the file. I can't move forward from there so any help like downloading reports in any-other form or the simple code changes in current method is greatly appreciated. Thanks in advance.
When I tried this I also had problems with the DownloadFile function so replaced this with a different version
function DownloadFile($reportDownloadUrl, $downloadPath) {
$url = $reportDownloadUrl;
// Example for the path would be in this format
// $path = '/xxx/yyy/reports/keywordperf.zip';
// using the server path and not relative to the file
$path = $downloadPath;
$fp = fopen($path, 'w');
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_SSLVERSION, 3);
curl_setopt($ch, CURLOPT_FILE, $fp);
if($result = curl_exec($ch)) {
$status = true;
}
else
{
$status = false;
}
curl_close($ch);
fclose($fp);
return status;
}

Error on ftp-file close operation

I'm using php to access file by ftp.
My file read function is:
function _rfile ($file = null) {
if ( is_readable($file) ) {
if ( !($fh = fopen($file, 'r')) ) return false;
$data = fread($fh, filesize($file));
fclose($fh);
return $data;
}
return false;
}
I am getting an error on file close operation in this function with one of my ftp host.
error:
fclose(): FTP server error 550:550 The specified network name is no
longer available.
This function worked fine for some other ftp host's.
Could smb advice a solution?
It may due to the access permission settings.
Please look at this link. http://forums.iis.net/t/1107644.aspx

Categories