【问题标题】:How to Integrate 3rd party API in Wordpress如何在 Wordpress 中集成 3rd 方 API
【发布时间】:2016-05-17 23:35:38
【问题描述】:

我正在使用 wordpress,我想将 SMS API 集成到我的 wordpress 网站中。任何人都可以帮助知道在哪里(在哪个文件中)编写集成代码以及集成 SMS API 的代码。

我的 SMS API 网址是: http://www.elitbuzzsms.com/app/smsapi/index.php?key=KEY&campaign=****&routeid=**&type=text&contacts=< NUMBER >&senderid=SMSMSG&msg=< Message Content >

我想将上述 API 集成到我的 wordpress 主题中,以便我可以根据手机号码发送短信并添加所需的消息。

【问题讨论】:

  • 到目前为止你做了什么?
  • 我不知道如何在 wordpress 中使用 API。我是新手。如果您能提供帮助,我将不胜感激

标签: php wordpress api


【解决方案1】:

在 wordpress 中,您可以使用 wp_remote_getwp_remote_post

获取请求示例

$url = 'http://www.elitbuzzsms.com/app/smsapi/index.php?key=KEY&campaign=****&routeid=**&type=text&contacts=< NUMBER >&senderid=SMSMSG&msg=< Message Content >';

$response = wp_remote_get( $url  );
if( is_array($response) ) {
  $header = $response['headers']; // array of http header lines
  $body = $response['body']; // use the content
}

发布请求示例

$response = wp_remote_post( $url, array(
    'method' => 'POST',
    'timeout' => 45,
    'redirection' => 5,
    'httpversion' => '1.0',
    'blocking' => true,
    'headers' => array(),
    'body' => array( 'username' => 'bob', 'password' => '1234xyz' ),
    '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>';
}

【讨论】:

  • 感谢您的帮助,我能知道我应该在 wordpress 中将上述代码写在哪个文件中,如果它在 functions.php 中,我们是否需要使用任何钩子或过滤器?
  • 你可以在functions.php或任何文件中使用它,不需要hook。
猜你喜欢
  • 1970-01-01
  • 2019-07-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-01-02
  • 1970-01-01
  • 2017-01-10
  • 1970-01-01
相关资源
最近更新 更多