访问第三方API-区分这一点和发布API的术语?

时间:2016-09-05 作者:Highly Irregular

What terminology can I use to help me different between using Wordpress to access a 3rd party API (what I want to do) and building an API for a 3rd party to use (not what I want to do)?

说明:

我有兴趣编写一些自定义代码来访问第三方API(定制产品),并使用结果在我的Wordpress站点中添加或更新一些自定义帖子数据。

我在网上搜索了有关这方面的信息,并取得了一些成功,但我想知道是否因为我不懂术语而遗漏了很多信息。我得到的大多数搜索结果都与在Wordpress中创建API层以供第三方软件连接有关,但这不是我感兴趣的。

我知道Wordpress HTTP API documentation 这似乎表明wp\\u remote\\u*函数将非常有用,但如果我能更容易地找到一些示例代码和一些有用的插件,它仍然会非常有用。

1 个回复
最合适的回答,由SO网友:rudtek 整理而成

Wp\\U remote的路径正确。这是我为向第三方注册新闻稿而编写的插件的一部分。基本上,您是在利用该公司的api,而不是使用wordpress。主要问题是,许多公司的api都不相似,因此在不了解更多与您交互的公司的情况下,您必须进行一些测试,或者看看是否可以从中获得公开的结果。

function html_form_code() {  // THIS IS THE FORM TO SEND COLLECT USER DATA TO SEND TO 3rd party.
    echo \'<form action="\' . esc_url( $_SERVER[\'REQUEST_URI\'] ) . \'" method="post">\';
    echo \'<p>\';
    echo \'Your Name (required) <br />\';
    echo \'<input type="text" name="cf-name" pattern="[a-zA-Z0-9 ]+" value="\' . ( isset( $_POST["cf-name"] ) ? esc_attr( $_POST["cf-name"] ) : \'\' ) . \'" size="40" />\';
    echo \'</p>\';
    echo \'<p>\';
    echo \'Your Email (required) <br />\';
    echo \'<input type="email" name="cf-email" value="\' . ( isset( $_POST["cf-email"] ) ? esc_attr( $_POST["cf-email"] ) : \'\' ) . \'" size="40" />\';
    echo \'</p>\';
    echo \'<p><input type="submit" name="cf-submitted" value="Send"/></p>\';
    echo \'</form>\';
}

function newsletter_signup() {  //THIS IS THE API INTERACTION.

    // if the submit button is clicked, send the POST
    if ( isset( $_POST[\'cf-submitted\'] ) ) {

        // sanitize form values
        $name    = sanitize_text_field( $_POST["cf-name"] );
        $email   = sanitize_email( $_POST["cf-email"] );


$url=\'YOUR API URL WILL GO HERE\'; //THIS is the actual interaction so this is where you are going to have to do some testing.
$response = wp_remote_post( $url, array(
    \'method\' => \'POST\',
    \'timeout\' => 45,
    \'redirection\' => 5,
    \'httpversion\' => \'1.0\',
    \'blocking\' => true,
    \'headers\' => array(\'Authorization\' => \'Basic \' . base64_encode(MY REMOVED PW)),
    \'body\' => array(    ),  // YOU\'ll NEED TO ADD YOUR CONTENT YOU\'RE sending here... //$name and $email in my example but formatting will be up to API
    \'cookies\' => array()
    )
);

if ( is_wp_error( $response ) ) {
   $error_message = $response->get_error_message();
   echo "Something went wrong: $error_message";
} else {
 //  echo \'Response:<pre>\';
 //  print_r( $response );
 //    echo \'</pre>\'; 
$responseBody = json_decode($response[\'body\'],true);
echo $responseBody[\'message\'];

    }
    }
}

function cf_shortcode() {  //FUNCTION to combine and submit the data in a fell swoop
    ob_start();
    newsletter_signup();
    html_form_code();
    return ob_get_clean();
}

add_shortcode( \'newsletter_contact_form\', \'cf_shortcode\' ); //SHORTCODE TO PRESENT ON FRONTEND.