【问题标题】:PHP Array Element Not Getting Put In String (Error)PHP 数组元素未放入字符串(错误)
【发布时间】:2012-11-19 23:12:04
【问题描述】:

下面的代码没有像我预期的那样工作......

curl_setopt($ch, CURLOPT_POSTFIELDS, "password="+$alphas[$x]+"&submit=yes");

将字母放入字符串中,$alphas[$x] 部分似乎不起作用。下面几行我回显 $alphas[$x] 并且效果很好。

例如,如果我将第一行代码更改为...

curl_setopt($ch, CURLOPT_POSTFIELDS, "password=j&submit=yes");

它按预期完美运行,因此我认为 $alpahs[$x] 没有像应有的那样将字母放入字符串中。

$content = "7";
$x = 0;
$alphas = array_merge(range("A", "Z"), range("a", "z"));

while($x < 52) {
print_r($alphas[$x]);
$ch = curl_init();
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch, CURLOPT_URL, "/Demo/form.php");
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, "password="+$alphas[$x]+"&submit=yes");
$content=curl_exec($ch);

echo $content;
echo "Pass: ";
echo $alphas[$x];
echo "<br>";
$x++;

}

【问题讨论】:

    标签: php forms post curl


    【解决方案1】:

    句号是字符串连接运算符,不是加号:

    curl_setopt($ch, CURLOPT_POSTFIELDS, "password=".$alphas[$x]."&submit=yes");
    

    【讨论】:

      【解决方案2】:

      您还可以使用以下语法将变量插入双引号字符串:

      curl_setopt($ch, CURLOPT_POSTFIELDS, "password={$alphas[$x]}&submit=yes");
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-04-27
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-05-28
        相关资源
        最近更新 更多