php - Laravel 队列在作业调度时出现 curl 错误
问题描述
我正在使用bitmex 库来构建一些交易机器人。当我尝试发出这样的请求时(来自测试控制器):
$this->api->createOrder(....);
一切正常 - api 创建一个订单。
但是,当我尝试使用队列作业(数据库驱动程序)发出此请求时,出现错误:
curl_setopt() 期望参数 1 是资源,给定整数
来自队列 cUrl 实例的任何 api 请求都返回 0 而不是资源和此错误。可能是什么问题呢?
库中的 PS Curl 方法:
private function authQuery($data) {
$method = $data['method'];
$function = $data['function'];
if($method == "GET" || $method == "POST" || $method == "PUT") {
$params = http_build_query($data['params']);
}
elseif($method == "DELETE") {
$params = json_encode($data['params']);
}
$path = self::API_PATH . $function;
$url = self::API_URL . self::API_PATH . $function;
if($method == "GET" && count($data['params']) >= 1) {
$url .= "?" . $params;
$path .= "?" . $params;
}
$nonce = $this->generateNonce();
if($method == "GET") {
$post = "";
}
else {
$post = $params;
}
$sign = hash_hmac('sha256', $method.$path.$nonce.$post, $this->apiSecret);
$headers = array();
$headers[] = "api-signature: $sign";
$headers[] = "api-key: {$this->apiKey}";
$headers[] = "api-nonce: $nonce";
$headers[] = 'Connection: Keep-Alive';
$headers[] = 'Keep-Alive: 90';
curl_reset($this->ch);
curl_setopt($this->ch, CURLOPT_URL, $url);
if($data['method'] == "POST") {
curl_setopt($this->ch, CURLOPT_POST, true);
curl_setopt($this->ch, CURLOPT_POSTFIELDS, $post);
}
if($data['method'] == "DELETE") {
curl_setopt($this->ch, CURLOPT_CUSTOMREQUEST, "DELETE");
curl_setopt($this->ch, CURLOPT_POSTFIELDS, $post);
$headers[] = 'X-HTTP-Method-Override: DELETE';
}
if($data['method'] == "PUT") {
curl_setopt($this->ch, CURLOPT_CUSTOMREQUEST, "PUT");
//curl_setopt($this->ch, CURLOPT_PUT, true);
curl_setopt($this->ch, CURLOPT_POSTFIELDS, $post);
$headers[] = 'X-HTTP-Method-Override: PUT';
}
curl_setopt($this->ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($this->ch, CURLOPT_SSL_VERIFYPEER , false);
curl_setopt($this->ch, CURLOPT_RETURNTRANSFER, true);
$return = curl_exec($this->ch);
if(!$return) {
$this->curlError();
$this->error = true;
return false;
}
$return = json_decode($return,true);
if(isset($return['error'])) {
$this->platformError($return);
$this->error = true;
return false;
}
$this->error = false;
$this->errorCode = false;
$this->errorMessage = false;
return $return;
}
解决方案
您需要使用此方法初始化 curl
$this->ch = curl_init();
curl_setopt($this->ch, CURLOPT_URL, $url);
推荐阅读
- r - 从具有因子的列到具有 0、1 的两个不同列
- excel - 数据验证(长度限制和字符必须为大写)
- laravel - 获取语句中单词的第一个字母
- angular - Angular 自定义 ControlValueAccessor 无法处理数组
- fastapi - 如何在graphql fastapi中进行依赖注入时获取graphiql ide?
- amazon-web-services - AWS ElasticBeanstalk - 自动扩展的实例看不到环境变量
- python-3.x - 用于 API 调用的 Locust 负载测试
- c# - VB.NET 通过 Excel.App Run 将参数作为 ByRef 传递在使用 Excel.Application 时不会更新变量,但在使用 Object 时有效
- php - 如何在 Windows 服务器上自动运行 PHP 文件?
- python - 安全停止机器人的方法