【问题标题】:mashape api Class 'Unirest' not found error in php codemashape api Class 'Unirest' not found php 代码中的错误
【发布时间】:2014-05-01 15:35:01
【问题描述】:

我是从 下载mashape api的php源代码 https://github.com/composer/composer 网址 我通过 git 命令安装 composer $ curl -sS https://getcomposer.org/installer | php

我包括

{
  "require" : {
    "mashape/unirest-php" : "dev-master"
  },
  "autoload": {
    "psr-0": {"Unirest": "lib/"}
  }
}

composer.json 中的这行代码 我在根目录上创建一个名为 test.php 的新文件并包含

require_once '/test/Unirest.php';
$response = Unirest::post("http://httpbin.org/post", array( "Accept" => "application/json" ),
  array(
    "parameter" => 23,
    "foo" => "bar"
  )
);

$response->code; // HTTP Status code
$response->headers; // Headers
$response->body; // Parsed body
$response->raw_body; // Unparsed body

经过以上所有过程 我运行 php 源代码然后显示类“unirest”未找到 有什么办法可以解决吗?

【问题讨论】:

    标签: php


    【解决方案1】:

    感谢您使用 Unirest,它对开发人员来说是一个很棒的工具,我每天都使用 Unirest,所以我可以帮助您:

    首先,您没有在 composer 中正确包含/需要库,您需要在 composer 本身生成的供应商文件夹中使用 autoload.php

    其次,您将返回一个数组,例如要显示 raw_body,您必须使用 print_r();

    希望这会有所帮助, 最好的! Mashape 警长:D

    工作+相关代码:

    <?php
    require 'vendor/autoload.php';
    $response = Unirest::post("http://httpbin.org/post", array( "Accept" => "application/json" ), array(
       "parameter" => 23,
       "foo" => "bar"
      )
    );
    print_r($response->raw_body);
    ?>
    

    【讨论】:

      猜你喜欢
      • 2014-07-26
      • 1970-01-01
      • 2013-11-02
      • 2011-11-06
      • 1970-01-01
      • 1970-01-01
      • 2015-02-12
      • 2023-04-03
      • 2013-10-05
      相关资源
      最近更新 更多