为什么当我尝试在php文件中使用curl时我被禁止使用

时间:2019-12-17 15:22:43

标签: php curl

我试图将付款方式集成到网站上,第一件事是,我尝试使用curl代码使用git console对其进行测试,并且效果很好,然后尝试使用PHP执行curl命令。我创建了一个文件,然后使用了以下代码:

<?php
$endpoint_url = 'https://secure.payinspect.com';
$params = [
  'action'=>'SALE',
  'order_id'=>'ORDER12345',
  'order_amount'=>'1.99',
  'order_currency'=>'USD',
  'order_description'=>'Product',
  'card_number'=>'4111111111111111',
  'card_exp_month'=>'05',
  'card_exp_year'=>'2020',
  'card_cvv2'=>'000',
  'payer_first_name'=>'John',
  'payer_last_name'=>'Doe',
  'payer_address'=>'BigStreet',
  'payer_country'=>'US',
  'payer_state'=>'CA',
  'payer_city'=>'City',
  'payer_zip'=>'123456',
  'payer_email'=>'doe@example',
  'payer_phone'=>'199999999',
  'payer_ip'=>'123.123.123.123',
  'term_url_3ds'=>'http://client.site.com/return.php',
  'recurring_init'=>'N',
  'hash'=>'e3dd86f469f40a5cfedf96a82ff257af'
];
$buff = [];
foreach ($params as $k => $v) {
  array_push($buff, "{$k}={$v}");
}
$url = $endpoint_url . implode('&', $buff);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
   curl_close($ch);
    if ($result===false){ print curl_error($curl); }
$response = json_decode($result, true);

echo $result;

?>

但是我遇到了这个错误:

Forbidden
You don't have permission to access

我搜索了此错误,然后尝试添加此行

curl_setopt($ch,CURLOPT_USERAGENT,'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/60.0.3112.113 Safari/537.36');
$result = curl_exec($ch);

但是我仍然遇到同样的错误。
那么什么原因导致了这个问题以及如何解决

1 个答案:

答案 0 :(得分:0)

假设其他所有内容都正确-此可能可以解决问题。

<?php
$endpoint_url = 'https://secure.payinspect.com';
$params = [
  'action'=>'SALE',
  'order_id'=>'ORDER12345',
  'order_amount'=>'1.99',
  'order_currency'=>'USD',
  'order_description'=>'Product',
  'card_number'=>'4111111111111111',
  'card_exp_month'=>'05',
  'card_exp_year'=>'2020',
  'card_cvv2'=>'000',
  'payer_first_name'=>'John',
  'payer_last_name'=>'Doe',
  'payer_address'=>'BigStreet',
  'payer_country'=>'US',
  'payer_state'=>'CA',
  'payer_city'=>'City',
  'payer_zip'=>'123456',
  'payer_email'=>'doe@example',
  'payer_phone'=>'199999999',
  'payer_ip'=>'123.123.123.123',
  'term_url_3ds'=>'http://client.site.com/return.php',
  'recurring_init'=>'N',
  'hash'=>'e3dd86f469f40a5cfedf96a82ff257af'
];

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $endpoint_url);

// -- this sets the request method to POST ----
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($params));
// --- end ----

curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_close($ch);

if ($result===false) { print curl_error($curl); }
$response = json_decode($result, true);

echo $result;

我不能肯定地说(这不是一个很大的答案),但是通过在URL后面加上参数来完成当前的创建工作 GET 请求而不是 POST 请求。

接收服务很有可能希望在 POST 数据中看到您的哈希值(以及其他所有内容),并且因为那里看不到它,所以它会完全拒绝您的请求。