Trouble converting POST curl from command line to php(无法将 POST curl 从命令行转换为 php)
问题描述
我在将 curl 命令转换为 php 时遇到问题.
I am having trouble converting my curl command into php.
这部分效果很好.
将条目添加到 Parse.com 数据库的 CURL 命令:
CURL command that adds an entry into my Parse.com database:
curl -X POST
-H "X-Parse-Application-Id: my_id"
-H "X-Parse-REST-API-Key: api_id"
-H "Content-Type: application/json"
-d "{"SiteID":"foundID","dataUsedString":"foundUsage","usageDate":"foundDate", "monthString":"foundMonth", "dayString":"foundDay","yearString":"foundYear"}"
https://api.parse.com/1/classes/MyClass
已解决的答案:
我创建了这个 php 脚本来复制命令:
I have created this php script to replicate the command:
<?php
$ch = curl_init('https://api.parse.com/1/classes/MyClass');
curl_setopt($ch,CURLOPT_HTTPHEADER,
array('X-Parse-Application-Id:my_id',
'X-Parse-REST-API-Key:api_id',
'Content-Type: application/json'));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, "{"SiteID":"foundID","dataUsedString":"foundUsage","usageDate":"foundDate", "monthString":"foundMonth", "dayString":"foundDay","yearString":"foundYear"}");
curl_exec($ch);
curl_close($ch);
?>
推荐答案
您错过了一些重要的配置.这些是设置 CURL 以使用 POST 发送请求,第二个是要发送的数据.(原始数据作为字符串发送到 POSTFIELDS,如果你发送数组 - 它会自动附加标题multipart/form-data"
You've missed some crucial configurations. These are the set the CURL to send request using POST, and the second is data to send. (RAW DATA is being sent as string into POSTFIELDS, those if you send array - it will automatically append header "multipart/form-data"
$ch = curl_init('https://api.parse.com/1/classes/MyClass');
curl_setopt($ch,CURLOPT_HTTPHEADER,
array(
'X-Parse-Application-Id:my_id',
'X-Parse-REST-API-Key:api_id',
'Content-Type: application/json'
)
);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, "{"SiteID":"foundID","dataUsedString":"foundUsage","usageDate":"foundDate", "monthString":"foundMonth", "dayString":"foundDay","yearString":"foundYear"}");
curl_exec($ch);
curl_close($ch);
HTH:)
这篇关于无法将 POST curl 从命令行转换为 php的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:无法将 POST curl 从命令行转换为 php
基础教程推荐
- 在 CakePHP 2.0 中使用 Html Helper 时未定义的变量 2021-01-01
- 在 yii2 中迁移时出现异常“找不到驱动程序" 2022-01-01
- 使用 PDO 转义列名 2021-01-01
- phpmyadmin 错误“#1062 - 密钥 1 的重复条目‘1’" 2022-01-01
- 找不到类“AppHttpControllersDB",我也无法使用新模型 2022-01-01
- 如何在 Symfony 和 Doctrine 中实现多对多和一对多? 2022-01-01
- HTTP 与 FTP 上传 2021-01-01
- 如何在 XAMPP 上启用 mysqli? 2021-01-01
- Doctrine 2 - 在多对多关系中记录更改 2022-01-01
- PHP 守护进程/worker 环境 2022-01-01