php中使用curl,求助,多谢

听海潮2012 2016-11-11 01:30:34
在linux下使用下面命令可以执行:
curl -v -S -u devuser:devuser123 -F'notification={"applicationId":"5","schemaId":"12","topicId":"1","type":"USER"};type=application/json' -F file=@notification.json "http://135.252.37.213:8080/kaaAdmin/rest/api/sendNotification"


我在php中代码如下:
<?php
$ch = curl_init();

$user="devuser:devuser123";
curl_setopt($ch, CURLOPT_USERPWD,$user); //user:passwd



curl_setopt($ch,CURLOPT_URL,"http://135.252.37.213:8080/kaaAdmin/rest/api/sendNotification"); //url set


curl_setopt($ch, CURLOPT_VERBOSE, 1); //-v


curl_setopt($ch, CURLOPT_POST, 1); //-F 以表单方式

//post data
$post_data = array(
'notification' => array(
'applicationId' => '5',
'schemaId' => '12',
'topicId' => '1',
'type' => 'USER'
),
'type'=>'application/json',
'file'=>'@D:\WAMP\www\kaa\notification.json'
);


curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($post_data));


$response = curl_exec($ch);

if($response === FALSE)
{
echo "cURL 具体出错信息: " . curl_error($ch);
}


curl_close($ch);
?>

返回结果如下:

HTTP ERROR 415

Problem accessing /kaaAdmin/rest/api/sendNotification. Reason:

Unsupported Media Type

请高手帮忙看看,多谢!

...全文
180 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
听海潮2012 2016-11-14
  • 打赏
  • 举报
回复
引用 6 楼 xuzuning 的回复:
400 请求出错 由于语法格式有误,服务器无法理解此请求。 这应该不是你的问题 因为你的代码在我这里测试成功(当然目标url和上传文件名是要换的)
按需要看server端的restful 接口吗?
xuzuning 2016-11-14
  • 打赏
  • 举报
回复
400 请求出错 由于语法格式有误,服务器无法理解此请求。 这应该不是你的问题 因为你的代码在我这里测试成功(当然目标url和上传文件名是要换的)
听海潮2012 2016-11-14
  • 打赏
  • 举报
回复
引用 3 楼 xuzuning 的回复:
$post_data = array(
  'notification ' => '{"applicationId":"5","schemaId":"12","topicId":"1","type":"USER"}',
  'type' => 'application/json',
  'file' => '@D:\WAMP\www\kaa\notification.json'
);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
这样应该可以
版主,我按照你的提示修改代码如下,但是还是有错误 <html> <head> <title> post data</title> </head> <body> <?php $ch = curl_init(); $user="devuser:devuser123"; curl_setopt($ch, CURLOPT_USERPWD,$user); //user:passwd curl_setopt($ch,CURLOPT_URL,"http://135.252.37.213:8080/kaaAdmin/rest/api/sendNotification"); //url set curl_setopt($ch, CURLOPT_VERBOSE, 1); //-v curl_setopt($ch, CURLOPT_POST, 1); //-F //post data $post_data = array( 'notification' => '{"applicationId":"5","schemaId":"12","topicId":"1","type":"USER"}', 'type'=>'application/json', 'file'=>'@D:\WAMP\www\kaa\notification.json' ); curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); $response = curl_exec($ch); echo $response; if($response === FALSE) { echo "cURL 错误信息: " . curl_error($ch); } curl_close($ch); ?> </body> </html> 返回结果: HTTP ERROR 400 Problem accessing /kaaAdmin/rest/api/sendNotification. Reason: Required request part 'notification' is not present Powered by Jetty:// 再帮忙看看吧,多谢
听海潮2012 2016-11-14
  • 打赏
  • 举报
回复
curl -v -S -u devuser:devuser123 -F'notification={"applicationId":"5","schemaId":"12","topicId":"1","type":"USER"};type=application/json' -F file=@notification.json "http://135.252.37.213:8080/kaaAdmin/rest/api/sendNotification" 我仔细理解了一下这条命令,-F是以表单格式提交,对于第一个-F,type表示 提交的是json数据,对于第二个-F,是按照默认格式提交的,那么在PHP中,不同格式的数据能同时提交吗? 还是说要看server侧的具体实现? thanks
xuzuning 2016-11-11
  • 打赏
  • 举报
回复
不是应该,而是就是可以
xuzuning 2016-11-11
  • 打赏
  • 举报
回复
$post_data = array(
  'notification ' => '{"applicationId":"5","schemaId":"12","topicId":"1","type":"USER"}',
  'type' => 'application/json',
  'file' => '@D:\WAMP\www\kaa\notification.json'
);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
这样应该可以
听海潮2012 2016-11-11
  • 打赏
  • 举报
回复
ls同学。我改成下面这样还是不行 $data_str=json_encode($post_data); curl_setopt($ch, CURLOPT_POSTFIELDS, $data_str); 另外能帮忙解释一下那条curl命令吗,尤其是红色部分,多谢 curl -v -S -u devuser:devuser123 -F'notification={"applicationId":"5","schemaId":"12","topicId":"1","type":"USER"};type=application/json' -F file=@notification.json "http://135.252.37.213:8080/kaaAdmin/rest/api/sendNotification"
xuzuning 2016-11-11
  • 打赏
  • 举报
回复
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($post_data)); 应写作 curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data); 因为 http_build_query 破坏了 @D:\WAMP\www\kaa\notification.json 的表述 但是 对于多维数组的 $post_data 还是存在问题 因为 curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data); 只接受一维数组 你可以改变 $post_data 的结构 或 分开来提交,但这都涉及到对方的程序

21,886

社区成员

发帖
与我相关
我的任务
社区描述
从PHP安装配置,PHP入门,PHP基础到PHP应用
社区管理员
  • 基础编程社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧