Php 创建一个带有for循环的字符串,然后使用cURL进行post

Php 创建一个带有for循环的字符串,然后使用cURL进行post,php,curl,Php,Curl,请帮助我,如果这是一个正确的方法,我如何使它工作,如果它不是你会建议张贴参数 $str = ''; for( $i = 11; $i <= 20; $i++ ) { $str .= $i . ' '; } $ch = curl_init(); //http post to another server curl_setopt($ch, CURLOPT_URL,"http://xxxx"); curl_setopt($ch, CURLOPT_POST, 1); curl_setop

请帮助我,如果这是一个正确的方法,我如何使它工作,如果它不是你会建议张贴参数

$str = '';
for( $i = 11; $i <= 20; $i++ )
{
 $str .= $i . ' ';    
}
$ch = curl_init(); //http post to another server
curl_setopt($ch, CURLOPT_URL,"http://xxxx");
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS,"username=$username&password=$password&string=$str"); 

// receive server response ...
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$server_output = curl_exec ($ch);
print_r($server_output);
curl_close ($ch);
$str='';

对于($i=11;$i如果您可以访问使用curl调用的脚本,请尝试添加:

var_dump($_POST);
看看打印的是什么

我只是让你的代码可读性更好一点。 但它是正确的。应该有效

试着查看一下php_错误日志文件,看看它是否触发了什么

<?php

$str = '';
for( $i = 11; $i <= 20; $i++ ) {
    $str .= $i . ' ';
}

$ch = curl_init(); //http post to another server
curl_setopt($ch, CURLOPT_URL           , 'http://xxxx');
curl_setopt($ch, CURLOPT_POST          , 1);
curl_setopt($ch, CURLOPT_POSTFIELDS    , 'username=' . $username . '&password=' . $password . '&string=' . $str);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

$server_output = curl_exec ($ch);

print_r($server_output);

curl_close($ch);

我知道您的代码是正确的,但您可以使用以下函数作为帮助程序:

<?php
$url  = "http://xxxx";
$str  = implode(" ", range(11,20));
$data = array("username" => $username, "password" => $password, "string" => $str);

$server_output = processURL($url, $data);
print_r($server_output);

function processURL($url, $data = array()){ 
    $ch=curl_init(); 
    curl_setopt($ch, CURLOPT_URL, $url); 
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1); 
    curl_setopt($ch, CURLOPT_POSTFIELDS, $data); 
    $response = curl_exec ($ch); 
    curl_close ($ch); 
    return $response; 
}

问题出在哪里?您遇到了什么错误?您期望得到什么?可能空格是个问题?请先在字符串中创建
CURL\u POSTFIELDS
值,然后使用
CURL\u escape
对其进行编码-空格将是@putvande建议的问题。此外,关于您实际遇到的问题的提示也会很有用我真的没有收到任何错误,但我没有收到http的任何响应。我想知道这段代码是否适合我想要的用途。您可能想在循环后的
$str
上使用
urlencode()
。google.com
什么时候开始接受用户名和密码的?