【问题标题】:Laravel queue get curl error when job dispatchingLaravel 队列在作业调度时出现 curl 错误
【发布时间】:2018-10-01 15:19:59
【问题描述】:

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

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

一切正常 - api 创建订单。

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

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

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

附:库中的 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 方法吗
  • 我的第一条消息更新了,这个库的完整代码:github.com/y0un1verse/bitmex-api-php/blob/master/BitMex.php
  • 我有同样的问题,不知道如何解决。问题出在我正在使用的第 3 方库中。这仅在从队列中提取作业时发生。使用工匠 tinker shell 可以正常工作,这使得这更加奇怪。
  • 您找到解决方案了吗?将 FaviconDownloader 与 Laravel 一起使用时,我遇到了同样的问题。我已经按照@gurpal-singh 的建议尝试了替代 curl_init() ,但这并没有解决问题。

标签: php laravel curl queue


【解决方案1】:

你需要使用这个方法来初始化curl

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

【讨论】:

  • 您阅读我的第一条消息了吗?通过我发送的链接查看库源。卷曲初始化在单独的方法中。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-08-06
  • 1970-01-01
  • 2020-03-14
  • 2021-09-16
  • 1970-01-01
  • 1970-01-01
  • 2021-03-07
相关资源
最近更新 更多