title: Sending json data from python to php
body: <p>What I want to do here is to send json data from python program and receive in php and store it in database and show it in a browser.</p>
  
  <p>This is the code I am using for sending the json data from python :</p>
  
  <pre><code>import httplib,json,urllib
  headers = { "charset":"utf-8",
  "Accept": "text/plain"}
  conn = httplib.HTTPConnection("localhost")
  #converting list to a json stream
  bulkData={"temp_value":123}
  bulkData = json.dumps(bulkData, ensure_ascii = 'False')
  # ensure_ascii is false as data is in unicode and not ascii encoding , use this if data is in any other encoding
  postData = urllib.urlencode({'results':bulkData})
  conn.request("POST", "/test1.php", postData,headers)
  response = conn.getresponse()
  text = response.read()
  print response.status,text
  conn.close()
  </code></pre>
  
  <p>These are the php codes that i used to receive the json data from python :
  opt 1)</p>
  
  <pre><code>&lt;?php
  if (isset($_POST['results']))
  {
  $data = json_decode($_POST['results']);
  // This is to decode the json stream, using a function called json_decode()
  foreach($data as $record) // ACCESS each record individually
  {
  foreach($record as $key =&gt; $value)
  {
  echo $key . '-&gt;' .$value;
  // you can get individual key , value pairs (if content is in dictionary format)
  }
  }
  }
  else
  {
      echo $data;
  echo 'POST Variable not found';
  }
  ?&gt;
  </code></pre>
  
  <p>opt 2)</p>
  
  <pre><code>&lt;?php
  $url = "http://localhost/pyjson.py";
  $json = file_get_contents($url);
  $json_data = json_decode($json, true);
  echo "temp value: ". $json_data["temp_value"];
  ?&gt;
  </code></pre>
  
  <p>When I run the python using option 1 i get like <code>200 POST Variable not found</code> in the python side and in the php side i get like <code>POST Variable not found</code>.</p>
  
  <p>When I run the same program using opt 2 i will get like this in python</p>
  
  <pre><code>200 &lt;br /&gt;
  &lt;font size='1'&gt;&lt;table class='xdebug-error xe-warning' dir='ltr' border='1' cellspacing='0' cellpadding='1'&gt;
  &lt;tr&gt;&lt;th align='left' bgcolor='#f57900' colspan="5"&gt;&lt;span style='background-color: #cc0000; color: #fce94f; font-size: x-large;'&gt;( ! )&lt;/span&gt; Warning: file_get_contents(http://localhost/pyjson.py): failed to open stream: HTTP request failed! HTTP/1.1 404 Not Found
  
   in C:\wamp\www\test1.php on line &lt;i&gt;3&lt;/i&gt;&lt;/th&gt;&lt;/tr&gt;
  &lt;tr&gt;&lt;th align='left' bgcolor='#e9b96e' colspan='5'&gt;Call Stack&lt;/th&gt;&lt;/tr&gt;
  &lt;tr&gt;&lt;th align='center' bgcolor='#eeeeec'&gt;#&lt;/th&gt;&lt;th align='left' bgcolor='#eeeeec'&gt;Time&lt;/th&gt;&lt;th align='left' bgcolor='#eeeeec'&gt;Memory&lt;/th&gt;&lt;th align='left' bgcolor='#eeeeec'&gt;Function&lt;/th&gt;&lt;th align='left' bgcolor='#eeeeec'&gt;Location&lt;/th&gt;&lt;/tr&gt;
  &lt;tr&gt;&lt;td bgcolor='#eeeeec' align='center'&gt;1&lt;/td&gt;&lt;td bgcolor='#eeeeec' align='center'&gt;0.0000&lt;/td&gt;&lt;td bgcolor='#eeeeec' align='right'&gt;240472&lt;/td&gt;&lt;td bgcolor='#eeeeec'&gt;{main}(  )&lt;/td&gt;&lt;td title='C:\wamp\www\test1.php' bgcolor='#eeeeec'&gt;..\test1.php&lt;b&gt;:&lt;/b&gt;0&lt;/td&gt;&lt;/tr&gt;
  &lt;tr&gt;&lt;td bgcolor='#eeeeec' align='center'&gt;2&lt;/td&gt;&lt;td bgcolor='#eeeeec' align='center'&gt;0.0000&lt;/td&gt;&lt;td bgcolor='#eeeeec' align='right'&gt;240704&lt;/td&gt;&lt;td bgcolor='#eeeeec'&gt;&lt;a href='http://www.php.net/function.file-get-contents' target='_new'&gt;file_get_contents&lt;/a&gt;
  (  )&lt;/td&gt;&lt;td title='C:\wamp\www\test1.php' bgcolor='#eeeeec'&gt;..\test1.php&lt;b&gt;:&lt;/b&gt;3&lt;/td&gt;&lt;/tr&gt;
  &lt;/table&gt;&lt;/font&gt;
  </code></pre>
  
  <p>And in the php side I got </p>
  
  <pre><code>Warning: file_get_contents(http://localhost/pyjson.py): failed to open stream: HTTP request failed! HTTP/1.1 404 Not Found in C:\wamp\www\test1.php on line 3
  </code></pre>
  
  <p>What I want to do here is to send json data from python program and receive in php and store it in database and show it in a browser. Someone please suggest me a proper way for this program and to do this.</p>