当我使用OpenTok API归档视频时,php file_get_contents总是返回bool(false)

时间:2015-10-01 16:17:31

标签: php webrtc file-get-contents opentok

我正在使用OpenTok API进行音频/视频聊天,现在尝试使用相同的方式录制音频/视频聊天会议。

我已从this link

下载了API

但是结果总是为空(通过 var_dump($ res); 检查结果)并且响应变为空数组。

以下是我的功能:

protected function request($method, $url, $opts = null) {
    $url = $this->endpoint . $url;
    if(($method == 'PUT' || $method == 'POST') && $opts) {
      $bodyFormat = $opts->contentType();
      $dataString = $opts->dataString();
    }

    $authString = "X-TB-PARTNER-AUTH: $this->apiKey:$this->apiSecret";

    if (function_exists("file_get_contents")) {

      $http = array(
        'method' => $method
      );

      $headers = array($authString);

      if($method == "POST" || $method == "PUT") {
        $headers[1] = "Content-type: " . $bodyFormat;
        $headers[2] = "Content-Length: " . strlen($dataString);
        $http["content"] = $dataString;
      }
      $http["header"] = $headers;
      $context_source = array ('http' =>$http);
      $context = stream_context_create($context_source);
      $res = file_get_contents( $url ,true, $context);
      var_dump($res); 
      $statusarr = explode(" ", $http_response_header[0]);

      $status = $statusarr[1];
      $headers = array();

      foreach($http_response_header as $header) {
        if(strpos($header, "HTTP/") !== 0) {
          $split = strpos($header, ":");
          $key = strtolower(substr($header, 0, $split));
          $val = trim(substr($header, $split + 1));
          $headers[$key] = $val;
        }
      }

      $response = (object)array(
        "status" => $status
      );

      if(strtolower($headers["content-type"]) == "application/json") {
        $response->body = json_decode($res);
      } else {
        $response->body = $res;
      }

    } else{
      throw new OpenTokArchivingRequestException("Your PHP installion doesn't support file_get_contents. Please enable it so that you can make API calls.");
    }

    return $response;
  }

当我打印 $ context_source 时,我会得到以下数组:

Array
(
    [http] => Array
        (
            [method] => POST
            [content] => {"action":"start","sessionId":"1_MX40NTM2MDgxMn4xMjcuMC4wLjF-MTQ0MzcxMDQ0NzU2NH4wWkZ3bkN1NDJaYlNFMFZFZmYwcGZ1a2F-UH4","name":"filename"}
            [header] => Array
                (
                    [0] => X-TB-PARTNER-AUTH: 45360812:cbbd8b29be4c75d5aab1945a71bf0cb3443e3939
                    [1] => Content-type: application/json
                    [2] => Content-Length: 136
                )

        )

)

一切似乎都很好。谁能告诉我我做错了什么。

0 个答案:

没有答案