2015年8月10日 星期一

apache php 接收 json

關鍵在apache的目錄必須開啟apache user讀寫權限。另外我遇到的狀況是傳送整個json檔案,因此必須使用json_decode函式將json file轉換成array才能進一步使用。
<?php
//header("Content-Type: application/json", true);

$filename = fopen('test.json', 'w');  //打開json暫存檔,'w'參數為每次打開皆清空檔案

if ( $_SERVER['REQUEST_METHOD'] === 'POST' ){ 
    $postText = file_get_contents('php://input'); 


fwrite($filename, $postText);  //寫入暫存檔
fclose($filename);  //關閉暫存檔

$url = "http://target.php";  //改成需要轉發的url

$data_decode = json_decode($postText);  //json格式解碼
$data_string = json_encode($data_decode);  //json格式編碼,這邊的目的是再將收到的json轉發出去                                                                                 
                                                                                                                     
$ch = curl_init($url);                                                                      
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");                                                                     
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);                                                                  
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);                                                                      
curl_setopt($ch, CURLOPT_HTTPHEADER, array(                                                                          
    'Content-Type: application/json',                                                                                
    'Content-Length: ' . strlen($data_string))                                                                       
);                                                                                                                   
                                                                                                                     
$result = curl_exec($ch);


}

?>

上面的範例是當對方發送json過來時,將收到的json file再轉發到另一個url的webserver。
另外jquery必須將.js嵌入網頁,可由網路或本地端,之後才可以使用。

清除舊的ubuntu repository資料
sudo sed -i -e 's/archive.ubuntu.com\|security.ubuntu.com/old-releases.ubuntu.com/g' /etc/apt/sources.list
sudo apt-get update

參考資料

php curl post

http://stackoverflow.com/questions/15200632/how-to-upload-file-using-curl-with-php
http://askubuntu.com/questions/9293/how-do-i-install-curl-in-php5
https://www.digitalocean.com/community/questions/installing-curl-on-ubuntu-12-04
http://blog.derakkilgo.com/2009/06/07/send-a-file-via-post-with-curl-and-php/
http://reader.roodo.com/esabear/archives/16358749.html
http://blog.roodo.com/esabear/archives/16358749.html
http://stackoverflow.com/questions/19758954/get-data-from-json-file-with-php
http://markonphp.com/php-curl-basic-post-example/
http://www.lornajane.net/posts/2011/posting-json-data-with-php-curl

php receive post

http://stackoverflow.com/questions/938811/receive-xml-file-via-post-in-php
http://stackoverflow.com/questions/19004783/reading-json-post-using-php
http://stackoverflow.com/questions/15220704/how-to-detect-if-post-is-set
http://www.blog.magepsycho.com/sending-json-data-remote-server/
http://www.php.net/manual/en/function.json-decode.php
http://codex.wiki/post/166087-764/
http://josh.gourneau.com/blog/2010/11/16/serve-json-as-content-type-applicationjson-with-apache-on-ubuntu/
https://blog-ldkrsi.rhcloud.com/php%E6%8E%A5%E6%94%B6post%E4%BE%86%E7%9A%84json%E8%B3%87%E6%96%99/
http://www.j2h.tw/bbs/bbs16/519.html
http://phpwolf.blogspot.tw/2013/08/php-post-json.html
http://stackoverflow.com/questions/18866571/receive-json-post-with-php

POST測試

https://www.hurl.it/

jquery傳送資料給php,php再回傳

https://docs.google.com/document/d/1U0n01CVFllcpd5Irzk8999x8ALHck5M4vOJizA_mYvs/pub
http://italwaysrainonme.blogspot.tw/2013/02/jqueryjsonphp-phpjsonjquery.html
http://kinomelma.pixnet.net/blog/post/30403239-%5B%E8%B6%85%E7%B0%A1%E6%98%93%5D-jquery-json-%E6%A0%BC%E5%BC%8F%E6%8E%A5%E6%94%B6%E8%88%87%E5%88%86%E6%9E%90

android溝通

http://j796160836.pixnet.net/blog/post/28994669-%5Bandroid%5D-%E4%BD%BF%E7%94%A8http%E7%9A%84post%E6%96%B9%E5%BC%8F%E5%92%8C%E7%B6%B2%E9%A0%81%E8%A1%A8%E5%96%AE%E6%BA%9D%E9%80%9A

install jquery

http://stackoverflow.com/questions/1458349/installing-jquery
http://www.mkyong.com/jquery/jquery-html-example/
https://developers.google.com/speed/libraries/#jquery
http://www.mkyong.com/jquery/jquery-hello-world/
http://expect7.pixnet.net/blog/post/37919326-%5B%E7%A8%8B%E5%BC%8F%5D%5Bjquery%5D-jquery%E4%B8%AD%E7%9A%84ajax%E7%9A%84%E5%9F%BA%E7%A4%8E%E9%81%8B%E7%94%A8%E3%80%82%E6%8F%90%E4%BE%9B%E7%AF%84
http://blog.jex.tw/blog/2013/02/19/php-json/
http://www.mkyong.com/jquery/jquery-html-example/

php server上傳

http://givemepass.blogspot.tw/2013/06/php-server.html

php fwrite example

http://php.net/manual/en/function.fwrite.php