duanmiexi2275 2017-09-23 20:47
浏览 70
已采纳

Php使用api密钥获取数据

I want to make a PHP page for sending like with API from social media panel which I have a membership.

They give me this example but I don't understand completely.

Example Code:

<?php
   class Api
   {
      public $api_url = 'http://domain/api/v2'; // API URL

      public $api_key = ''; // Your API key

      public function order($data) { // add order
        $post = array_merge(array('key' => $this->api_key, 'action' => 'add'), $data);
        return json_decode($this->connect($post));
      }

      public function status($order_id) { // get order status
        return json_decode($this->connect(array(
          'key' => $this->api_key,
          'action' => 'status',
          'id' => $order_id
        )));
      }

      public function services() { // get services
        return json_decode($this->connect(array(
          'key' => $this->api_key,
          'action' => 'services',
        )));
      }

      public function balance() { // get balance
        return json_decode($this->connect(array(
          'key' => $this->api_key,
          'action' => 'balance',
        )));
      }


      private function connect($post) {
        $_post = Array();
        if (is_array($post)) {
          foreach ($post as $name => $value) {
            $_post[] = $name.'='.urlencode($value);
          }
        }
        $ch = curl_init($this->api_url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_HEADER, 0);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
        if (is_array($post)) {
          curl_setopt($ch, CURLOPT_POSTFIELDS, join('&', $_post));
        }
        curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)');
        $result = curl_exec($ch);
        if (curl_errno($ch) != 0 && empty($result)) {
          $result = false;
        }
        curl_close($ch);
        return $result;
      }
   }

   // Examples

   $api = new Api();

   $services = $api->services(); # return all services

   $balance = $api->balance(); # return user balance

   // add order

   $order = $api->order(array('service' => 1, 'link' => 'http://example.com/test', 'quantity' => 100)); # Default

   $order = $api->order(array('service' => 1, 'link' => 'http://example.com/test', 'quantity' => 100, 'keywords'=>"test, testing")); # SEO

   $order = $api->order(array('service' => 1, 'link' => 'http://example.com/test', 'comments' => "good pic
great photo
:)
;)")); # Custom Comments

   $order = $api->order(array('service' => 4, 'link' => 'http://example.com/test', 'quantity' => 100, 'usernames'=>"test, testing", 'hashtags'=>"#goodphoto")); # Mentions with Hashtags

   $order = $api->order(array('service' => 5, 'link' => 'http://example.com/test', 'usernames' => "test
example
fb")); # Mentions Custom List

   $order = $api->order(array('service' => 6, 'link' => 'http://example.com/test', 'quantity' => 100, 'hashtag'=>"test")); # Mentions Hashtag

   $order = $api->order(array('service' => 7, 'link' => 'http://example.com/test', 'quantity' => 1000, 'username'=>"test")); # Mentions User Followers

   $order = $api->order(array('service' => 8, 'link' => 'http://example.com/test', 'quantity' => 1000, 'media'=>"http://example.com/p/Ds2kfEr24Dr")); # Mentions Media Likers

   $order = $api->order(array('service' => 9, 'link' => 'http://example.com/test', 'quantity' => 1000, 'usernames'=>"test")); # Mentions

   $order = $api->order(array('service' => 10, 'link' => 'http://example.com/test')); # Package

   $order = $api->order(array('service' => 12, 'link' => 'http://example.com/test', 'quantity' => 100, 'runs' => 10, 'interval' => 60)); # Drip-feed

   $order = $api->order(array('service' => 11, 'username' => 'username', 'min' => 100, 'max' => 110, 'posts' => 0,'delay' => 30)); # Subscriptions

   $status = $api->status($order->order); # return status, charge, remains, start count

?>

For now, I want to see "get balance" on my PHP page but I couldn't do. I fill API URL and API key. Could you help me how can I do?

Thanks

  • 写回答

1条回答 默认 最新

  • dqlhsm9820 2017-09-23 20:51
    关注

    You would use the API by calling balance() on it, like so;

    $api = new Api();
    
    $response = $api->balance();
    
    // The response has "balance" and "currency" properties, which can be accessed like so;
    echo "Your balance is " . $response->balance . " " . $response->currency;
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?
  • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?
  • ¥15 乘性高斯噪声在深度学习网络中的应用
  • ¥15 运筹学排序问题中的在线排序
  • ¥15 关于docker部署flink集成hadoop的yarn,请教个问题 flink启动yarn-session.sh连不上hadoop,这个整了好几天一直不行,求帮忙看一下怎么解决
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛