[英]cURL Passing Parameters - PHP - Laravel
我正在使用此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發送數據嗎?
由於不需要使用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;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.