WP PHP REST API 在从不同网站通过curl 传递的某些文件上返回 403 错误

问题描述 投票:0回答:1

我在 WordPress 网站上开发了一个 REST API 端点。 它接受文件并在扫描后响应,我从未设置过 403 代码。

function api_fwd_response($HttpStatus,$code,$msg,$data=null){
    $resp=array("id"=>"infoPmi","code"=>$code,"message"=>$msg,"data"=>$data);
    $res = new WP_REST_Response($resp);
    $res->set_status($HttpStatus);
    return $res;
}

function api_fwd_init(WP_REST_Request $req){
    ...

    $res=api_fwd_file_parse($filePath,$ext);
    if(!is_array($res))
        $apiRes= api_fwd_response(200, 'FILE3', "Api_fwd_file_parse res is not an array", array( 'status' => 500 ) );
    else if(@$res["res"]=="OK"){
        $apiRes= api_fwd_response( 200,'OK', 'Success', $res );

//No one condition set the 403 code
return $apiRes;
                      
}

我通过 CURL PHP 从不同的网站进行 API 调用。

class fwdXcg_Api{
...

function CallCurl($data){
if(!$this->UserPw()){
    return false;
}
    $this->curl = curl_init();
    curl_setopt($this->curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
    curl_setopt($this->curl, CURLOPT_USERPWD, "{$this->user}:{$this->pw}");

    curl_setopt($this->curl, CURLOPT_URL, $this->url);
    curl_setopt($this->curl, CURLOPT_RETURNTRANSFER, 1);

    curl_setopt($this->curl, CURLOPT_POST, 1);
    curl_setopt($this->curl, CURLOPT_FAILONERROR, true);
 
    curl_setopt($this->curl, CURLOPT_CONNECTTIMEOUT, 1); 
    curl_setopt($this->curl, CURLOPT_TIMEOUT, 20); //timeout in seconds

    curl_setopt($this->curl, CURLOPT_POSTFIELDS, $data);
    $result = curl_exec($this->curl);

    if($result===false)
       $result= curl_error($this->curl);
    else if(is_string($result))
       $result= json_decode($result,true);
    curl_close($this->curl);
    return $result;

    }

function CallFileRead($filePath){
       
    if(!file_exists($filePath))
        return "File inesistente: $filePath";

    $ext = strtolower(pathinfo($filePath, PATHINFO_EXTENSION));
    
    if (function_exists('curl_file_create')) { // php 5.5+
       $cFile = curl_file_create($filePath);
    } else { 
       $cFile = '@' . realpath($filePath);
    }
    
    $data=array("type"=>"fileScan",'file_contents' => $cFile,"file_ext"=>$ext);
    return $this->CallCurl($data);
    }
}
 

API调用

...
$api=new fwdXcg_Api();

$ret=$api->CallFileRead(FWD_XCG_DIR_UPLOAD.$file);

echo "<pre>";
print_r($ret);
echo "</pre>";

一切正常,但对于某些文件,我收到 CURL 错误: “请求的 URL 返回错误:403”。 但正如之前所解释的,我从未在我的 API 端点中设置过该代码。

我认为问题出在我开发API的WordPress环境上,但我不知道为什么。 一些上传安全选项? 谢谢。

php wordpress http-status-code-403
1个回答
0
投票

该错误是由 Wordfence 阻止端点 WordPress 站点上的请求引起的。

我在

Wordfence > Tools > Live Traffic
下启用了“将参数添加到白名单”。

© www.soinside.com 2019 - 2024. All rights reserved.