我正在使用此cURL函数发送一组必须由api接收的数据。然而,api收到null。
public static function httpPost($url,$type,$params)
{
$postData = '';
foreach ($params as $k => $v) {
$postData .= $k . '=' . $v . '&';
}
$postData = rtrim($postData, '&');
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SAFE_UPLOAD, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postData);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $type);
$output = curl_exec($ch);
curl_close($ch);
return $output;
}
我尝试在发送之前输出数据并显示正确的数据。另一个卷曲部分有效。知道为什么数据不是通过curl发送的吗?
答案 0 :(得分:0)
由于您不需要使用POST,请取消设置curl选项并使用PHP http_builder构建URL
public static function httpPost($url,$type,$params)
{
// Clean URL + append "?" at the end of URL + append the query
$url = rtrim($url,"?") . "?" . http_build_query($params);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SAFE_UPLOAD, true);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $type);
$output = curl_exec($ch);
curl_close($ch);
return $output;
}