解决 PayPal IPN 验证中的 "Access Denied" 问题


解决 paypal ipn 验证中的

本文档旨在帮助开发者解决在使用 PHP 验证 PayPal IPN (Instant Payment Notification) 时遇到的 "Access Denied" 错误。通过详细的代码分析和配置调整,我们将指导你如何正确配置 cURL 请求头,从而成功验证 IPN 信息,确保交易的可靠性和安全性。

问题分析

在使用 PHP 脚本验证 PayPal IPN 时,出现 "Access Denied" 错误通常是由于服务器拒绝了你的请求。这可能是因为 PayPal 服务器需要特定的请求头信息才能正确处理你的请求。常见的解决方法是修改 cURL 请求头,添加 User-Agent 字段。

解决方案

以下是修改后的 PHP 代码示例,重点在于 CURLOPT_HTTPHEADER 的配置:

ViiTor AI ViiTor AI

一个强大的多语言AI语音合成和视频转译平台

ViiTor AI 9414 查看详情 ViiTor AI
    $raw_post_array = file_get_contents('php://input');
    $raw_post_array = explode('&', $raw_post_array);

    $myPost = array();
    foreach ($raw_post_array as $keyval) {
      $keyval = explode ('=', $keyval);
      if (count($keyval) == 2)
        $myPost[$keyval[0]] = urldecode($keyval[1]);
    }
    // read the IPN message sent from PayPal and prepend 'cmd=_notify-validate'
    $req = 'cmd=_notify-validate';
    if (function_exists('get_magic_quotes_gpc')) {
      $get_magic_quotes_exists = true;
    }
    foreach ($myPost as $key => $value) {
      if ($get_magic_quotes_exists == true && get_magic_quotes_gpc() == 1) {
        $value = urlencode(stripslashes($value));
      } else {
        $value = urlencode($value);
      }
      $req .= "&$key=$value";
    }

    // Step 2: POST IPN data back to PayPal to validate
    //Operacion: https://ipnpb.paypal.com/cgi-bin/webscr
    //Sandbox: https://ipnpb.sandbox.paypal.com/cgi-bin/webscr
    $ch = curl_init('https://ipnpb.sandbox.paypal.com/cgi-bin/webscr');
    curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $req);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 1);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
    curl_setopt($ch, CURLOPT_FORBID_REUSE, 1);
    // 修改 CURLOPT_HTTPHEADER 添加 User-Agent
    curl_setopt($ch, CURLOPT_HTTPHEADER, array('Connection: Close', 'User-Agent: YourCompanyName'));
    // In wamp-like environments that do not come bundled with root authority certificates,
    // please download 'cacert.pem' from "https://curl.haxx.se/docs/caextract.html" and set
    // the directory path of the certificate as shown below:
    // curl_setopt($ch, CURLOPT_CAINFO, dirname(__FILE__) . '/cacert.pem');
    if ( !($res = curl_exec($ch)) ) {
      // error_log("Got " . curl_error($ch) . " when processing IPN data");
      curl_close($ch);
      exit;
    }
    curl_close($ch);

    // inspect IPN validation result and act accordingly
    if (strcmp ($res, "VERIFIED") == 0) {
      // The IPN is verified, process it:
      // check whether the payment_status is Completed
      // check that txn_id has not been previously processed
      // check that receiver_email is your Primary PayPal email
      // check that payment_amount/payment_currency are correct
      // process the notification
      // assign posted variables to local variables
      $item_name = $_POST['item_name'];
      $item_number = $_POST['item_number'];
      $payment_status = $_POST['payment_status'];
      $payment_amount = $_POST['mc_gross'];
      $payment_currency = $_POST['mc_currency'];
      $txn_id = $_POST['txn_id'];
      $receiver_email = $_POST['receiver_email'];
      $payer_email = $_POST['payer_email'];

      $header = "From: <a class=\"__cf_email__\" data-cfemail=\"137a7d757c537e7c7d76616a3d707c7e\" href=\"/cdn-cgi/l/email-protection\">[email&#160;protected]</a>\nReply-To:".$payer_email."\n";
      $mensaje = "Se recibio " . $item_name . ", status fue: " . $payment_status . ", el recibidor fue: " . $receiver_email . ", el pagador fue: " . $payer_email . ". Se cobro $" . $payment_amount;
      mail('<a class="__cf_email__" data-cfemail="e18c8e8f849398d3d1a1868c80888dcf828e8c" href="/cdn-cgi/l/email-protection\">[email&#160;protected]</a>',$item_name, $mensaje, $header);
      // IPN message values depend upon the type of notification sent.
      // To loop through the &_POST array and print the NV pairs to the screen:
      foreach($_POST as $key => $value) {
        echo $key . " = " . $value . "<br>";
      }
    } else{
      // IPN invalid, log for manual investigation
        $payer = '<a class="__cf_email__" data-cfemail="e78a888982959ed5d7a7808a868e8bc984888a" href="/cdn-cgi/l/email-protection\">[email&#160;protected]</a>';
        $item_name = 'Ha fallado';
      echo "The response from IPN was: <b>" .$res ."</b>";
      $header = "From: <a class=\"__cf_email__\" data-cfemail=\"e0898e868fa08d8f8e859299ce838f8d\" href=\"/cdn-cgi/l/email-protection\">[email&#160;protected]</a>\nReply-To:".$payer."\n";
      $mensaje = "Fue invalido, recibiste " . $res;
      mail('<a class="__cf_email__" data-cfemail="cba6a4a5aeb9b2f9fb8baca6aaa2a7e5a8a4a6" href="/cdn-cgi/l/email-protection\">[email&#160;protected]</a>',$item_name, $mensaje, $header);
    }

关键代码解释:

curl_setopt($ch, CURLOPT_HTTPHEADER, array('Connection: Close', 'User-Agent: YourCompanyName'));
  • User-Agent: YourCompanyName:这行代码设置了 User-Agent 请求头。将 YourCompanyName 替换为你公司的名称或其他标识符。

其他注意事项

  1. SSL 证书验证: 在生产环境中,确保 CURLOPT_SSL_VERIFYPEER 设置为 true,并配置正确的 CA 证书。在开发环境中,如果遇到 SSL 证书问题,可以暂时设置为 false,但强烈建议在生产环境中不要这样做。
  2. Sandbox 环境: 在测试阶段,请使用 PayPal Sandbox 环境(https://ipnpb.sandbox.paypal.com/cgi-bin/webscr)。
  3. 错误日志: 在代码中添加详细的错误日志,以便在出现问题时进行调试。
  4. IPN 监听器 URL: 确保你的 IPN 监听器 URL 在 PayPal 账户中正确配置。
  5. 数据验证: 在处理 IPN 数据时,务必进行严格的验证,包括支付状态、交易 ID、接收者邮箱、支付金额和货币等。

总结

通过正确配置 cURL 请求头,特别是添加 User-Agent 字段,可以有效解决 PayPal IPN 验证中的 "Access Denied" 问题。同时,注意 SSL 证书验证、Sandbox 环境的使用以及数据验证,可以确保 IPN 验证的可靠性和安全性。

以上就是解决 PayPal IPN 验证中的 "Access Denied" 问题的详细内容,更多请关注php中文网其它相关文章!


# 为你  # 小企业网站建设与推广  # 唐山seo案例  # 巴中seo优化报价  # 海淀区酒店网站建设  # 主题推广语 营销方案  # 杉杉来辞官方网站推广达人  # 武汉seo搜索排名技术  # 盐城网站建设产品介绍  # 杭州营销推广哪里有  # 城南seo优化  # 或其他  # 这样做  # 中文网  # 请使用  # 相关文章  # php  # 是因为  # 设置为  # 怎么看  #   # 开发环境  # 邮箱  # 解决方法  # cdn  # ai  # curl  # ssl  # access  # go  # html 


相关栏目: 【 Google疑问12 】 【 Facebook疑问10 】 【 优化推广96088 】 【 技术知识133117 】 【 IDC资讯59369 】 【 网络运营7196 】 【 IT资讯61894


相关推荐: 圆通快递包裹轨迹查询 圆通速递快件实时位置跟踪  《优志愿》修改手机号方法  Git命令与VS Code UI操作的对应关系解析  京东快递包裹信息查询入口 京东快递官方查询平台入口  《大学搜题酱》官网地址登录  漫蛙manwa漫画官网链接_漫蛙manwa最新可用网址推荐  macosmonterey系统外接显示器驱动怎么安装_macosmonterey外接显示器驱动与分辨率调整  J*a中导出MySQL表为SQL脚本的两种方法  可米酷漫画在线阅读入口_ 可米酷漫画官网直达链接  word文档中的分隔符有哪些不同类型和用途_Word分隔符类型与用途方法  Go语言中方法接收器的选择:值类型还是指针类型?  mysql中如何分析索引使用情况_mysql索引使用分析方法  荣耀Magic7拍照夜景噪点处理_荣耀Magic7相机优化  如何在CSS中设置背景图像:一个全面指南  《杖剑传说》食谱大全  汽水音乐官方网站登录入口_汽水音乐网页版进入链接  如何在CSS中使用过渡制作按钮边框渐变_border-color transition实现  抖音火山版如何进行提现  百度竞价WAP显示PC链接问题  德邦快递收费标准详解  个人所得税办理入口 个人所得税综合所得年度汇算入口  《东方财富》条件单关闭方法  Windows自带的便笺数据如何备份_防止数据丢失的便利贴迁移教程【干货】  Win11怎么设置分辨率 Win11显示设置调整分辨率及刷新率修改  使用CSS :has() 选择器实现父元素样式控制:从子元素反向应用样式  iSpring三分屏制作教程  C#解析并修改XML后保存 如何确保格式与编码的正确性  HTML与J*aScript实现下拉菜单驱动的动态表格:构建交互式维修表单  网易云音乐闹钟铃声设置教程  b站网页版入口 哔哩哔哩官方网站直接进入  研招网官方网站招生平台入口_中国研究生招生信息网官网登录  PyEZ 配置提交中 RpcTimeoutError 的健壮性处理策略  微信网页版在线登录 微信网页版在线使用入口  WooCommerce 新客户订单自动添加管理员备注教程  J*aScript类型数组_TypedArray使用  汽水音乐官网网页版入口 汽水音乐官网网页版在线入口  MacBook Pro词典使用指南  《oppo商城》维修服务位置  CSS动画如何实现图标旋转并放大_transform rotate scale @keyframes实现  Firefox OS应用开发:解决XMLHttpRequest跨域请求阻塞问题  掌握CSS :has() 选择器:父选择器、嵌套限制与常见陷阱解析  《土豆雅思》修改密码方法  CSS如何控制元素外边距_margin实现布局间隔  教育查询官方网站入口 教育个人档案查询免费官网  Go反射进阶:访问内嵌结构体中的被遮蔽方法  AO3中文入口稳定分享_AO3官网HTTPS看文详解  抖音如何进行蓝V认证 抖音企业号申请所需资料与流程  漫蛙manwa官网浏览入口_漫蛙漫画网页版访问链接  《兴业银行》注册登录方法  《漫蛙manwa2》防走失网页版链接2025 

 2025-11-03

了解您产品搜索量及市场趋势,制定营销计划

同行竞争及网站分析保障您的广告效果

点击免费数据支持

提交您的需求,1小时内享受我们的专业解答。

运城市盐湖区信雨科技有限公司


运城市盐湖区信雨科技有限公司

运城市盐湖区信雨科技有限公司是一家深耕海外推广领域十年的专业服务商,作为谷歌推广与Facebook广告全球合作伙伴,聚焦外贸企业出海痛点,以数字化营销为核心,提供一站式海外营销解决方案。公司凭借十年行业沉淀与平台官方资源加持,打破传统外贸获客壁垒,助力企业高效开拓全球市场,成为中小企业出海的可靠合作伙伴。

 8156699

 13765294890

 8156699@qq.com

Notice

We and selected third parties use cookies or similar technologies for technical purposes and, with your consent, for other purposes as specified in the cookie policy.
You can consent to the use of such technologies by closing this notice, by interacting with any link or button outside of this notice or by continuing to browse otherwise.