Laravel queue get curl error when job dispatching

牧云@^-^@ 提交于 2019-12-08 06:17:16

问题


I'm using bitmex library to build some trading bot. When i try to make request like this (from test controller):

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

Everything works fine - api creates an order.

But, when i try to make this request with queue job(database driver) i got error:

curl_setopt() expects parameter 1 to be resource, integer given

Any api request from queue cUrl instance return 0 instead resource and this error. What could be the problem?

P.S. Curl method from library:

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:


You need to initialize the curl by using this method

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


来源:https://stackoverflow.com/questions/52594197/laravel-queue-get-curl-error-when-job-dispatching

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!