【问题标题】:Warning curl_setopt() expects parameter 2 to be long, string given警告 curl_setopt() 期望参数 2 很长,给定字符串
【发布时间】:2016-01-24 02:40:01
【问题描述】:

我不断收到这个 curl 错误

警告 curl_setopt() 期望参数 2 很长,给定字符串

curl_setopt() 期望参数 2 很长,字符串在 /var/www/mbl.domain.com/html/admin/functions/api/jobs_send.php 中的 706 处给出

代码:

$request = urlencode($xml);
$url    = 'http://integrated.com/1.3';
$query  = 'token=' . $token . '&';
$query .= 'idomain=' . $idomain . '&';
$query .= 'cdomain=' . $cdomain . '&';
$query .= 'request=' . $request;
if(!$ch = curl_init()){
    die("Could not init cURL session.\n");
}
curl_setopt($ch, CURLOPT_ENCODING , "gzip"); // we are making our api call fast by 70%
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDSIZE, sizeof($query));
curl_setopt($ch, CURLOPT_POSTFIELDS, $query);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT ,0); 
curl_setopt($ch, CURLOPT_TIMEOUT, 40); //timeout in seconds
curl_setopt($ch, CURLOPT_SSLVERSION, 3);
$result = curl_exec($ch);
curl_close($ch);
$xmlObj  = simplexml_load_string($result,"SimpleXMLElement", LIBXML_NOCDATA);

第 706 行:

curl_setopt($ch, CURLOPT_POSTFIELDSIZE, sizeof($query));

我该如何解决这个问题?

【问题讨论】:

    标签: php json curl


    【解决方案1】:

    PHP 中没有 CURLOPT_POSTFIELDSIZE 这样的选项。当您指定 CURLOPT_POSTFIELDS 时,它会在内部自动为您设置。

    您收到错误是因为 PHP 看到未定义的常量 CURLOPT_POSTFIELDSIZE,将其转换为字符串 ("CURLOPT_POSTFIELDSIZE"),并将其传递给函数。这会导致错误,因为所有CURLOPT_* 常量实际上都是整数。 (未定义的常量到字符串的转换会引发警告;如果您正在记录警告/错误,它应该会显示在您的日志中。)

    另外:不会导致您的问题,但仍然会出现错误:sizeof($string) === 1sizeofcount 的别名,它为标量返回 1。对于字符串的长度,您想改用strlen($string)

    【讨论】:

    • @Coci same 错误?那么,在您删除 CURLOPT_POSTFIELDSIZE 行之后,它在抱怨哪一行?
    • 当我删除这一行时,它给了我 require_once(/var/www/mbl.domain.com/html/admin/functions/api/jobs_send.php):无法打开流:没有这样的文件或目录
    • 该文件存在吗?该代码甚至不在您的原始问题中。
    猜你喜欢
    • 1970-01-01
    • 2021-03-20
    • 2011-07-12
    • 1970-01-01
    • 1970-01-01
    • 2014-03-27
    • 2015-11-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多