PHP IMAP connection to Exchange works but throws errors - php

Searched all through Server Fault and Stack Overflow, but cannot find any useful information regarding this issue I have. There are plenty of questions regarding getting connected to Exchange, but that's not where my issue is.
I am running php-imap (latest via composer) and have setup a PHP script to connect to Office 365/Exchange to check for bounced messages. The script executes and works correctly, however throws some confusing connection errors.
The basic script:
$imap_server = 'outlook.office365.com';
$imap_folder = 'INBOX';
$imap_user = '((myemailaddress))';
$imap_pass = '((mypasswrod))';
$mailbox = new PhpImap\Mailbox ('{' . $imap_server . '}' . $imap_folder, $imap_user, $imap_pass);
try {
$mailsIds = $mailbox->searchMailbox('ALL', true);
print_r ($mailsIds);
} catch(PhpImap\Exceptions\ConnectionException $ex) {
echo "IMAP connection failed: " . $ex;
die();
}
Output:
Array
(
[0] => 47
[1] => 49
[2] => 51
[3] => 53
[4] => 55
[5] => 57
[6] => 59
)
Notice: Unknown: Connection failed to MNZ-efz.ms-acdc.office.com,143: No route to host (errflg=1) in Unknown on line 0
Notice: Unknown: Connection failed to MNZ-efz.ms-acdc.office.com,143: No route to host (errflg=1) in Unknown on line 0
Notice: Unknown: Connection failed to MNZ-efz.ms-acdc.office.com,143: No route to host (errflg=1) in Unknown on line 0
Notice: Unknown: Connection failed to MNZ-efz.ms-acdc.office.com,143: No route to host (errflg=1) in Unknown on line 0
EDIT:
Further - it appears all emails in the INBOX are being deleted upon execution of the script. Not sure why... but it seems it would have something to do with whatever is happening here in the background that is causing those errors.

Related

ssh2_scp_send not accepting array variable more than 9

this is my php script, here iam trying to upload photos to other server using SSH in that when i scan directory and store it in an array.
then i try to run in terminal upto index 9 its uploading properly, issue come when index comes at 10...........................................................................................................................................
$username = 'root';
$password = 'Workflow#5992$';
$connection = ssh2_connect($host, 22);
ssh2_auth_password($connection, $username, $password);
$sftp = ssh2_sftp($connection);
// Specifying directory
$mydir = '/var/www/html/AcadWFReport/photos';
// Scanning files in a given directory in ascending order
$myfiles = scandir($mydir);
// Displaying the files in the directory
print_r($myfiles);
$c = count($myfiles);
//print_r($c);
for ($i = 2; $i < $c; $i++){
$fname= substr($myfiles[$i],0,8);
$ext= substr($myfiles[$i],9,3);
$b= substr($myfiles[$i],2,2);
$batch=20 . $b ;
$pid= substr($myfiles[$i],4,1);
$pname=$myfiles[$i];
$result1=ssh2_sftp_mkdir($sftp, '/var/www/html/Dev/File_upload/doc/byroll/'.$batch.'');
$result2=ssh2_sftp_mkdir($sftp, '/var/www/html/Dev/File_upload/doc/byroll/'.$batch.'/'.$pid.'');
$local_file = '/var/www/html/AcadWFReport/photos/'.$pname.'';
$remote_file = '/var/www/html/Dev/File_upload/doc/byroll/'.$batch.'/'.$pid.'/'.$pname.'';
echo 'Uploading...'.$pname.''.$i.''."\n";
$stream = file_exists("ssh2.sftp://$connection/var/www/html/Dev/File_upload/doc/byroll/".$batch."/".$pid."/".$pname."");
if($stream){
echo "File already Exits";
$new_name= $fname.'_'.date('dmY').'.'.$ext;
ssh2_sftp_rename($sftp, '/var/www/html/Dev/File_upload/doc/byroll/'.$batch.'/'.$pid.'/'.$pname.'', '/var/www/html/Dev/File_upload/doc/byroll/'.$batch.'/'.$pid.'/'.$new_name.'');
}
$result = ssh2_scp_send($connection, $local_file, $remote_file, 0644);
if(!$result) {
echo 'Error while uploading';
}
else{
echo 'Uploaded successfully!'."\n";
}
}
and my output is
below
[root#localhost AcadWFReport]# php transfer.php
Array
(
[0] => .
[1] => ..
[2] => ME18S010.JPG
[3] => MM17S001.JPG
[4] => MS12B043.JPG
[5] => MS15F001.JPG
[6] => MS16D003.JPG
[7] => MS19S034.JPG
[8] => MS20B036.JPG
[9] => MS21D003.JPG
[10] => MS21W002.JPG
[11] => MS21W004.JPG
[12] => MS21W005.JPG
[13] => MS21W007.JPG
[14] => MS21W008.JPG
[15] => MS21W009.JPG
[16] => MS21W010.JPG
[17] => MS21W011.JPG
[18] => MS21W012.JPG
[19] => MS21W014.JPG
)
Uploading...ME18S010.JPG2
File already ExitsUploaded successfully!
Uploading...MM17S001.JPG3
File already ExitsUploaded successfully!
Uploading...MS12B043.JPG4
File already ExitsUploaded successfully!
Uploading...MS15F001.JPG5
File already ExitsUploaded successfully!
Uploading...MS16D003.JPG6
File already ExitsUploaded successfully!
Uploading...MS19S034.JPG7
File already ExitsUploaded successfully!
Uploading...MS20B036.JPG8
File already ExitsUploaded successfully!
Uploading...MS21D003.JPG9
File already ExitsUploaded successfully!
Uploading...MS21W002.JPG10
File already ExitsPHP Warning: ssh2_scp_send(): Failure creating remote file: Channel open failure (connect failed) (-21) in /var/www/html/AcadWFReport/transfer.php on line 60
Error while uploadingUploading...MS21W004.JPG11
PHP Warning: ssh2_scp_send(): Failure creating remote file: Channel open failure (connect failed) (-21) in /var/www/html/AcadWFReport/transfer.php on line 60
Error while uploadingUploading...MS21W005.JPG12
PHP Warning: ssh2_scp_send(): Failure creating remote file: Channel open failure (connect failed) (-21) in /var/www/html/AcadWFReport/transfer.php on line 60
Error while uploadingUploading...MS21W007.JPG13
PHP Warning: ssh2_scp_send(): Failure creating remote file: Channel open failure (connect failed) (-21) in /var/www/html/AcadWFReport/transfer.php on line 60
Error while uploadingUploading...MS21W008.JPG14
PHP Warning: ssh2_scp_send(): Failure creating remote file: Channel open failure (connect failed) (-21) in /var/www/html/AcadWFReport/transfer.php on line 60
Error while uploadingUploading...MS21W009.JPG15
PHP Warning: ssh2_scp_send(): Failure creating remote file: Channel open failure (connect failed) (-21) in /var/www/html/AcadWFReport/transfer.php on line 60
Error while uploadingUploading...MS21W010.JPG16
PHP Warning: ssh2_scp_send(): Failure creating remote file: Channel open failure (connect failed) (-21) in /var/www/html/AcadWFReport/transfer.php on line 60
Error while uploadingUploading...MS21W011.JPG17
PHP Warning: ssh2_scp_send(): Failure creating remote file: Channel open failure (connect failed) (-21) in /var/www/html/AcadWFReport/transfer.php on line 60
Error while uploadingUploading...MS21W012.JPG18
PHP Warning: ssh2_scp_send(): Failure creating remote file: Channel open failure (connect failed) (-21) in /var/www/html/AcadWFReport/transfer.php on line 60
Error while uploadingUploading...MS21W014.JPG19
PHP Warning: ssh2_scp_send(): Failure creating remote file: Channel open failure (connect failed) (-21) in /var/www/html/AcadWFReport/transfer.php on line 60 ```
and after index 9 it not allowing any double digit number to it
You start SFTP session (ssh2_sftp), while you use SCP later for actual transfer (ssh2_scp_send). You definitely do not need the ssh2_sftp line; and it can actually be a cause of your problem.
While technically it is possible to have both SFTP and SCP sessions over one SSH connection, I would not expect PHP to support this. Though I'm not sure.
and I commented the SFTP part in my code and uploaded more than 50 file though ssh2
And its working perfectly fine all 57 files have been moved to remote server from local server

imap_open(): Couldn't open stream on linux server

My code is working fine when run on windows laptop with XAMPP, but when I try to run same code on linux machine it is giving error.
My code:
<?php
$connect_to = '{imap.gmail.com:993/imap/ssl}INBOX';
$user = 'my gmail address';
$password = 'my gmail password';
$connection = imap_open($connect_to, $user, $password)
or die("Can't connect to '$connect_to': " . imap_last_error());
imap_close($connection);
?>
Error I am getting:
PHP Warning:imap_open(): Couldn't open stream {imap.gmail.com:993/imap/ssl}INBOX in /home/re-work/test_imap.php on line 30
Array
(
[type] => 2
[message] => imap_open(): Couldn't open stream {imap.gmail.com:993/imap/ssl}INBOX
[file] => /home/re-work/test_imap.php
[line] => 30
)
PHP Warning: imap_num_msg() expects parameter 1 to be resource, boolean given in /home/re-work/test_imap.php on line 39
PHP Warning: imap_search() expects parameter 1 to be resource, boolean given in /home/re-work/test_imap.php on line 42
0</br>PHP Notice: Unknown: Retrying PLAIN authentication after [ALERT] Please log in via your web browser: https://support.google.com/mail/acco (errflg=1) in Unknown on line 0
PHP Notice: Unknown: Retrying PLAIN authentication after [ALERT] Please log in via your web browser: https://support.google.com/mail/acco (errflg=1) in Unknown on line 0
PHP Notice: Unknown: Can not authenticate to IMAP server: [ALERT] Please log in via your web browser: https://support.google.com/mail/acco (errflg=2) in Unknown on line 0
Please help where and what I am missing?

imap_open(): Couldn't open stream

I am trying to access my gmail inbox using imap. I have applied almost all the possible solutions available here and also in google. But i am facing the same issue.
Below is my code :
$hostname = "{imap.gmail.com:993/imap/ssl/novalidate-cert}";
$mailbox = imap_open($hostname, 'my gmail account', 'my gmail password');
if ($mailbox)
{
echo 'if';
}
else
{
echo 'else';
}
exit;
When i run this code in to my local system it will give me some message like this :
Warning: imap_open(): Couldn't open stream {imap.gmail.com:993/imap/ssl/novalidate-cert} in D:\xampp\htdocs\php-imap-mailboxes\test-mail.php on line 4
else
Notice: Unknown: [ALERT] Please log in via your web browser: https://support.google.com/mail/accounts/answer/78754 (Failure) (errflg=1) in Unknown on line 0
Notice: Unknown: [ALERT] Please log in via your web browser: https://support.google.com/mail/accounts/answer/78754 (Failure) (errflg=1) in Unknown on line 0
Notice: Unknown: [ALERT] Please log in via your web browser: https://support.google.com/mail/accounts/answer/78754 (Failure) (errflg=1) in Unknown on line 0
Notice: Unknown: Too many login failures (errflg=2) in Unknown on line 0
I am running this script in to my local system. Please help me in to this as i have already spent lot's of time in this issue.
Thanks,
Ali
Make sure you have OpenSSL installed.
Here is a related discussion on google groups.
https://groups.google.com/forum/#!topic/comp.lang.php/JB5hm8cKi_Q

Error fetching data from youtube in xml format

I am trying to fetch youtube videos using Youtube Data API URL. Whenever i paste the URL directly in the address bar, it perfectly shows the result. But whenever i try to use that URL in simplexml_load_file() function, i get errors. I cannot figure out what is going on.
This is how i am doing this.
$url = "https://gdata.youtube.com/feeds/api/videos/-/cat";
$output = simplexml_load_file($url);
echo "<pre>";
print_r($output);
echo "</pre>";
I am getting this error message.
Warning: simplexml_load_file(): SSL: The operation completed successfully. in J:\xampp\htdocs\youtube\tube.php on line 23
Notice: simplexml_load_file(): send of 38 bytes failed with errno=10054 An existing connection was forcibly closed by the remote host. in J:\xampp\htdocs\youtube\tube.php on line 23
Notice: simplexml_load_file(): send of 25 bytes failed with errno=10054 An existing connection was forcibly closed by the remote host. in J:\xampp\htdocs\youtube\tube.php on line 23
Notice: simplexml_load_file(): send of 2 bytes failed with errno=10054 An existing connection was forcibly closed by the remote host. in J:\xampp\htdocs\youtube\tube.php on line 23
Warning: simplexml_load_file(https://gdata.youtube.com/feeds/api/videos/-/cat): failed to open stream: HTTP request failed! in J:\xampp\htdocs\youtube\tube.php on line 23
Warning: simplexml_load_file(): I/O warning : failed to load external entity "https://gdata.youtube.com/feeds/api/videos/-/cat" in J:\xampp\htdocs\youtube\tube.php on line 23
I tried to use file_get_contents() function it also gives error.
$url = "https://gdata.youtube.com/feeds/api/videos/-/cat";
$output = file_get_contents($url);
echo $output;
I get these errors using above code.
Warning: file_get_contents(): php_network_getaddresses: getaddrinfo failed: No such host is known. in J:\xampp\htdocs\youtube\tube.php on line 15
Warning: file_get_contents(https://gdata.youtube.com/feeds/api/videos/-/cat): failed to open stream: php_network_getaddresses: getaddrinfo failed: No such host is known. in J:\xampp\htdocs\youtube\tube.php on line 15
I also tried to use simplexml_load_string() function it also gives error.
$url = "https://gdata.youtube.com/feeds/api/videos/-/cat";
$output = simplexml_load_string($url);
echo "<pre>";
print_r($output);
echo "</pre>";
The above code gives me these errors.
Warning: simplexml_load_string(): Entity: line 1: parser error : Start tag expected, '<' not found in J:\xampp\htdocs\youtube\tube.php on line 14
Warning: simplexml_load_string(): https://gdata.youtube.com/feeds/api/videos/-/cat in J:\xampp\htdocs\youtube\tube.php on line 14
Warning: simplexml_load_string(): ^ in J:\xampp\htdocs\youtube\tube.php on line 14
I am using XAMPP v3.2.1 to run my script.
Please help. Thanks.
First you have to check that remote file access is enabled, read the PHP manual page about Using remote files.
After that, and if your firewall is not blocking connections, almost any option that you tried should work. But, in any case, here you have one using simpler commands, or at least simpler concerning what is working internally. Just a good old fopen.
// initialize variables
$url = 'https://gdata.youtube.com/feeds/api/videos/-/cat';
$content = false;
$handle = false;
// open the url/file
$handle = fopen( $url, 'r' );
// get the content from streaming
$content = stream_get_contents($handle);
// close the file reading session
fclose( $handle );
var_dump( $content );

file_get_contents(http://localhost:51089): no such host is known

I am following step-by-step guide of google app engine "Using the User Service", but after I ran the app server locally
C:\Users\xxx>dev_appserver.py --php_exectuable_path="C:\Users\xxx\Desktop\php54\php-cgi.exe" C:\Users\xxx\Desktop\FirstPhpPrj\
below errors pop up:
Warning: file_get_contents(): php_network_getaddresses: getaddrinfo failed:
No such host is known. in <b>C:\Users\xxx\Desktop\google_appengine\php\sdk\
google\appengine\runtime\RemoteApiProxy.php</b> on line 92
Warning:
file_get_contents(http://localhost:51089)
: failed to open stream:
php_network_getaddresses: getaddrinfo failed: No such host is known. in C
:\Users\xxx\Desktop\google_appengine\php\sdk\google\appengine\runtime\RemoteApiP
roxy.php on line 92
Fatal error: Uncaught exception 'google\net\ProtocolBufferDecodeError'
with message 'Not initialized: login_url' in C:\Users\xxx\Desktop\google_appengin
e\php\sdk\google\appengine\runtime\proto\ProtocolMessage.php:121
Stack trace:
#0 C:\Users\xxx\Desktop\google_appengine\php\sdk\google\appengine\runtime\proto\
ProtocolMessage.php(88): google\net\ProtocolMessage->mergeFromString('')
#1 C:\Users\xxx\Desktop\google_appengine\php\sdk\google\appengine\runtime\Remote
ApiProxy.php(109): google\net\ProtocolMessage->parseFromString('')
#2 C:\Users\xxx\Desktop\google_appengine\php\sdk\google\appengine\runtime\ApiProxy.php(42): google\appengine\runtime\RemoteApiProxy->makeSyncCall('user', 'CreateLoginURL', Object(google\appengine\CreateLoginURLRequest), Object(google\app
engine\CreateLoginURLResponse), NULL)
#3 C:\Users\xxx\Desktop\google_appengine\php\sdk\google\appengine\api
\users\UserService.php(69): google\appengine\runtime\ApiProxy::makeSyncCall('user', 'CreateLoginURL', Object(google\appengine\CreateLoginURLRequest), Object(google in C
:\Users\xxx\Desktop\google_appengine\php\sdk\google\appengine\runtime\proto\ProtocolMessage.php on line 121
I browsed the log, there are two warnings and one fatal error. But not after I looked thru the coding of php sdk, i realized that this fatal error, caused by empty string input in "mergeFromString", is in fact caused by the warnings of"
file_get_contents(http://localhost:51089)
no such host is known", because in RemoteApiProxy.php, function CreateLoginURLResponse:
$serialized_remote_respone = file_get_contents(
'http://localhost:' . $this->apiPort, false, $context);
once serialized_remote_response gets an empty result, it will affect the command in next line:
$remote_response->parseFromString($serialized_remote_respone);
In this case, my own codes are actually irrelevant as it is exactly the same as the google sample codes:here.
I just want to figure out why on earth, after google app engine starts its API server at
http://localhost:51089
yet it is not recognized! I tried pin this host and windows also threw error!

Categories