You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

WordPress中cURL调用Ongage API出现400/404错误的排查求助

WordPress中cURL调用Ongage API的400/404错误排查与解决

问题场景

用户在WordPress页面提交表单时触发自定义函数,新增的Ongage Transactional Mailing API调用代码通过PHP Inserter插入后台。该API在Postman测试正常,但在WordPress环境中调用失败:

  • 初始代码因list_id和campaign_id参数缺失,返回400状态码
  • 补充参数后,返回404状态码,怀疑请求内容或配置存在问题

相关代码

初始代码(触发400错误)

function test(){
  $url     = "https://api.ongage.net/MY_LIST_ID/api/transactional/send";
  $xun = "X_USERNAME: " . MY_USERNAME;
  $xpw = "X_PASSWORD: " . MY_PASSWORD;
  $xac = "X_ACCOUNT_CODE: " . MY_ACCOUNTCODE;
  $content = '{
   "list_id": ,
   "campaign_id": ,
   "recipients": ["myEmail@email.com"],
   "check_status": true,
   "sending_connection_id": MY_SENDING_CONNECTION_ID
  }';

  $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",
    $xun,
    $xpw,
    $xac 
  ) );
  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 != 200 && $status != 201 ) {
    $error = error_log( "Error: call to URL $url failed with status $status, 
      response $json_response, curl_error " . curl_error( $curl ) . ", curl_errno " . curl_errno( $curl ) );
    echo '<script language="javascript">';
    echo 'alert("fail "+'.$status.')';
    echo '</script>';
} else {
    echo '<script language="javascript">';
    echo 'alert("success")';
    echo '</script>';
}
  curl_close( $curl );
  $response = json_decode( $json_response, true );
}

修改后的$content(触发404错误)

$content = '{
   "list_id": MY_LIST_ID,
   "campaign_id": MY_CAMAPAIGN_ID,
   "recipients": ["myEmail@email.com"],
   "check_status": true,
   "sending_connection_id": MY_SENDING_CONNECTION_ID
  }';

错误原因分析

  1. 初始400错误:无效JSON格式
    初始代码中list_id和campaign_id字段为空,导致JSON语法无效,API返回400 Bad Request。

  2. 修改后404错误的可能原因

    • URL路径错误:初始URL包含多余的MY_LIST_ID路径段,不符合Ongage API的端点规范,Postman测试时使用的正确URL无此段,导致WordPress中请求的资源不存在。
    • JSON变量解析失败:手动拼接JSON时,MY_LIST_ID等常量未被PHP解析,被当作字符串字面量传递给API,导致找不到对应资源。
    • 常量拼写错误:MY_CAMAPAIGN_ID存在拼写错误(应为MY_CAMPAIGN_ID),传递了无效的活动ID。
    • 常量未正确定义:MY_LIST_ID、MY_CAMPAIGN_ID等常量未在WordPress环境中正确定义,传递了无效值。

解决方法

1. 修正API端点URL

移除URL中的多余路径段,使用规范的正确端点:

$url = "https://api.ongage.net/api/transactional/send";

2. 用json_encode生成请求体

避免手动拼接JSON导致的语法错误和变量解析问题,通过数组构建请求内容后转JSON:

$payload = array(
    "list_id" => MY_LIST_ID,
    "campaign_id" => MY_CAMPAIGN_ID, // 修正拼写错误
    "recipients" => ["myEmail@email.com"],
    "check_status" => true,
    "sending_connection_id" => MY_SENDING_CONNECTION_ID
);
$content = json_encode($payload);

3. 验证常量定义与拼写

确保MY_USERNAME、MY_PASSWORD、MY_ACCOUNTCODE、MY_LIST_ID、MY_CAMPAIGN_ID等常量已在WordPress中正确定义(可通过define()函数在主题functions.php或插件中添加),且无拼写错误。

4. 增强错误日志排查

捕获API返回的详细错误信息,便于定位问题:

$error_msg = "Error: call to URL $url failed with status $status, response: $json_response, curl_error: " . curl_error($curl) . ", curl_errno: " . curl_errno($curl);
error_log($error_msg);

修正后的完整代码

function test(){
    // 修正API端点URL
    $url = "https://api.ongage.net/api/transactional/send";
    
    // 构建请求头
    $headers = array(
        "Content-type: application/json",
        "X_USERNAME: " . MY_USERNAME,
        "X_PASSWORD: " . MY_PASSWORD,
        "X_ACCOUNT_CODE: " . MY_ACCOUNTCODE
    );
    
    // 用数组构建请求体,避免JSON拼接错误
    $payload = array(
        "list_id" => MY_LIST_ID,
        "campaign_id" => MY_CAMPAIGN_ID, // 修正拼写错误
        "recipients" => ["myEmail@email.com"],
        "check_status" => true,
        "sending_connection_id" => MY_SENDING_CONNECTION_ID
    );
    $content = json_encode($payload);

    $curl = curl_init($url);
    curl_setopt($curl, CURLOPT_HEADER, false);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
    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 != 200 && $status != 201) {
        // 记录详细错误信息
        $error_msg = "Error: call to URL $url failed with status $status, response: $json_response, curl_error: " . curl_error($curl) . ", curl_errno: " . curl_errno($curl);
        error_log($error_msg);
        echo '<script>alert("请求失败,状态码:' . $status . ',详情请查看错误日志");</script>';
    } else {
        echo '<script>alert("请求成功");</script>';
    }
    
    curl_close($curl);
    $response = json_decode($json_response, true);
}

内容的提问来源于stack exchange,提问作者John Beasley

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.18 11:07:06