Laravel队列在作业分配时出现卷曲错误

时间:2018-10-01 15:20:00

标签: php laravel curl queue

我正在使用bitmex library构建一些交易机器人。当我尝试从测试控制器发出这样的请求时:

$this->api->createOrder(....);

一切正常-api创建订单。

但是,当我尝试使用队列作业(数据库驱动程序)发出此请求时,出现错误:

  

curl_setopt()期望参数1为资源,给定整数

来自队列cUrl实例的任何api请求都返回0而不是资源,并且返回此错误。可能是什么问题?

P.S。库中的卷曲方法:

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;

}

1 个答案:

答案 0 :(得分:0)

您需要使用此方法初始化卷曲

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