{"id":3134,"date":"2013-02-21T13:45:25","date_gmt":"2013-02-21T19:45:25","guid":{"rendered":"http:\/\/icesquare.com\/wordpress\/?p=3134"},"modified":"2015-07-01T11:51:13","modified_gmt":"2015-07-01T16:51:13","slug":"php-network-error","status":"publish","type":"post","link":"https:\/\/icesquare.com\/wordpress\/php-network-error\/","title":{"rendered":"PHP Network Error"},"content":{"rendered":"<p>\nOne of my PHP web applications stopped working today. After I investigated the issues, I noticed that this is a very famous, yet unsolved error. I have no idea why it happens, but I do have a work around solution for it. Basically, this article applies to you if you match all of the following:\n<\/p>\n<ul style='margin-left:30px;'>\n<li>You are using PHP<\/li>\n<li>Your web applications talk to other servers via domain name (e.g., example.com rather than 123.1.1.3)<\/li>\n<li>You use XAMPP (instead of native Apache, PHP). <\/li>\n<\/ul>\n<p>Notice that I am not 100% sure whether this has anything to do with XAMPP. But most of problem I experienced happen on XAMPP platform.<\/p>\n<p>And here are some example problems:<\/p>\n<ul style='margin-left:30px;'>\n<li>Getting a file connect using file_get_contents(&#8216;http:\/\/example.com\/somepage.html&#8217;), or CURL etc.<\/li>\n<li>Sending emails (SMTP server: &#8216;mail.example.com&#8217;)<\/li>\n<li>Connect to a database server via domain name (&#8216;example.com:3306&#8217;)<\/li>\n<\/ul>\n<h3>Why this problem happens?<\/h3>\n<p>\nThis problem has nothing to do with your PHP code. In fact, the problem happens when PHP tries to look up the IP address of your domain name. Let&#8217;s take a look to the following example. Suppose I have the following code:<\/p>\n<pre>\r\n$data = file_get_contents('http:\/\/example.com\/test.html')\r\n\r\n\/\/process the $data here...\r\n<\/pre>\n<p>When PHP executes this code, it will try to get the IP address of example.com first, and talk to the server to retrieve the content. This problem happens because PHP is unable to get the IP address of example.com.<\/p>\n<p>Initially, I thought it was my server issues, therefore I tried to ping example.com on the server, i.e.,<\/p>\n<pre>\r\n#ping example.com\r\n<\/pre>\n<p>and the result looks fine to me. So the problem has nothing to do with the OS \/ server. Then I run the following code in PHP<\/p>\n<pre>\r\n$IP = gethostbyname('example.com');\r\necho $IP;\r\n<\/pre>\n<p>Normally, I expect to see the IP address of example.com. If it return &#8216;example.com&#8217;, that means PHP is unable to determine the IP address. That explains why the web application stops working.<\/p>\n<h3>Solution #1: Restart XAMPP<\/h3>\n<p>Try to restart the XAMPP to see whether it resolves the problem or not:<\/p>\n<pre>\r\nsudo \/opt\/lampp\/lampp stopapache\r\nsudo \/opt\/lampp\/lampp startapache\r\n<\/pre>\n<p>This method aims to resolve the situation that the XAMPP was already started but the network was not available. Restarting the Apache server helps to resolve this problem.<\/p>\n<h3>Solution #2: \/etc\/hosts<\/h3>\n<p>Since PHP was unable to lookup the IP address, I decided to give some hints to PHP by editing \/etc\/hosts:<\/p>\n<pre>\r\n123.1.1.1   example.com\r\n123.1.1.2   anotherexample.com\r\n<\/pre>\n<p>This is a quick and easy solution. However, if example.com is moved a different IP address, you will need to update the file. It is pain in long term.<\/p>\n<h3>Solution #3: Stop using XAMPP<\/h3>\n<p>As I mentioned earlier, I notice that this problem happen in XAMPP environment only. I haven&#8217;t experienced this kind of problem with native Apache and native PHP. So I guess it may have something to do with XAMPP.<\/p>\n<p>In fact, it is quite easy to switch from XAMPP to native Apache+PHP+MySQL etc. Native applications give you better performance and reliability and most importantly: the packages get upgraded automatically.<\/p>\n<p>Hope it helps.<\/p>\n<p>&#8211;Derrick<\/p>\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>One of my PHP web applications stopped working today. After I investigated the issues, I noticed that this is a very famous, yet unsolved error. I have no idea why <a class=\"more-link\" href=\"https:\/\/icesquare.com\/wordpress\/php-network-error\/\">Continue Reading &rarr;<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_hide_featured_image":false,"_jetpack_featured_image_duplicate_dismissed":0,"_jetpack_feature_clip_id":0,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2},"jetpack_post_was_ever_published":false},"categories":[94,141,144,6,69],"tags":[172,58],"class_list":["post-3134","post","type-post","status-publish","format-standard","hentry","category-computer-problems-computer-help-pc-support","category-programming-problems","category-sever-problem","category-technology","category-troubleshooting-technology","tag-php","tag-xampp"],"jetpack_publicize_connections":[],"jetpack_shortlink":"https:\/\/wp.me\/p1sqzr-Oy","jetpack_sharing_enabled":true,"jetpack-related-posts":[{"id":3495,"url":"https:\/\/icesquare.com\/wordpress\/xampp-php-fatal-error-class-ziparchive-not-found\/","url_meta":{"origin":3134,"position":0},"title":"XAMPP &#8211; PHP Fatal error:  Class &#8216;ZipArchive&#8217; not found","author":"Derrick","date":"March 11, 2014","format":false,"excerpt":"I just finished writing a program written in PHP today. When I moved to the another environment, I found that the script didn't work. After some investigations, I found that the program stopped running because of the following error: PHP Fatal error: Class 'ZipArchive' not found in ... The error\u2026","rel":"","context":"In &quot;Computer Problems&quot;","block_context":{"text":"Computer Problems","link":"https:\/\/icesquare.com\/wordpress\/category\/technology\/computer-problems-computer-help-pc-support\/"},"img":{"alt_text":"","src":"https:\/\/i0.wp.com\/icesquare.com\/wordpress\/wp-content\/uploads\/2014\/03\/xampp-logo.jpg?resize=350%2C200&ssl=1","width":350,"height":200},"classes":[]},{"id":2788,"url":"https:\/\/icesquare.com\/wordpress\/phpfatal-error-allowed-memory-size-of-x-bytes-exhausted-tried-to-allocate-y-bytes\/","url_meta":{"origin":3134,"position":1},"title":"[PHP]Fatal error: Allowed memory size of x bytes exhausted (tried to allocate y bytes)","author":"Derrick","date":"May 4, 2012","format":false,"excerpt":"[ad#adbrite] A user complained to me that my PHP web applications stopped working today. After I digged into the error log, I found that following message: PHP Fatal error: Allowed memory size of 262144 bytes exhausted (tried to allocate 261900 bytes) in Unknown on line 0 Obviously, this is related\u2026","rel":"","context":"In &quot;Computer Problems&quot;","block_context":{"text":"Computer Problems","link":"https:\/\/icesquare.com\/wordpress\/category\/technology\/computer-problems-computer-help-pc-support\/"},"img":{"alt_text":"","src":"\/wordpress\/wp-content\/uploads\/2011\/12\/computer-problem.jpg","width":350,"height":200},"classes":[]},{"id":305,"url":"https:\/\/icesquare.com\/wordpress\/installing-xampp-on-fedora-12-64-bit\/","url_meta":{"origin":3134,"position":2},"title":"Installing XAMPP on Fedora 12 64-bit system","author":"Derrick","date":"December 8, 2009","format":false,"excerpt":"I was installing XAMPP on Fedora 11 64-bit today, and I saw this famous message: sudo \/opt\/lampp\/lampp startapache XAMPP is currently only availably as 32 bit application. Please use a 32 bit compatibility library for your system. The message is very clear that XAMPP doesn't like 32-bit system. So, we\u2026","rel":"","context":"In &quot;Computer Problems&quot;","block_context":{"text":"Computer Problems","link":"https:\/\/icesquare.com\/wordpress\/category\/technology\/computer-problems-computer-help-pc-support\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":2156,"url":"https:\/\/icesquare.com\/wordpress\/cant-connect-to-local-mysql-server-through-socket-varlibmysqlmysql-sock-2\/","url_meta":{"origin":3134,"position":3},"title":"Can&#8217;t connect to local MySQL server through socket &#8216;\/var\/lib\/mysql\/mysql.sock&#8217; (2)","author":"Derrick","date":"December 21, 2011","format":false,"excerpt":"When I tried to connect to the MySQL server through PHP, I got the following error: Can't connect to local MySQL server through socket '\/var\/lib\/mysql\/mysql.sock' (2) To give you some background, here is my situation. My Fedora Linux is a 64-bit machine, and I have installed a 32-bit version of\u2026","rel":"","context":"In &quot;Computer Problems&quot;","block_context":{"text":"Computer Problems","link":"https:\/\/icesquare.com\/wordpress\/category\/technology\/computer-problems-computer-help-pc-support\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":832,"url":"https:\/\/icesquare.com\/wordpress\/think-twice-before-upgrading-your-server-to-php-5-3-3\/","url_meta":{"origin":3134,"position":4},"title":"Think twice before upgrading your server to PHP 5.3.3","author":"Derrick","date":"September 6, 2010","format":false,"excerpt":"I upgraded to PHP 5.3.3 today, and I was shocked! I found a huge security hole default setting change that may leak the source code to public, which never happened in the earlier version. Here is an example. Suppose my code looks like this: <?php echo \"This is a good\u2026","rel":"","context":"In &quot;Computer Problems&quot;","block_context":{"text":"Computer Problems","link":"https:\/\/icesquare.com\/wordpress\/category\/technology\/computer-problems-computer-help-pc-support\/"},"img":{"alt_text":"Good Example","src":"\/wordpress\/wp-content\/uploads\/2010\/09\/php_5-3-3_goodExample-300x163.png","width":350,"height":200},"classes":[]},{"id":1063,"url":"https:\/\/icesquare.com\/wordpress\/xampp-error-1-couldnt-start-apache-sorry-ive-no-idea-whats-going-wrong\/","url_meta":{"origin":3134,"position":5},"title":"XAMPP: Error 1! Couldn&#8217;t start Apache! &#8230; Sorry, I&#8217;ve no idea what&#8217;s going wrong.","author":"Derrick","date":"November 14, 2010","format":false,"excerpt":"Today I helped my friend to install XAMPP on a Debian system. When I tried to start the Apache webserver, I got the following message: #\/opt\/lampp\/lampp startapache XAMPP: Starting Apache with SSL (and PHP5)... XAMPP: Error 1! Couldn't start Apache! XAMPP: Starting diagnose... XAMPP: Sorry, I've no idea what's going\u2026","rel":"","context":"In &quot;Computer Problems&quot;","block_context":{"text":"Computer Problems","link":"https:\/\/icesquare.com\/wordpress\/category\/technology\/computer-problems-computer-help-pc-support\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]}],"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/icesquare.com\/wordpress\/wp-json\/wp\/v2\/posts\/3134","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/icesquare.com\/wordpress\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/icesquare.com\/wordpress\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/icesquare.com\/wordpress\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/icesquare.com\/wordpress\/wp-json\/wp\/v2\/comments?post=3134"}],"version-history":[{"count":36,"href":"https:\/\/icesquare.com\/wordpress\/wp-json\/wp\/v2\/posts\/3134\/revisions"}],"predecessor-version":[{"id":3953,"href":"https:\/\/icesquare.com\/wordpress\/wp-json\/wp\/v2\/posts\/3134\/revisions\/3953"}],"wp:attachment":[{"href":"https:\/\/icesquare.com\/wordpress\/wp-json\/wp\/v2\/media?parent=3134"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/icesquare.com\/wordpress\/wp-json\/wp\/v2\/categories?post=3134"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/icesquare.com\/wordpress\/wp-json\/wp\/v2\/tags?post=3134"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}