PHP curl / json POST

时间:2016-07-30 21:51:37

标签: php json curl

我遇到了一个问题,试图从表单中向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);      

    }

1 个答案:

答案 0 :(得分:0)

我已经使用了这一个代码,但它返回401错误并要求进行Basic身份验证。在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);