duanluan3651 2014-05-31 18:33
浏览 73
已采纳

PHP卷曲格式[关闭]

I'd like to use the FreshDesk API, say, the 'create a ticket' command.
I have used POST cUrl before, but the example given there confuses me and I am not quite sure how to concretely make a cUrl call as provided there,

curl -u user@yourcompany.com:test -H "Content-Type: application/json" -d '{ "helpdesk_ticket": { "description": "Details about the issue...", "subject": "Support Needed...", "email": "tom@outerspace.com", "priority": 1, "status": 2 }, "cc_emails": "ram@freshdesk.com,diana@freshdesk.com" }' -X POST http://domain.freshdesk.com/helpdesk/tickets.json

I noticed this is a shell command, and I do not have access to shell commands.
However, I have successfully been able to use cUrl POST API's before (with Disqus).
There, API calls would look like

$thread = "param1";
$remote_auth_s3 = "param2";
$forum = "param3";
$api = "param4";
$url = 'http://disqus.com/api/3.0/posts/create.json';

$fields = array(
    'api_secret'=>urlencode($api),
    'thread'=>urlencode($thread),
    'remote_auth'=>urlencode($remote_auth_s3),
    'message'=>urlencode($message)
);

foreach($fields as $key=>$value) { $fields_string .= $key.'='.$value.'&'; }
rtrim($fields_string,'&');
$ch = curl_init();
curl_setopt($ch,CURLOPT_URL,$url);
curl_setopt($ch,CURLOPT_POST,count($fields));
curl_setopt($ch,CURLOPT_POSTFIELDS,$fields_string);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

$result = curl_exec($ch);
curl_close($ch);

$results = json_decode($result, true);
if ($results === NULL) die('Error parsing json');

This seemed all very logical, but how am I supposed to convert the shell command to this format of a cUrl call?
This might be a very stupid question, but also a honest one. I did find two others with the same problem, but they did not need to also include POST Parameters, so please don't mark this as a duplicate.

I hope someone is able to solve my dilemma.

  • 写回答

2条回答 默认 最新

  • douzhun5971 2014-05-31 18:48
    关注

    Use curl_setopt($ch,CURLOPT_POSTFIELDS,json_encode($fields)); to send a json encoded array to the API.

    Your example array would look like:

    $fields=array(
     'helpdesk_ticket'=>array(
      'description'=>'Details about the issue...',
      'subject'=>'Support needed...',
      'email'=>'tom@outerspace.com',
      'priority'=>1,
      'status'=>2
     ),
     'cc_emails'=>'ram@freshdesk.com,diana@freshdesk.com'
    );
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 怎样才能让鼠标沿着线条的中心线轨迹移动
  • ¥60 用visual studio编写程序,利用间接平差求解水准网
  • ¥15 Llama如何调用shell或者Python
  • ¥20 谁能帮我挨个解读这个php语言编的代码什么意思?
  • ¥15 win10权限管理,限制普通用户使用删除功能
  • ¥15 minnio内存占用过大,内存没被回收(Windows环境)
  • ¥65 抖音咸鱼付款链接转码支付宝
  • ¥15 ubuntu22.04上安装ursim-3.15.8.106339遇到的问题
  • ¥15 blast算法(相关搜索:数据库)
  • ¥15 请问有人会紧聚焦相关的matlab知识嘛?