Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/api/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php使用api键获取数据_Php_Api - Fatal编程技术网

Php使用api键获取数据

Php使用api键获取数据,php,api,Php,Api,我想做一个PHP页面发送喜欢的API从社会媒体面板,我有一个成员 他们给了我这个例子,但我不完全理解 示例代码: <?php class Api { public $api_url = 'http://domain/api/v2'; // API URL public $api_key = ''; // Your API key public function order($data) { // add order $po

我想做一个PHP页面发送喜欢的API从社会媒体面板,我有一个成员

他们给了我这个例子,但我不完全理解

示例代码:

<?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\ngreat photo\n:)\n;)")); # 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\nexample\nfb")); # 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

?>

现在,我想在我的PHP页面上看到“获取平衡”,但我做不到。我填写API URL和API密钥。你能帮我做什么吗


谢谢

您可以通过在API上调用
balance()
来使用它,就像这样

$api=newapi();
$response=$api->balance();
//响应具有“余额”和“货币”属性,可以这样访问;
呼应“你的平衡是”$回应->平衡。" " . $响应->货币;

投票否决,因为它过于宽泛,主要是基于意见-您可以对此进行研究,oAuth 2.0也是一个问答,如果您在现有代码中遇到某些特定问题,我们可以帮助您,但我们不是来为您编写的。您链接到的cde(应该包含在实际问题中)只是一个类。由于这基本上是PHP 101,我建议您阅读一些关于PHP和OOP的入门教程,因为您肯定会很快遇到更多的问题。感谢您的回答,但我收到了以下错误“可捕获的致命错误:stdClass类的对象无法在第71行转换为字符串”@Can1-该错误发生在哪个文件中?在您显示的代码的第71行中,我看不到任何“对象到字符串”的转换。我在页面末尾添加了@jack wilsdon代码,这些代码给出了该错误。现在我试着用“print_r($balance)”,而不是“echo$balance”,我得到了这些“stdClass对象([balance]=>0.45912[currency]=>USD)”我怎么能只得到余额部分?@Can1-当然。这些方法返回对象。Do:
$data=$api->balance()要回送它,您还应该检查是否得到了有效的响应:
echo-isset($data->balance)$数据->余额:“无效响应”。添加了解释如何访问返回对象属性的代码。