我正在使用curl将一些数据从网站1提交到网站2。
当我通过然后在接收端提交数据时,我会像一样得到它
Array
(
[ip] => 112.196.17.54
[amp;email] => test@test.com
[amp;user] => test123,
[amp;type] => point
[amp;password] => password
)
据我说http_build_query()产生了错误的结果。
"ip";字段是正确的其余部分是不正确的
请告诉我为什么会这样
curl函数如下:http_build_query($config)
function registerOnPoints($username ,$password,$email,$ip , $time )
{
$ch = curl_init("http://website2c.com/curl-handler");
curl_setopt(
$ch, CURLOPT_RETURNTRANSFER, 1);
$config = array( 'ip' => $ip,
'user' => $username,
'email' => $email,
'password'=> $password,
'time' => $time,
'type' => 'point') ;
# add curl post data
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($config));
curl_setopt($ch, CURLOPT_POST, true);
# execute
$response = curl_exec($ch);
# retreive status code
$http_status = curl_getinfo($ch , CURLINFO_HTTP_CODE);
if($http_status == '200')
{
$response = json_decode($response);
} else {
echo $http_status;
}
// Close handle
curl_close($ch);
}
如果是php版本问题,那么,很明显,我无权更改php版本,因为只有curl函数产生错误。其余项目已完成并按预期工作。请帮帮我。
我想你可以试试:
http_build_query($config, '', '&');
或替代方案:
$paramsArr = array();
foreach($config as $param => $value) {
$paramsArr[] = "$param=$value";
}
$joined = implode('&', $paramsArr);
//and use
curl_setopt($ch, CURLOPT_POSTFIELDS, $joined);