我已经在我的服务器上设置了ScRapyd,一切似乎都运行得很好.我可以使用cURL来获取我的蜘蛛列表,比如curl -u super:secret http://111.111.111.111:6800/listspiders.json?project=myproject.我也可以用curl -u super:secret http://111.111.111.111:6800/schedule.json -d project=myproject -d spider=spider1来启动蜘蛛.

现在,我想在PHP中执行此操作.第一个cURL命令运行正常:

<?php
  $username='super';
  $password='secret';
  
  $ch = curl_init();
  
  $url = 'http://111.111.111.111:6800/listspiders.json?project=myproject';
  #$url = "http://111.111.111.111:6800/schedule.json -d project=myproject -d spider=spider1";
  curl_setopt($ch, CURLOPT_URL, $url);
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true);
  curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
  curl_setopt($ch, CURLOPT_USERPWD, "$username:$password");
  
  $response = curl_exec($ch);
  echo 'Type ($response): '.gettype($response). "<br/>";
  echo 'Response: '.$response;

  curl_close($ch);
?>

这将返回预期的响应:{"node_name": "ubuntu-s-1vcpu-512mb-10gb-fra1-01", "status": "ok", "spiders": ["spider1", "spider2"]}.

然而,当我将上面代码片段中的URL更改为$url = "http://111.111.111.111:6800/schedule.json -d project=myproject -d spider=spider1";时,我没有得到any响应.没有错误,什么都没有.gettype($response)的回报是boolean,不管它值多少钱.

同样,在使用终端时,curl命令运行正常,并返回类似{"node_name": "ubuntu-s-1vcpu-512mb-10gb-fra1-01", "status": "ok", "jobid": "6a885343fa8233a738bb9efa11ec2e94"}的结果

任何关于这里正在发生的事情的提示,或者更好的解决问题的方法,都是非常感激的.

推荐答案

在您的PHP代码中,当您将URL更改为包含-d个参数时,您实际上是在try 以一种特定于command-line cURL而不是PHP cURL的方式发送数据.希望以下内容能有所帮助.

<?php
$username = 'super';
$password = 'secret';

$ch = curl_init();

$url = 'http://111.111.111.111:6800/schedule.json';

curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query([
    'project' => 'myproject',
    'spider' => 'spider1'
]));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true);
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
curl_setopt($ch, CURLOPT_USERPWD, "$username:$password");

$response = curl_exec($ch);

if ($response === false) {
    echo 'Curl error: ' . curl_error($ch);
}

echo 'Type ($response): ' . gettype($response) . "<br/>";
echo 'Response: ' . $response;

curl_close($ch);
?>

Php相关问答推荐

如何在Livewire中验证多个 Select 字段

在WooCommerce管理订单页面中显示区域名称

Symfony Api平台:按自定义实体方式排序结果

PHP原始数据读取引发max_input_vars错误

将购买限制在WooCommerce中具有相同产品属性的项目

PHP测试字符串的所有POST值

如何保存基于用户 Select 的复选框 Select 模式

PHP根据特定索引值从中删除子数组

如何从WooCommerce checkout 帐单邮箱字段中删除星号?

如何按类别过滤自定义帖子类型

在WooCommerce存档页面中显示可变产品的库存变化属性

Symfony框架锁定和.env设置

如何解决 Laravel 外键错误?

无法允许元素(img:'src')与symfony html-sanitizer(symfony6.3 php 8.2)

关于Laravel缓存出现的错误

PHP 中使用 JSON 的自定义会话处理程序会因错误而 destruct 会话

在特征中使用类的属性

Shopware 6 AuthControllerDecorator已弃用

如何更改或管理Carbon时区,使其不会来回移动时钟

为什么 ECDSA 384 签名验证在 GO 中失败,但在 PHP 中却没有?