【问题标题】:Alternative way to write cUrl in PHP在 PHP 中编写 cUrl 的另一种方法
【发布时间】:2016-07-28 10:58:10
【问题描述】:

我正在尝试在 hook.io 上创建一个服务以从另一个 API 加载令牌。

public function loadToken()
        {
            $computedHash = base64_encode(hash_hmac ( 'md5' , $this->authServiceUrl , $this->password, true ));
            $authorization = 'Authorization: Bearer '.$this->username.':'.$computedHash;

            $curl = curl_init();
            curl_setopt($curl, CURLOPT_POST, true);
            curl_setopt($curl, CURLOPT_POSTFIELDS, '');
            curl_setopt($curl, CURLOPT_HTTPHEADER, array('Content-Type: application/json' , $authorization ));
            curl_setopt($curl, CURLOPT_URL, $this->authServiceUrl);
            curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
            curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);

            $result = curl_exec($curl);
            $obj = json_decode($result);
            $info = curl_getinfo($curl);
            curl_close($curl);

            if($info['http_code'] != '200')
            {
                // print error from the server
                echo($obj);
                return NULL;
            }

            return $obj;
        }

但事实证明 hook.io 不支持 PHP 中的 cUrl。我知道它可以直接用 php 完成,但我不知道如何。

编辑: 我使用了 file_get_contents 现在它给出了一些其他错误。

$username = '';
$password = '';
$authServiceUrl = '';



$computedHash = base64_encode(hash_hmac ( 'md5' , $authServiceUrl , $password, true ));
$authorization = 'Authorization: Bearer '.$username.':'.$computedHash;

// Create map with request parameters


// Build Http query using params


// Create Http context details
$contextData = array ( 
                'method' => 'POST',
                'header' => "Content-Type: application/json". $authorization ,
                 );

// Create context resource for our request
$context = stream_context_create (array ( 'http' => $contextData ));

// Read page rendered as result of your POST request
$result =  file_get_contents (
                  $authServiceUrl,  // page url
                  false,
                  $context);

我在下面的cmets中添加了错误

【问题讨论】:

标签: php curl hook webhooks hook.io


【解决方案1】:

实际上,据我所知,hook.io 支持 curl。但如果您想尝试其他替代方案,您可以使用file_get_contents。它支持自定义上下文和参数。

$opts = [
    'http' => [
            'method' => 'POST',
            'headers' => ['Authorization' => 'Bearer ' . $this->username . ':' . $computedHash]
    ]
];
$context = stream_context_create($opts);
$file = file_get_contents('http://www.hook.io/example/', false, $context);

【讨论】:

  • PHP 致命错误:调用 /bin/run-hook-php(43) 中未定义的函数 curl_init() : eval()'d code(153) : eval()'d code on line 39
  • 这是我在 hook.io 中点击测试代码时遇到的错误,
  • 我明白了。您的微服务/容器等可能没有 curl 库。您可以使用php_info() 来检查已安装的库。另外,你试过file_get_content吗?
  • PHP 警告:file_get_contents(sandbox-authservice.priaid.ch/login):打开流失败:HTTP 请求失败! /bin/run-hook-php(43) 中需要 HTTP/1.1 411 长度:eval()'d code(107):eval()'d code on line 29
猜你喜欢
  • 2012-01-23
  • 2021-02-13
  • 1970-01-01
  • 2023-04-03
  • 1970-01-01
  • 1970-01-01
  • 2017-04-22
  • 1970-01-01
  • 2014-04-25
相关资源
最近更新 更多