Square Webhook Curl Put请求

时间:2018-04-13 13:39:06

标签: php curl webhooks square

Square的API文档规定,要设置Web挂钩,您只需执行以下CURL请求:

curl -X PUT -H“授权:Bearer PERSONAL_ACCESS_TOKEN”-H“Content-Type:application / json”-d“[\”PAYMENT_UPDATED \“]”https://connect.squareup.com/v1/LOCATION_ID/webhooks

我在PHP中将其翻译成以下内容:

    echo "<br>curl started<br>";

    // create curl resource
    $ch = curl_init();

    $data = array(
    "[\"PAYMENT_UPDATED\"]",
    );

    // set url

    curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "PUT");

    curl_setopt($curl, CURLOPT_HTTPHEADER, array("Authorization: Bearer XYZXYZXYZXYZXYZXYZ", "Content-type: application/json\r\n"));

    curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($data));

    curl_setopt($ch, CURLOPT_URL, "https://connect.squareup.com/v1/XYZXYZXYZXYZXYZXYZ/webhooks");

    //return the transfer as a string
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

    // $output contains the output string
    $output = curl_exec($ch);

    echo $output;

    // close curl resource to free up system resources
    curl_close($ch);     

    echo "<br>curl ends<br>";

但没有快乐,显然我的“授权标题格式不正确”,但我不知道如何解决它?

1 个答案:

答案 0 :(得分:1)

您遇到了代码问题。运行代码时,除了描述的输出外,您还应该看到以下错误:

<br>curl started<br>
Warning: curl_setopt() expects parameter 1 to be resource, null given in /Users/tristans/Desktop/test.php on line 13

Warning: curl_setopt() expects parameter 1 to be resource, null given in /Users/tristans/Desktop/test.php on line 15

Warning: curl_setopt() expects parameter 1 to be resource, null given in /Users/tristans/Desktop/test.php on line 17
{"type":"unauthorized","message":"Authorization header format incorrect"}<br> 

如果您没有看到,则应提高日志记录级别。问题是您混淆了$ch$curl

试试这段代码:

<?php
echo "<br>curl started<br>";

// create curl resource
$ch = curl_init();

$data = array(
"[\"PAYMENT_UPDATED\"]",
);

// set url

curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT");

curl_setopt($ch, CURLOPT_HTTPHEADER, array("Authorization: Bearer sq0atp-XXXXXXXXXXX", "Content-type: application/json\r\n"));

curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));

curl_setopt($ch, CURLOPT_URL, "https://connect.squareup.com/v1/XXXXXXXXXX/webhooks");

//return the transfer as a string
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

// $output contains the output string
$output = curl_exec($ch);

echo $output;

// close curl resource to free up system resources
curl_close($ch);

echo "<br>curl ends<br>";
?>