Issue with PHP curl / json POST


Issue with PHP curl / json POST

我在试图从表单中向chargify API日志付款发出POST时遇到问题。

然而,由于某种原因,POST实际上是作为GET发送的,因为它返回了一个错误。

有人能就我可能做错了什么提出建议吗?Postman可以很好地发送以JSON为主体的POST,但我无法让它在PHP中工作。

(注意:<urlmaskedforprivacy>显然不在我的实际代码中)

    //find invoice id # using invoice number search form
    $invoiceNumber = fRequest::get('invoicenumber');
    $json = file_get_contents('https://<maskedforprivacy>.chargify.com/invoices.json?number=' . $invoiceNumber);
    $returnedInvData = fJSON::decode($json);
    $invoiceId = $returnedInvData[0]->invoice->id;

    // grab form data as payment info to send to chargify
    $paymentAmount = fRequest::Get('amount');
    $memo = fRequest::Get('memo');
    if ((isset($invoiceNumber,$paymentAmount,$memo))) {
    $url = 'https://<maskedforprivacy>.chargify.com/subscriptions/' . $invoiceId . '/payments.json';
    $params = array(
                      'payment' => array(
                           'amount'   => $paymentAmount,
                           'memo'    => (string)$memo
                      ),
                );
    // encode as json               
    $content = FJSON::encode($params);

    // send to chargify
    $curl = curl_init($url);
    curl_setopt($curl, CURLOPT_HEADER, false);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($curl, CURLOPT_HTTPHEADER,
            array("Content-type: application/json"));
    curl_setopt($curl, CURLOPT_POST, true);
    curl_setopt($curl, CURLOPT_POSTFIELDS, $content);
    $json_response = curl_exec($curl);
    $status = curl_getinfo($curl, CURLINFO_HTTP_CODE);
    if ( $status != 201 ) {
        die("Error: call to URL $url failed with status $status, response $json_response, curl_error " . curl_error($curl) . ", curl_errno " . curl_errno($curl));
    }

    curl_close($curl);
    $response = json_decode($json_response, true);      
    }

我已经使用了这一段代码,但它返回401错误并要求基本身份验证。在json_response变量中。也许你错过了?

$paymentAmount = 10;
    $memo = 'test';

    $url = 'https://test.chargify.com/subscriptions/1/payments.json';
    $params = array(
                      'payment' => array(
                           'amount'   => $paymentAmount,
                           'memo'    => (string)$memo
                      ),
                );
    // encode as json
    $content = json_encode($params);

    // send to chargify
    $curl = curl_init($url);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($curl, CURLOPT_HTTPHEADER,
            array("Content-type: application/json"));
    curl_setopt($curl, CURLOPT_POST, true);
    curl_setopt($curl, CURLOPT_POSTFIELDS, $content);
    $json_response = curl_exec($curl);
    $status = curl_getinfo($curl, CURLINFO_HTTP_CODE);
    if ( $status != 201 ) {
        die("Error: call to URL $url failed with status $status, response $json_response, curl_error " . curl_error($curl) . ", curl_errno " . curl_errno($curl));
    }

    curl_close($curl);
    $response = json_decode($json_response, true);