Difference between php commandline and server - php

If I run
php -r 'print_r(file_get_contents("http://mirror.facebook.net/centos/timestamp.txt"));'
I get "Tue Oct 28 13:24:01 UTC 2014" (as expected).
But if I have a php file:
<?php
print_r(file_get_contents("http://mirror.facebook.net/centos/timestamp.txt"));
It gives me:
**Warning:** file_get_contents(): php_network_getaddresses: getaddrinfo failed: Name or service not known ...
**Warning:** file_get_contents(http://mirror.facebook.net/centos/timestamp.txt): failed to open stream: php_network_getaddresses: getaddrinfo failed: Name or service not known ...
What is the difference between "php -r" and php running the code out of a file
UPDATE
This is not the answer to the question but it did solve my problem so for the sake of future googlers: I don't know which of these two solutions was the key but it was working when I restarted apache.
In resolv.conf set nameserver 8.8.8.8 (which had my router's address)
There's an SELinux boolean httpd_can_network_connect which must be turned on:
setsebool -P httpd_can_network_connect 1
See this solution and this one.

Your PHP CLI is using a different php.ini (or maybe no php.ini at all). It's also possible you are running a completely different PHP binary than what your server is using.
You can run php -i on the command line, and phpinfo() in a script to determine which php.ini is in use.

Related

php command is not recognized in wamp

I'm working on websocket. I came across this article, and simply downloaded their file and try running it in my localhost.
https://www.sanwebe.com/2013/05/chat-using-websocket-php-socket
What I understand is, they want to sue local server's websocket server.
But I have problem in starting up the server. I'm using windows 10 with wamp 2.2. As I checked webscket is enabled in my php.ini's extention.
I followed this example to cmd the right path to start it but to no avail:
https://www.sanwebe.com/2013/05/chat-using-websocket-php-socket/comment-page-1#comment-5593
It says 'php.exe' is not recognized as an internal or external ...
I then searched online again, thus set the path to my php folder in the system's environment variable. The path is: C:\wamp\bin\php. Then I closed the cmd and relaunched. Nothing worked out. The same error shows up.
This is what I did on cmd:
1) cd C:\wamp\bin\php
2) php.exe -q C:\projects\myfolder\server.php
Please help me to connect to wamp server's websocket to run the example I've downloaded.In the console, the error shown is:
WebSocket connection to 'ws://localhost:9000/demo/server.php' failed: Error in connection establishment: net::ERR_CONNECTION_REFUSED
I found the answer from this site: http://rodrixar.blogspot.my/2011/07/how-to-run-php-sockets-in-wamp.html
I did the following,
1) open cmd
2) cd C:\wamp\bin\php\php5.6.19
3)php.exe -q C:\projects\mysite\server.php
4) Firewall open up and allow access for CLI
5) now connection made already..

Nginx + php-fpm: Bad gateway only when xdebug server is running

Problem
When xdebug server is running from IntelliJ IDEA, I get 502 Bad Gateway from nginx when I try loading my site to trigger breakpoints.
If I stop the xdebug server, the site works as intended.
So, I'm not able to run the debugger, but it did work previously (!). Not able to pinpoint why it suddenly stopped working.
Setup
A short explanation of the setup (let me know if I need to expand on this).
My php app is running in a docker container, and it is linked to nginx running in a different container using volumes_fromin the docker compose config.
After starting the app, I can verify using phpinfo(); the xdebug module is loaded.
My xdebug.ini has the following content:
zend_extension=xdebug.so
xdebug.remote_enable=1
xdebug.remote_host=10.0.2.2
xdebug.remote_connect_back=0
xdebug.remote_port=5555
xdebug.idekey=complex
xdebug.remote_handler=dbgp
xdebug.remote_log=/var/log/xdebug.log
xdebug.remote_autostart=1
I got the ip address for remote_host (where the xdebug server is running) by these steps:
docker-machine ssh default
route -n | awk '/UG[ \t]/{print $2}' <-- Returns 10.0.2.2
To verify I could reach the debugging server from within my php container, I did the following steps
docker exec -it randomhash bash
nc -z -v 10.0.2.2 5555
Giving the following output depending on xdebug server running or not:
Running: Connection to 10.0.2.2 5555 port [tcp/*] succeeded!
Not running: nc: connect to 10.0.2.2 port 5555 (tcp) failed: Connection refused
So IntelliJ IDEA is surely set up to receive connections on 5555. I also did the appropriate path mapping between my source file paths and the remote path (when setting up the PHP Remote Debugging server from within IDEA).
Any ideas? Kind of lost on this one as I don't have much experience with any of these technologies :D
This sometimes happens, the reason is the errors in php-fpm and xdebug (exactly)!
When I refactored my colleagues code, оne page on the project returned 502 Bad Gateway
Here's what I found:
php-fpm.log
WARNING: [pool www] child 158 said into stderr: "*** Error in `php-fpm: pool www': free(): invalid size: 0x00007f1351b7d2a0 ***"
........
........
WARNING: [pool www] child 158 exited on signal 6 (SIGABRT - core dumped) after 38.407847 seconds from start
I found a piece of code that caused the error:
ob_start();
$result = eval("?>".$string."<"."?p"."hp return 1;");
$new_string = ob_get_clean();
But that is not all. The error occurred only in a certain state $string which at first glance, did not differ from the others. In my case, everything is simple. I removed the code that caused the error. This did not affect the functionality of the web page. I continued to debug the code further.
I had the same problem with the Vagrant Homestead Parallels box with a Silicon chip. Switching from php 7.3 to 7.4 fixed the issue for me.

Permission denied while using PHP Mail using Postfix

I have installed postfix and dovecot properly and did all configurations as per this guide: http://www.krizna.com/ubuntu/setup-mail-server-ubuntu-14-04/
But when I try sending mail via terminal like:
sudo php -r "mail('xyz#xyz.com', 'test', 'test');"
It gives me the error
sh: 1: /etc/postfix: Permission denied
I tried doing chmod -R 777 to the /etc/postfix directory and I still get the same error.
What should I do to get this working? I just want to send mail from my server to other mails like Gmail (i.e. xyz#xyz.com to abc#gmail.com)
I have even configured the mx records and a records in my dns manager.
If I see the Postfix log, I have this:
Feb 2 12:30:33 myhost postfix/pickup[16695]: ED7A120FD3: uid=33 from=<www-data>
Feb 2 12:30:33 myhost postfix/cleanup[4891]: ED7A120FD3: message-id=<20160202173033.ED7A120FD3#xyz.com>
Feb 2 12:30:33 myhost postfix/cleanup[4891]: warning: hash:/etc/postfix/virtual is unavailable. open database /etc/postfix/virtual.db: No such file or directory
Feb 2 12:30:33 myhost postfix/cleanup[4891]: warning: hash:/etc/postfix/virtual lookup error for "xyz#xyz.com"
Feb 2 12:30:33 myhost postfix/cleanup[4891]: warning: ED7A120FD3: virtual_alias_maps map lookup problem for xyz#xyz.com -- message not accepted, try again later
I am using PHP 7 running Apache 2 on Ubuntu 14.04
(Note: Postfix and dovecot seems to be working cause I did telnet as given in the tutorial and got the correct response.)
You forgot to run sudo postmap /etc/postfix/virtual.
Got my problem solved. The permission error occured only when accessing Postfix from terminal. It works when accessed from PHP script.
Moreover, I had to modify PHP.ini sendmail path which I set as /etc/postfix before.

mysqli_connect() error with PHP's built-in web server

I compiled PHP with mysqlnd using the prescriptions from Mysqlnd installation. PHP works fine. php -m | grep mysql shows that neccessary MySQL modules are actually installed. Then I run php -S localhost:8088 -t <dir>. In PHP code function_exists('mysqli_connect') does return true. But when I run mysqli_connect() warning mysqli_connect(): (HY000/2002): No such file or directory arises. In /etc/php.ini I set default socket to /tmp/mysql.sock then to /var/lib/mysql/mysql.sock. Both variants didn't work. How can I enable mysql support in PHP-built-in server?
Have you tried those solutions ?
Warning: mysql_connect(): [2002] No such file or directory (trying to connect via unix:///tmp/mysql.sock) in
First try to use 127.0.0.1 instead of "localhost" in your mysqli_connect call.
Then make sure there is sockets where it should : make a symbolic link from /tmp/mysql.sock to /var/mysql/mysql.sock (or other way depending what exists on your computer)
Finally try to read all answers listed in the link above, maybe one of them will hel you

could not connect localhost with cassandra?

my cassandra is working well in CLI. and thrift also installed well. i already started apache server and cassandara..but when trying to execute php file that is not executed on browser.it shows the error as:
TException: Error: TSocket: Could not connect to localhost:9160 (Permission denied [13])
and the 12th and 13th line is:
$socket = new TSocket('localhost', 9160);
$transport = new TBufferedTransport($socket, 1024, 1024);
i gave 127.0.0.1 instead of localhost.even it is not working.
can any one please help me?
hi every one i solve the problem....
selinux is cause for that problem.....
TException: Error: TSocket: Could not connect to localhost:9160 (Permission denied [13])
To resolve it, you need to change an SELinux boolean value (which will automatically persist across reboots). You may also want to restart httpd to reset the proxy worker, although this isn't strictly required.
setsebool -P httpd_can_network_connect 1
http://wiki.apache.org/httpd/13PermissionDenied

Categories