duanluan3651 2014-05-31 10: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 10: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条)
编辑
预览

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部