Trouble converting POST curl from command line to php

前端 未结 2 1646
执笔经年
执笔经年 2021-01-24 21:13

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          


        
2条回答
  •  天涯浪人
    2021-01-24 21:47

    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:)

提交回复
热议问题