php - 卷曲请求到 PHP 卷曲请求
问题描述
我需要帮助将此请求重写为 php curl:
curl -d '{"text":"Hello,
#param#.","port":[2,3],"param":[{"number":"123456","text_param
":["John"],"user_id":1},{"number":"123478",
"text_param":["Sam"],"user_id":2}]}’ –H "Content-Type:
application/json" http://gateway_ip/api/send_sms
到目前为止,我没有任何成功。这是我尝试过的:
$url = "http://url/api/send_sms";
$params = array(
'auth' => 'user:pass',
'port' => 7,
'text' => utf8_encode('Hello, world!')
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_USERPWD, "user:pass");
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($params));
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 60);
curl_setopt($ch, CURLOPT_TIMEOUT, 60);
我做错了什么?
解决方案
像这样的东西:
<?php
$ch = curl_init();
$json = <<<JSON
{"text":"Hello,
#param#.","port":[2,3],"param":[{"number":"123456","text_param
":["John"],"user_id":1},{"number":"123478",
"text_param":["Sam"],"user_id":2}]}
JSON;
curl_setopt($ch, CURLOPT_URL, "http://gateway_ip/api/send_sms");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $json);
curl_setopt($ch, CURLOPT_POST, 1);
$headers = array();
$headers[] = "Content-Type: application/json";
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$result = curl_exec($ch);
if (curl_errno($ch)) {
echo 'Error:' . curl_error($ch);
}
curl_close ($ch);
自己构建 $json var,但使用json_encode函数创建一个有效的 json
推荐阅读
- python - 需要将画布移动到应用的右侧
- javascript - 在使用js动态加载脚本时,为什么我们有onload事件时要使用回调?
- laravel - Yajra 数据表将可变数量的自定义列附加到行
- ios - 如何在没有空间问题的情况下创建各种大小的 collectionView 单元格
- javascript - 在reactjs中上传之前的多重图像预览?
- java - 如何在 Selenium Java 中转换 Katalon 脚本?
- angular - 在子组件中测试发出的值
- python-3.x - 自动从网站下载某些 csv 文件
- karate - com.intuit.karate.exception.KarateException: user.feature:11 - 找不到步骤定义方法匹配:匹配响应
- database - PostgreSQL:PgAdmin 上具有 3 个主键的表的外键