简体   繁体   English

WordPress wp_remote_post()将信息发送到Redmine REST API

[英]WordPress wp_remote_post() sending information to Redmine REST API

I am having a problem using WordPress's wp_remote_post() function with Redmine's REST API. 我在Redmine的REST API上使用WordPress的wp_remote_post()函数时遇到问题。 The main issue is that when I attempt to post, i get a 403 - forbidden return. 主要问题是,当我尝试发布时,我收到403-禁止退货。

This is the code that i'm working with: 这是我正在使用的代码:

$info = array(
    'issue' => array(
        'key' => '59930c6460e8e71ef58b4cc95d852153bf21b510',
        'subject' => 'New bug from ' . get_bloginfo('name') . ' (' . get_bloginfo('wpurl') . ')',
        'project_id' => 10,
        'tracker_id' => 1,
        'description' => $_POST['bug_contents'],
        'custom_fields' => array(
            array(
                'name' => 'Web Server Version',
                'id' => 1,
                'value' => $_SERVER['SERVER_SOFTWARE'],
            ),
            array(
                'name' => 'PHP Version',
                'id' => 2,
                'value' => phpversion(),
            ),
            array(
                'name' => 'MySQL Version',
                'id' => 3,
                'value' => $wpdb->get_var( "SELECT VERSION( ) as `version`" ),
            ),
            array(
                'name' => 'WordPress Version',
                'id' => 4,
                'value' => $wp_version,
            ),
            array(
                'name' => 'WordPress Admin Email',
                'id' => 5,
                'value' => get_bloginfo('admin_email'),
            ),
            array(
                'name' => 'Server IP',
                'id' => 6,
                'value' => $_SERVER['SERVER_ADDR'],
            ),
            array(
                'name' => 'Server External IP',
                'id' => 7,
                'value' => $ip_feedback_array['origin'],
            ),
        ),
    ),
);

$api_call = wp_remote_post(
    'https://ias:1q2w3e$r@rm.14all.me/issues.json',
    array(
        'method' => 'POST',
        'timeout' => 45,
        'redirection' => 5,
        'httpversion' => '1.0',
        'blocking' => true,
        'headers' => array(
            'Content-Type' => 'application/json',
            //'Authorization' => 'Basic ' . base64_encode( 'ias' . ':' . '1q2w3e$r' ),
            'X-Redmine-API-Key' => '59930c6460e8e71ef58b4cc95d852153bf21b510'
            ),
        'cookies' => array(),
        'body' => json_encode($info),
    )
);

However the response that I recieve is: 但是我收到的答复是:

Array
(
    [headers] => Array
        (
            [server] => cloudflare-nginx
            [date] => Wed, 30 Apr 2014 15:07:58 GMT
            [content-type] => application/json; charset=utf-8
            [connection] => close
            [set-cookie] => Array
                (
                    [0] => __cfduid=dd5dfb36ec5e73c40e7eb94ba34ab8ece1398870478439; expires=Mon, 23-Dec-2019 23:50:00 GMT; path=/; domain=.14all.me; HttpOnly
                    [1] => _redmine_session=BAh7BiIPc2Vzc2lvbl9pZCIlOWU4NWRlOWRjMGNiNDczZWI2YzU1NGE2MTlkMTc4NjI%3D--2396b75554bde9fbe8a15870b0e3861687968094; path=/; HttpOnly
                    [2] => autologin=; path=/; expires=Thu, 01-Jan-1970 00:00:00 GMT
                )

            [x-runtime] => 0.048408
            [cache-control] => no-cache
            [x-ua-compatible] => IE=Edge,chrome=1
            [x-rack-cache] => invalidate, pass
            [x-request-id] => 9dcd72c966f6434914e2b65ae706bb30
            [x-powered-by] => Phusion Passenger 4.0.10
            [status] => 403 Forbidden
            [cf-ray] => 1234a6ea3ce9014b-CDG
            [content-encoding] => gzip
        )

    [body] =>  
    [response] => Array
        (
            [code] => 403
            [message] => Forbidden
        )

    [cookies] => Array
        (
            [0] => WP_Http_Cookie Object
                (
                    [name] => __cfduid
                    [value] => dd5dfb36ec5e73c40e7eb94ba34ab8ece1398870478439
                    [expires] => 1577145000
                    [path] => /
                    [domain] => .14all.me
                    [httponly] => 
                )

            [1] => WP_Http_Cookie Object
                (
                    [name] => _redmine_session
                    [value] => BAh7BiIPc2Vzc2lvbl9pZCIlOWU4NWRlOWRjMGNiNDczZWI2YzU1NGE2MTlkMTc4NjI=--2396b75554bde9fbe8a15870b0e3861687968094
                    [expires] => 
                    [path] => /
                    [domain] => rm.14all.me
                    [httponly] => 
                )

            [2] => WP_Http_Cookie Object
                (
                    [name] => autologin
                    [value] => 
                    [expires] => 0
                    [path] => /
                    [domain] => rm.14all.me
                )
        )

    [filename] => 
)

I assume that there's an issue with how I'm sending the authentication parameters. 我假设我发送身份验证参数的方式存在问题。

I managed to solve this issue myself by correcting the following issues: 我自己通过更正以下问题来解决此问题:

  1. Fixing the project ID (which was incorrect) 修复项目ID(不正确)
  2. Using Artax instead of WordPress's built in wp_remote_post() function. 使用Artax代替WordPress的内置wp_remote_post()函数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM