使用CURL发布带有PHP变量的JSON

时间:2013-08-22 17:48:30

标签: php json curl zendesk

我正在尝试使用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);

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

2 个答案:

答案 0 :(得分: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);
?>

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

感谢大家的帮助:)。

答案 1 :(得分: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参数会处理此问题。