您好,欢迎访问一九零五行业门户网

php怎么写服务端获取 客户端发来的json

我客户端是用 libcurl 库的  用http 给服务端发出请求的 不是用socket的  php应该怎么写才能收到请求
回复讨论(解决方案) 你先
file_put_contents('test.txt', 'get:' . print_r($_get, 1) . 'post:' . print_r($_post, 1));
看看 test.txt 中哪段有内容
如果使用的是http协议的get方法,数据保存在$_get数组中.
如果使用的是http协议的post方法,数据通常保存在$_post数组中.
注:如果post方法没有指出数据类型,提交的数据可以通过$http_raw_post_data获取.
找到数据后可以通过json_decode函数处理下即可转换成数组或对象.
直接输出
print_r($_post);echo 111;
pint_r($_get);
你先
file_put_contents('test.txt', 'get:' . print_r($_get, 1) . 'post:' . print_r($_post, 1));
看看 test.txt 中哪段有内容
print_r($_post, 1) 我直接打这个什么都没看到。。。 奇怪 我客户端就是发的是字符串 
你直接 print_r($_post) 给谁看?给客户端?
那你客户端有显示回传数据的代码吗?
所以我让你 print_r($_post, 1) 到文件,这样才好检查呀
get:array
(
)
post:array
(
)
我收到的是这个哈 还是什么都没有..蛋疼
你直接 print_r($_post) 给谁看?给客户端?
那你客户端有显示回传数据的代码吗?
所以我让你 print_r($_post, 1) 到文件,这样才好检查呀
get:array
(
)
post:array
(
)
我收到的是这个哈 还是什么都没有..蛋疼

file_put_contents('test.txt', file_get_contents('php://input'));
看看
进来学习的,抱歉,没帮到忙

file_put_contents('test.txt', file_get_contents('php://input'));
看看
什么都没有了..
你能确定客户端发了数据吗?
确定  是不是解码问题的 我客户端是用c++ libcurl post发的    
你能确定客户端发了数据吗? 确定  是不是解码问题的 我客户端是用c++ libcurl post发的    
你都没有收到数据,何来的解码?
有可能的话,请贴出 c++ 的代码片段
有可能的话,请贴出 c++ 的代码片段
#include hhttp.h
#include json.h
//导入curl头文件
#include ../libs/cocos2dx/platform/third_party/ios/curl/curl.h
bool hhttp::pserver(){
    //------------------------
    curl *curl; // 初始化 curl 结构
curlcode res;
res = curl_global_init(curl_global_all);//初始化所有的可能的调用。
    if (res != curle_ok)
    {
        printf( failed to global init default [%d]\n, res );
        return false;
    }
// curl = curl_easy_init();
//    json::fastwriter writer;
//    json::value root;
//    root[action] = device;
//      std::string json_file=writer.write(root);
if (curl)
{
std::string cc; 
        curl_easy_setopt( curl, curlopt_url, http://127.0.0.1/ceshi/ceshi.php); //请求的地址
        curl_easy_setopt(curl, curlopt_post, true);                     //启用post提交
        curl_easy_setopt(curl, curlopt_postfields, account=zhycheng); //发送的数据
        curl_easy_setopt( curl, curlopt_followlocation, 1l);
        curl_easy_setopt(curl,curlopt_writefunction,write_data); //处理的函数
        curl_easy_setopt( curl, curlopt_writedata, &cc);        //缓冲的内存
        res = curl_easy_perform(curl);// post
        if(res != curle_ok){
            if (curle_operation_timedout == res) //连接失败 超时
            {
                ccmessagebox(请重新尝试请求!, 网络连接超时);
                cclog(网络超时:错误信息:%s\n,curl_easy_strerror(res));
                return 0;
            }
            ccmessagebox(请重新尝试请求!, 网络连接超时);
            cclog(raw_send_recv : 启动 curl 失败,错误信息:%s\n,curl_easy_strerror(res));
            return 0;
        }
        cclog(~send data done~~);
//清理 curl 结构,释放资源等
curl_easy_cleanup(curl);
} else  {
cclog(raw_send_recv : 初始化 curl 失败 \n);
        return 0; 
}
    return true;
}
int alllength;//总流长度
std::vector alldata;//总数据
//服务器有数据返回的时候就会调用这个函数
size_t hhttp::write_data(uint8_t *dataptr, size_t size, size_t nmemb, void *stream) {
int  len  = size * nmemb;
if(len!=1440){
        cclog(收到服务器数据,返回的数据长度 : %i,alllength);
}else{
        alllength+=len;
        alldata.push_back(dataptr);
    }
    size_t sizes = size * nmemb;
    return sizes;
}
你这样写对吗?
一般这么写
    // 初始化libcurl  
    curlcode return_code;  
    return_code = curl_global_init(curl_global_win32);  
    if (curle_ok != return_code)  
        return 0;  
    curl *curl_handle;  
    curlcode res;  
    curl_handle = curl_easy_init();  
    curl_easy_setopt(curl_handle, curlopt_url, url);  
   ....
你虽有
curl *curl;
但没有
curl = curl_easy_init();
那 if (curl) 分支能进去吗?
你这样写对吗?
一般这么写
    // 初始化libcurl  
    curlcode return_code;  
    return_code = curl_global_init(curl_global_win32);  
    if (curle_ok != return_code)  
        return 0;  
    curl *curl_handle;  
    curlcode res;  
    curl_handle = curl_easy_init();  
    curl_easy_setopt(curl_handle, curlopt_url, url);  
   ....
你虽有
curl *curl;
但没有
curl = curl_easy_init();
那 if (curl) 分支能进去吗?
代码确实运行了哈 连接成功了哈..... 我应该怎么办
其它类似信息

推荐信息