PHP curl/json问题POST
我遇到了一个问题,试图从表单内发出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);
}
我用这一个代码,但它会返回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);
身份验证处理的URL使用
好吧 - 所以代码几乎和你的一样 - 我已经删除标题以全模式查看答案,并且没有404错误。你可以尝试相同的,并发送完整的答案,否则我认为这是不可能的,以帮助你。 – Vyacheslav
请张贴你得到的输出。 –
错误404.但我知道的网址是好的,因为我可以张贴到它没有问题,使用邮递员和发送json在身体 – firecasey