简体   繁体   English

PHP curl / json POST问题

[英]Issue with PHP curl / json POST

I am running into an issue trying to issue a POST to the chargify API log payments from within a form. 我遇到了一个问题,试图从表单内向CHARGify API日志付款发出POST。

However, it appears that the POST is actually being sent as a GET for some reason as it's returning an error. 但是,由于某种原因,由于实际上返回了错误,因此POST实际上是作为GET发送的。

Can anyone offer any advice on what I may be doing wrong? 有人可以针对我可能做错的事情提供任何建议吗? Postman is working just fine sending a POST with the JSON as the body, but I can't get it to work from PHP. Postman可以很好地发送以JSON为主体的POST,但我无法从PHP使其正常工作。

(note: the <urlmaskedforprivacy> is obviously not in my actual code) (注意: <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);      

    }

I've used this one code, but it returns 401 error and asks for Basic auth. 我已经使用了这一代码,但是它返回401错误并要求基本身份验证。 in json_response variable. 在json_response变量中。 May be you've missed it? 可能您错过了吗?

$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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM