首页 > 解决方案 > 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;

}

标签: phplaravelcurlqueue

解决方案


您需要使用此方法初始化 curl

$this->ch = curl_init();
curl_setopt($this->ch, CURLOPT_URL, $url);

推荐阅读