我使用curl从website1向website2提交一些数据。
当我通过接收端提交数据时,我得到它
Array
(
[ip] => 112.196.17.54
[amp;email] => test@test.com
[amp;user] => test123,
[amp;type] => point
[amp;password] => password
)
据我说http_build_query()产生了错误的结果。
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 函数产生错误休息项目已完成并按预期工作。 请帮帮我。
答案 0 :(得分:6)
我想你可以试试:
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);