简体   繁体   中英

How can send params array in curl get request

I have an array of params which I'm sending in CURL get request but not getting params which I have checked. I want to send these params(key, value) in curl get request but not sending giving empty data

this is my code you can see that

$params = [];

foreach($product_data->params as $param) {

    array_push($params, $param);
}

$curl = curl_init($product_data_url);

$headers = [
    'Content-Type: application/json',
    'Authorization: Bearer ' . $token,
];

curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);

curl_setopt($curl, CURLOPT_URL, $product_datat_url);

curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "GET");   
curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($params)); 
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);


$resp = curl_exec($curl);
if($resp === false)
{
    echo "Error Number:".curl_errno($curl)."<br>";
    echo "Error String:".curl_error($curl);
}
curl_close($curl);

return $resp;

First you have a typo in this line:

curl_setopt($curl, CURLOPT_URL, $product_datat_url);

$product_datat_url should be $product_data_url .

Second, another problem that you are trying to send a post request parameters in get request, As known the get request parameters appears in the url like: url?param1=value&param2=value so you should add the following before setting the url:

$url = $product_data_url . '?' . http_build_query($params);
curl_setopt($curl, CURLOPT_URL, $url);

And remove this line:

curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($params));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM