php - PHP curl 带有引号和空格的长链接
问题描述
需要使用 cURL 从 php 访问 REST api。这个 ia 休息链接:
http://username:password@172.16.59.225:8042/tools/find -d '{"Level":"Study","Expand":false,"Query":{"StudyDate":"20191226-20200324"}}'
正如你看到的很多 " 和空白。
我试图像这样手动准备字符串:
$url = "http://172.16.59.225/tools/find -d '{\"Level\":\"Study\",\"Expand\":false,\"Query\":{\"StudyDate\":\"20191226-20200324\"}}'";
and pass password using: curl_setopt($ch, CURLOPT_USERPWD, "username:password");
我用过urlencode()
,curl_escape
没有任何效果。同时,此链接在 Linux cmd 中与 curl 一起使用可以完美运行。
可行吗?
解决方案
试试这个,--data
或者-d
参数是sends the specified data in a POST request to the HTTP server
Source
$url = 'http://172.16.59.225/tools/find';
$postdata = '{"Level":"Study","Expand":false,"Query":{"StudyDate":"20191226-20200324"}}';
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postdata);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_USERPWD, "username:password");
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
$result = curl_exec($ch);
curl_close($ch);
print_r ($result);
推荐阅读
- c# - 错误 MSB4018 “GenerateClsidMap”任务意外失败
- google-cloud-platform - 有什么方法可以将参数或参数文件传递给数据融合中的管道
- python - 当我在 python 中复制列表时出现问题。两个列表不是独立的
- git - 将樱桃选择提交到其他分支的 Git 问题
- laravel - HarmonyLinkingError:在“vue”中找不到导出“onUnmounted”
- powershell - 如何通过脚本在 Hyper-V 中创建的虚拟机中运行一些脚本(用户名和密码),无需提示
- reactjs - 我在我的反应应用程序中收到此错误“指示是否通过指定其 SameSite 属性在跨站点请求中发送 cookie”
- android - 如何在 Android Chromium 浏览器应用程序中设置代理
- c - fputs() 产生错误的结果
- react-native - 具有多个类别的多选过滤器