2016-07-30 5 views
0

양식 내에서 chargify API 로그 결제에 POST를 실행하는 중 문제가 발생합니다.PHP curl/json 관련 문제 POST

그러나 오류를 반환하기 때문에 POST가 실제로 GET으로 보내지는 것으로 보입니다.

내가 잘못하고있는 것에 대해 조언 해 줄 사람이 있습니까? Postman은 JSON을 POST로 보내면 잘 작동하지만 PHP에서 작동하지는 않습니다.

:이 하나 개의 코드를 사용했습니다

//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

(주는 <urlmaskedforprivacy> 내 실제 코드에서 분명하지 않습니다)하지만 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

인증은 : : //chargify.com을 사용하여 URL에서 처리됩니다. 개인 정보 보호를 위해 제 코드에서 가면을 썼습니다. – firecasey

+0

글쎄 - 코드가 너와 거의 비슷해. 풀 모드로 응답을 보려면 헤더를 제거하고 404 오류는 제거하지 마라. 너는 똑같은 것을 시도하고 완전한 대답을 보낼 수 있는가? 그렇지 않으면 나는 너를 도울 수 없다고 생각한다. – Vyacheslav