2016-07-30 156 views
0

我遇到了一個問題,試圖從表單內發出POST到chargify API日誌付款。PHP curl/json問題POST

但是,由於某種原因,它實際上是作爲GET發送的,因爲它返回了一個錯誤。

任何人都可以提供任何意見,我可能做錯了什麼?郵差工作正常工作發送與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);  

    } 
+0

請張貼你得到的輸出。 –

+0

錯誤404.但我知道的網址是好的,因爲我可以張貼到它沒有問題,使用郵遞員和發送json在身體 – firecasey

回答

0

我用這一個代碼,但它會返回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); 
+0

身份驗證處理的URL使用://chargify.com ....我只是在我的隱私代碼中掩蓋了它。 – firecasey

+0

好吧 - 所以代碼幾乎和你的一樣 - 我已經刪除標題以全模式查看答案,並且沒有404錯誤。你可以嘗試相同的,併發送完整的答案,否則我認爲這是不可能的,以幫助你。 – Vyacheslav