我正在尝试与Clickbank API进行交互以创建支持服务单。 (https://api.clickbank.com/rest/1.2/tickets/)
我似乎无法使用CURL。我可以使用GET(获取订单信息)使API工作正常,但不能使用POST(更改订单所需)。
这是我的代码,它从Clickbank返回错误404。
<?php
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://api.clickbank.com/rest/1.2/tickets/FNKBEP34?type=cncl&reason=ticket.type.cancel.7");
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_NOBODY, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Accept: application/xml", "Authorization: removed_dev_key:removed_api_key"));
$result = curl_exec($ch);
curl_close($ch);
print $result;
?>
我已经能够使用RESTClient2.3成功发布POST。但是我无法用CURL复制它。
任何帮助都会非常感激,我一整天都被困在这里!
答案 0 :(得分:1)
我的狂野猜测是RestClient试图变得聪明并且实际上将你的获取url参数转换为动态发布变量(虽然可能是错误的)。
Anyhoo ... POST,你做错了。
curl POST请求应该是这样的......
<?php
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://api.clickbank.com/rest/1.2/tickets/FNKBEP34");
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_POST, true);
//Take this out: curl_setopt($ch, CURLOPT_NOBODY, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, "type=cncl&reason=ticket.type.cancel.7");
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Accept: application/xml", "Authorization: removed_dev_key:removed_api_key"));
$result = curl_exec($ch);
curl_close($ch);
print $result;
?>
答案 1 :(得分:1)
经过大约10个半小时的反复试验......我明白了。
问题是两个人。
我正在运行PHP 5.2.4,它有一个接受自定义标头的错误。我发现这个代码完美无缺。
if(version_compare(PHP_VERSION,'5.3.0')== -1){ ini_set('user_agent','PHP-SOAP /'。PHP_VERSION。“\ r \ n”。$ options ['header']); }
卷曲不起作用。期。我尝试了一切。我真的不知道为什么。
这是我最终得到的最终代码,它完美无缺。
这通过POST与Clickbank API连接。我希望这可以帮助很多人。
<?
$options = array(
'http'=>array(
'method'=>"POST",
'header'=>
"Accept: application/xml\r\n".
"Authorization: DEV-key-here:API-key-here\r\n"
)
));
if(version_compare(PHP_VERSION, '5.3.0') == -1){
ini_set('user_agent', 'PHP-SOAP/' . PHP_VERSION . "\r\n" . $options['header']);
}
$context = stream_context_create($options);
$xml = file_get_contents('https://api.clickbank.com/rest/1.2/tickets/VZR9RYE3?reason=ticket.type.cancel.7&type=cncl',false,$context);
?>
答案 2 :(得分:0)
<?php
function executeCurl($arrOptions) {
$mixCH = curl_init();
foreach ($arrOptions as $strCurlOpt => $mixCurlOptValue) {
curl_setopt($mixCH, $strCurlOpt, $mixCurlOptValue);
}
$mixResponse = curl_exec($mixCH);
curl_close($mixCH);
return $mixResponse;
}
// if need any http auth
$username = 'http-auth-username';
$password = 'http-auth-password';
$requestType = 'POST'; // this can be PUT or POST
// this can be $arrPostData = $_POST;
$arrPostData = array(
'key1' => 'value-1-for-k1y-1',
'key2' => 'value-2-for-key-2',
'key3' => array(
'key31' => 'value-for-key-3-1',
'key32' => array(
'key321' => 'value-for-key321'
)
),
'key4' => array(
'key' => 'value'
)
);
// you can set your post data
$postData = http_build_query($arrPostData); // raw php array
$postData = json_encode($arrPostData); // Only USE this when request json data
$arrResponse = executeCurl(array(
CURLOPT_URL => 'http://whatever-your-request-url.com/xyz/yii',
CURLOPT_RETURNTRANSFER => true,
CURLOPT_HTTPGET => true,
CURLOPT_VERBOSE => true,
CURLOPT_AUTOREFERER => true,
CURLOPT_CUSTOMREQUEST => $requestType,
CURLOPT_POSTFIELDS => $postData,
CURLOPT_HTTPHEADER => array(
"X-HTTP-Method-Override: " . $requestType,
'Content-Type: application/json', // Only USE this when request json data
),
// if required HTTP Authentication use below lines
CURLOPT_HTTPAUTH => CURLAUTH_BASIC,
CURLOPT_USERPWD => $username. ':' . $password
));