无法将POST curl从命令行转换为php [英] Trouble converting POST curl from command line to php

查看:187
本文介绍了无法将POST curl从命令行转换为php的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法将curl命令转换为php。



这部分效果很好。



CURL命令在我的Parse.com数据库中添加一个条目:

  curl -X POST \ 
-HX -Parse-Application-Id:my_id\
-HX-Parse-REST-API-Key:api_id\
-HContent-Type:application / json\
-d{\SiteID\:\foundID \,\dataUsedString\:\foundUsage\,\usageDate\:\ foundDate \,\monthString\:\foundMonth\,\dayString\:\foundDay\,\yearString\:\foundYear\\ \\}\
https://api.parse.com/1/classes/MyClass

已解决的答案:



我已经创建了此php脚本来复制命令:

 <?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发送请求,第二个是要发送的数据。 (RAW DATA正在以字符串形式发送到POSTFIELDS,如果你发送数组,那么它将自动追加标题multipart / form-data。

  $ ch = curl_init('https://api.parse.com/1/classes/MyClass'); 

curl_setopt($ ch,CURLOPT_HTTPHEADER,
array $ b'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);


I am having trouble converting my curl command into php.

This part works great.

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

SOLVED ANSWER:

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);
?>

解决方案

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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆