I cannot send parameter with Curl

Asked

Viewed 51 times

1

I’m trying to make a POST request so I can register a product using Curl but when I run it returns me an empty string

$param_POST = array(
    "IdProduct"=> "000001",
    "Name"=> "Caixa de Som",
    "Code"=> "125241",
    "Brand"=> "JBL",
    "NbmOrigin"=> "11111",
    "NbmNumber"=> "",
    "WarrantyTime"=> "0",
    "Active"=> true,
    "Categories"=> array(
        "Id"=> "1",
        "Name"=> "teste",
        "ParentId"=> ""
    ),
    "Attributes"=> array(
        "Name"=> "Protecao",
        "Value"=> "resistente a agua"
    )
);

$GetMarketplace = Integra_POST('/api/Product',$param_POST);
var_dump($GetMarketplace);

This is the array I send to my function with endpoint and array.

function Integra_POST($endpoint, $params = array()){
    //Execução de POST
    $url = $_SESSION['url'];
    $url .= $endpoint;

    $data = json_encode($params);

    $ch_opts = integra_GetCurlOpts();
    $ch_opts[CURLOPT_CUSTOMREQUEST] = "POST";
    $ch_opts[CURLOPT_POSTFIELDS]    = $data;
    $ch_opts[CURLOPT_HTTPHEADER]    = array(
        'Content-Type: application/json',
        'Content-Length: ' . strlen($data)
    );

    echo $data;

    $ch = curl_init($url);
    curl_setopt_array($ch, $ch_opts);
    $result = curl_exec($ch);

    return $result;
}

1 answer

0

I made some modifications in sending the data by CURL and it worked perfectly on my local server. See below the changes:

<?php
...

$GetMarketplace = Integra_POST('/api/Product',$param_POST);
var_dump($GetMarketplace);

function Integra_POST($endpoint, $params = array()){

  $url = $_SESSION['url'];
  $url .= $endpoint;

  $curl = curl_init();

  curl_setopt_array($curl, array(
    CURLOPT_URL => $url,
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_ENCODING => "",
    CURLOPT_MAXREDIRS => 10,
    CURLOPT_TIMEOUT => 0,
    CURLOPT_FOLLOWLOCATION => true,
    CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
    CURLOPT_CUSTOMREQUEST => "POST",
    CURLOPT_POSTFIELDS => http_build_query($params),
    CURLOPT_HTTPHEADER => array(
      "Content-Type: application/x-www-form-urlencoded"
    ),
  ));

  $response = curl_exec($curl);

  curl_close($curl);

  return $response;
}
  • my Curl already has these parameters but on another page php Porq I’m not only using a function so the main I left separate, still I tried to put in the same file and it still didn’t work

Browser other questions tagged

You are not signed in. Login or sign up in order to post.