如何向php服务器发送数据为json的php post json请求

php 通过curl post发送json数据实例-Php应用-Php教程-壹聚教程网php 通过curl post发送json数据实例
利用php curl发送json数据与curl post其它数据是一样的,下面我来给大家总结几个关于curl post发送json数据实例,希望能加深各位对curl post json数据的理解吧。
$data = array(&name& =& &Hagrid&, &age& =& &36&);&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
$data_string = json_encode($data);&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
$ch = curl_init('http://api.local/rest/users');&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
($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);
&& function http_post_data($url, $data_string) {
&&&&&&& $ch = curl_init();
&&&&&&& curl_setopt($ch, CURLOPT_POST, 1);
&&&&&&& curl_setopt($ch, CURLOPT_URL, $url);
&&&&&&& curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
&&&&&&& curl_setopt($ch, CURLOPT_HTTPHEADER, array(
&&&&&&&&&&& 'Content-Type: application/ charset=utf-8',
&&&&&&&&&&& 'Content-Length: ' . strlen($data_string))
&&&&&&& );
&&&&&&& ob_start();
&&&&&&& curl_exec($ch);
&&&&&&& $return_content = ob_get_contents();
&&&&&&& ob_end_clean();
&&&&&&& $return_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
&&&&&&& return array($return_code, $return_content);
$url& = &&;
$data = json_encode(array('a'=&1, 'b'=&2));
list($return_code, $return_content) = http_post_data($url, $data);
&&&& &button&:[
&&&&&&&&& &type&:&click&,
&&&&&&&&& &name&:&今日歌曲&,
&&&&&&&&& &key&:&V1001_TODAY_MUSIC&
&&&&&&&&&& &type&:&click&,
&&&&&&&&&& &name&:&歌手简介&,
&&&&&&&&&& &key&:&V1001_TODAY_SINGER&
&&&&&&&&&& &name&:&菜单&,
&&&&&&&&&& &sub_button&:[
&&&&&&&&&&& {
&&&&&&&&&&&&&& &type&:&click&,
&&&&&&&&&&&&&& &name&:&hello &,
&&&&&&&&&&&&&& &key&:&V1001_HELLO_WORLD&
&&&&&&&&&&& },
&&&&&&&&&&& {
&&&&&&&&&&&&&& &type&:&click&,
&&&&&&&&&&&&&& &name&:&赞一下我们&,
&&&&&&&&&&&&&& &key&:&V1001_GOOD&
&&&&&&&&&&& }]
$ch = curl_init($urlcon); //请求的URL地址
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, &POST&);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);//$data JSON类型字符串
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json', 'Content-Length: ' . strlen($data)));
$data = curl_exec($ch);
print_r($data);//创建成功返回:{&errcode&:0,&errmsg&:&ok&}
小结,我们发现最核心的一句代码就是Content-Type: application/这个是文件格式类型了。
上一页: &&&&&下一页:相关内容问题对人有帮助,内容完整,我也想知道答案
问题没有实际价值,缺少关键内容,没有改进余地
具体描述:
本地模拟请求服务器数据,请求数据格式为json,服务器返回数据也是json。
使用ajax模拟都成功了
type: "POST",
crossDomain: true,
url: 'http://*******',
data: {'command':'test'},
success: function(e) {
console.log(e);//这里log的是json
dataType: 'json'
curl就没有成功
$url = 'http://*******';
$param = "{'command':'test'}";
$ch = curl_init($url); //请求的URL地址
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, $param);//$data JSON类型字符串
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json', 'Content-Length: ' . strlen($param)));
$data = curl_exec($ch);//这里的data打印出来是空的
答案对人有帮助,有参考价值
答案没帮助,是错误的答案,答非所问
$param = "{'command':'test'}";
这一行改成PHP数组键值的形式。
因为curl模拟POST的时候,POST的参数应该是以数组的形式传参。
答案对人有帮助,有参考价值
答案没帮助,是错误的答案,答非所问
请问lz最后的代码是怎么写的,我把param换成数组,最后获取数据也是空
答案对人有帮助,有参考价值
答案没帮助,是错误的答案,答非所问
$url = '';
$param = '{"userne":"lipi","password":"e10be56e057f20f883e"}';$ch = curl_init($url); //请求的URL地址curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");curl_setopt($ch, CURLOPT_POSTFIELDS, $param);//$data JSON类型字符串curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json', 'Content-Length: ' . strlen($param)));$data = curl_exec($ch);
同步到新浪微博
分享到微博?
Hi,欢迎来到 SegmentFault 技术社区!⊙▽⊙ 在这里,你可以提出编程相关的疑惑,关注感兴趣的问题,对认可的回答投赞同票;大家会帮你解决编程的问题,和你探讨技术更新,为你的回答投上赞同票。
明天提醒我
关闭理由:
删除理由:
忽略理由:
推广(招聘、广告、SEO 等)方面的内容
与已有问题重复(请编辑该提问指向已有相同问题)
答非所问,不符合答题要求
宜作评论而非答案
带有人身攻击、辱骂、仇恨等违反条款的内容
无法获得确切结果的问题
非开发直接相关的问题
非技术提问的讨论型问题
其他原因(请补充说明)
我要该,理由是:
扫扫下载 App2929人阅读
需要向@&http://222.44.51.34/jienu_serv/serv.php&发送一个请求,数据为@&{\&application\&:\&UpdataVersion\&,\&os\&:\&ios\&,\&display\&:\&480*960\&}&; 代码如下:
-(RemoteReturn *)versionUpdata{
NSURL *url=[NSURL URLWithString:@&http://222.44.51.34/jienu_serv/serv.php&];
ASIHTTPRequest *request=[ASIHTTPRequest requestWithURL:url];
NSMutableData *bodyData=[NSMutableData data];
NSString *bodyDataStr=@&{\&application\&:\&UpdataVersion\&,\&os\&:\&ios\&,\&display\&:\&480*960\&}&;
[bodyData appendData:[bodyDataStr dataUsingEncoding:NSUTF8StringEncoding]];
[request setRequestMethod:@&POST&];
//[request addRequestHeader:@&Content-Type& value:@&application/json&];
[request appendPostData:bodyData];
[request start];
NSString *response=[request responseString];
NSLog(@&===---------%@&,response);
RemoteReturn *remote=[[RemoteReturn alloc] initWithString:response];
但是发送请求总是错误,错误信息如下:
12:53:04.544 jienu[b] [STATUS] Starting synchronous request
12:53:04.548 jienu[b] [CONNECTION] Request will not use a persistent connection
12:53:04.549 jienu[] [CONNECTION] Request will not use a persistent connection
12:53:04.549 jienu[b] [STATUS] Request : Failed
12:53:04.550 jienu[b] [CONNECTION] Request #(null) failed and will invalidate connection #(null)
大师看下,该怎么解决
你将bodyData转换成nsstring类型,而不是post一个数组。
你只要根据它的接口,加数据,设置为正确的头就行啦。
&&相关文章推荐
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
访问:265393次
积分:3476
积分:3476
排名:第9247名
原创:74篇
转载:100篇
评论:32条
(1)(1)(6)(3)(3)(2)(4)(1)(6)(6)(2)(3)(11)(12)(4)(4)(18)(5)(2)(3)(7)(10)(5)(14)(16)(25)1571人阅读
ios知识总结(13)
方法一、使用直接写写入格式
NSURL *url=[NSURLURLWithString:urlString];
& & NSMutableURLRequest *requests = [[NSMutableURLRequestalloc]initWithURL:urlcachePolicy:NSURLRequestUseProtocolCachePolicytimeoutInterval:50];
& & [requests setHTTPMethod:@&POST&];
//这是最终的数据格式,只要能够转换成这种格式其他的数据类型均可,但有时候服务器端不能够解析
& & NSString *postParams = [NSString stringWithFormat:@&{\&token\&:\&IOS_Json_Data\&,\&account\&:\&%@\&,\&password\&:\&%@\&}&,username,password];
& & NSData *data =[postParamsdataUsingEncoding:NSUTF8StringEncoding];
& & [requests
setValue:[NSStringstringWithFormat:@&%lu&,(unsignedlong)data.length]forHTTPHeaderField:@&Content-Length&];
& & [requests
setValue:@&application/json&forHTTPHeaderField:@&Content-Type&];&& &
& & [requests setHTTPBody:data];
方法二、使用AFNetworking框架
&AFHTTPClient *httpClient = [[AFHTTPClientalloc]
initWithBaseURL:[NSURLURLWithString:@&&]];
& & httpClient.parameterEncoding =AFJSONParameterEncoding;
& & [httpClient setDefaultHeader:@&Accept&value:@&text/json&];
& & NSMutableDictionary *params=[[NSMutableDictionaryalloc]
& & [params setObject:username
forKey:@&account&];
& & [params setObject:password
forKey:@&password&];
& & [params setObject:@&token&forKey:@&token&];
& & [httpClient postPath:urlString
parameters:params success:^(AFHTTPRequestOperation *operation,id responseObject){
& & & & NSLog(@&params ==%@&,params);
& & & & NSString *responseStr = [[NSStringalloc]
initWithData:responseObjectencoding:NSUTF8StringEncoding];
& & & & NSLog(@&Request Successful, response '%@'&, responseStr);
& & }failure:^(AFHTTPRequestOperation *operation,NSError *error)
&& & & & NSLog(@&[HTTPClient Error]: %@&,error); }];
方法三、使用系统自带的方法
& &&NSMutableURLRequest *requests = [[NSMutableURLRequest alloc]initWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:50];
& & [requests setHTTPMethod:@&POST&];
& & NSDictionary *jsonDic=[NSDictionarydictionaryWithObjectsAndKeys:@&ios_json_data&,@&token&,@&testid&,@&account&,@&testid&,@&password&,nil];
& & NSData *
& & if ([NSJSONSerializationisValidJSONObject:jsonDic])
& & & & NSError *
& & & & data = [NSJSONSerializationdataWithJSONObject:jsonDic
options:NSJSONWritingPrettyPrintederror:&error];
& & [requests
setValue:[NSStringstringWithFormat:@&%lu&,(unsignedlong)data.length]forHTTPHeaderField:@&Content-Length&];
& & [requests
setValue:@&application/json&forHTTPHeaderField:@&Content-Type&];
& & [requests setHTTPBody:data];
&&相关文章推荐
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
访问:37385次
排名:千里之外
原创:25篇
(1)(1)(1)(10)(5)(4)(3)(2)(4)

我要回帖

更多关于 php post发送json数据 的文章

 

随机推荐