2

我正在尝试使用 ZenDesk 的 API 通过我的网站上的付款表格来设置帐户创建。他们给出的示例代码是:

curl -v -u {email_address}:{password} https://{subdomain}.zendesk.com/api/v2/users.json \
  -H "Content-Type: application/json" -X POST -d '{"user": {"name": "Roger Wilco",     "email": "roge@example.org"}}'

由于我需要包含 PHP 变量,因此我尝试使用它:

$data = array("name" => $entry["1"], "email" => $entry["3"], "role" => "end-user");                                                                    
$data_string = json_encode($data); 

$ch = curl_init('https://xxxx.zendesk.com/api/v2/users.json');
curl_setopt($ch, CURLOPT_USERPWD, "xxxx@example.com:xxxx");                                                                      
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");                                                                     
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);                                                                  
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);                                                                      
curl_setopt($ch, CURLOPT_HTTPHEADER, array(                                                                          
'Content-Type: application/json',                                                                                
'Content-Length: ' . strlen($data_string))                                                                       
);                                                                                                                   

$result = curl_exec($ch);

但是,它不起作用。我的代码在复制第一个片段的功能方面是否正确?

4

2 回答 2

6

我找到了 ZenDesk 的 API 的另一个示例,并且能够想出这个:

<?PHP
define("ZDAPIKEY", "SECRETKEYGOESHERE");
define("ZDUSER", "me@mysite.com");
define("ZDURL", "https://mysite.zendesk.com/api/v2");

/* Note: do not put a trailing slash at the end of v2 */

function curlWrap($url, $json, $action)
{
  $ch = curl_init();
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
    curl_setopt($ch, CURLOPT_MAXREDIRS, 10 );
    curl_setopt($ch, CURLOPT_URL, ZDURL.$url);
    curl_setopt($ch, CURLOPT_USERPWD, ZDUSER."/token:".ZDAPIKEY);
    switch($action){
            case "POST":
            curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
            curl_setopt($ch, CURLOPT_POSTFIELDS, $json);
            break;
        case "GET":
            curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET");
            break;
        case "PUT":
            curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT");
            curl_setopt($ch, CURLOPT_POSTFIELDS, $json);
        default:
            break;
    }
    curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-type: application/json'));
    curl_setopt($ch, CURLOPT_USERAGENT, "MozillaXYZ/1.0");
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_TIMEOUT, 10);
    $output = curl_exec($ch);
    curl_close($ch);
    $decoded = json_decode($output);
    return $decoded;
}

$arr = array("z_name"=>$namevariable,
       "z_email"=>$emailvariable,
       "z_role"=>"end_user",
       "z_verified"=>"yes"
      );
$create = json_encode(array('user' => array('name' => $arr['z_name'], 'email' =>     $arr['z_email'], 'role' => $arr['z_role'])), JSON_FORCE_OBJECT);
$data = curlWrap("/users.json", $create, "POST");
var_dump($data);
?>

它似乎是独立工作的,所以这回答了这里存在的问题。

谢谢大家的帮助 :)

于 2013-08-23T12:26:53.337 回答
0

我知道问题已得到解答,但由于我在遇到相同问题时发现它并且由于它没有解决我的问题,所以我想我会发布我所做的。希望它可以帮助别人。

在我必须通过 PUT 提交 json 数据的情况下,这是对我有用的组合:

        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_FRESH_CONNECT, TRUE);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
        curl_setopt($ch, CURLOPT_TIMEOUT, 90);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $json);
        curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-type: application/json', 'Content-Length: ' . strlen($json), 'X-HTTP-Method-Override: PUT'));

请注意,它不需要CURLOPT_CUSTOMREQUESTCURLOPT_PUT因为该X-HTTP-Method-Override: PUT参数会处理该问题。

于 2014-05-07T21:11:06.547 回答