0

I'm trying to post a parameter with cURL, when I tried with this type of format : CURLOPT_POSTFIELDS => "label=sample"

I exactly got "label" key in the server with "sample" as its value but I get it empty in the server when I sent it out as a variable . CURLOPT_POSTFIELDS => "label=$email"

 $curl = curl_init();
        $user_info=$this->web_model->retriveUserInfo();
        $email=$user_info->email;
  curl_setopt_array($curl, array(
  CURLOPT_URL => "https://test.bitgo.com/api/v2/".$coin."/wallet/".$wallet_id."/address",
  CURLOPT_RETURNTRANSFER => true,
  CURLOPT_ENCODING => "",
  CURLOPT_MAXREDIRS => 10,
  CURLOPT_TIMEOUT => 30,
  CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
  CURLOPT_CUSTOMREQUEST => "POST",
  CURLOPT_POSTFIELDS => "label=$email",
  CURLOPT_HTTPHEADER => array(
    "Accept: */*",
    "Accept-Encoding: gzip, deflate",
    "Authorization: Bearer v2x4e7cf3fb7e6c2e87bf8103e49756b3892b2e350d6cdbaeb65757980",
    "Cache-Control: no-cache",
    "Connection: keep-alive",
    "Content-Length: 11",
    "Content-Type: application/x-www-form-urlencoded",
    "Host: test.bitgo.com",
    "Postman-Token: b3f2ee7c-9a19-479b-bfe2-27000c90e3c7,d611bde9-3eb1-4e2b-b8f5-a7a5f5485726",
    "User-Agent: PostmanRuntime/7.15.2",
    "cache-control: no-cache"
  ),
));

$response = curl_exec($curl);
$response = json_decode($response, true);
$err = curl_error($curl);

curl_close($curl);

my main problem is CURLOPT_POSTFIELDS format for variables !

3 Answers3

1

Doc says about CURLOPT_POSTFIELDS:

This parameter can either be passed as a urlencoded string like 'para1=val1&para2=val2&...' or as an array with the field name as key and field data as value.

So you can:

  1. Replace: CURLOPT_POSTFIELDS => "label=$email", with CURLOPT_POSTFIELDS => ['label' => $email], and if you'll need more data to pass as POST field you can just add another pair $key => $value to that array or prepare it before setting curl options.

  2. Set POST fields via http_build_query:

curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data)); where $ch is curl handle and $data is array of $key => $value pairs where $key is field name.

But keep in mind:

Passing an array to CURLOPT_POSTFIELDS will encode the data as multipart/form-data, while passing a URL-encoded string will encode the data as application/x-www-form-urlencoded.

neolodor
  • 75
  • 1
  • 7
0

Many thanks Finally I had to use Guzzle library simply

 $client = new  GuzzleHttp\Client(['base_uri' => 'https://test.bitgo.com/api/v2/']);
            $response = $client->post($coin.'/wallet/'.$wallet_id.'/address', [
            'headers' => [
                'Authorization' => 'Bearer v2x4e7cf3fb7e6c2e87bf8103e4975dsddbaeb65ba017b555757980'],
            'form_params' => [

                "label" => $email

            ] 

very very useful

  • Guzzle's very handy, but giving `CURLOPT_POSTFIELDS` an array like you're doing to Guzzle would've solved it with raw cURL as well. – ceejayoz Aug 29 '19 at 13:11
-1

You can try this

function curl($post = array(), $url, $token = '', $method = "POST", $json = false, $ssl = false){
    $ch = curl_init();  
    curl_setopt($ch, CURLOPT_URL, $url);    
    curl_setopt($ch, CURLOPT_HEADER, false);
    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method);
    curl_setopt($ch,CURLOPT_HTTP_VERSION,CURL_HTTP_VERSION_1_1);
    if($method == 'POST'){
        curl_setopt($ch, CURLOPT_POST, true);
    }
    if($json == true){
        curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($post));
        curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json', 'Authorization: Bearer '.$token, 'Content-Length: ' . strlen(json_encode($post))));
    }else{
        curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($post));
        curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/x-www-form-urlencoded'));
    }       
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_SSLVERSION, 6);
    curl_setopt($ch, CURLOPT_ENCODING, 'gzip');
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, false);
    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);
    curl_setopt($ch, CURLOPT_TIMEOUT, 60);
    // Proxy example only
    if (!empty($proxy)) {
        curl_setopt($ch, CURLOPT_PROXY, '127.0.0.1:888');
        if (!empty($proxyAuth)) {
            curl_setopt($ch, CURLOPT_PROXYUSERPWD, 'user:password');
        }
    }
    if($ssl == false){
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    }
    $response = curl_exec($ch); 
    $statusCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
    $this->statusCode = $statusCode;

    if (curl_error($ch)) {  
        $error = 'CURL_ERROR '.$statusCode.' - '.curl_error($ch);
        // print_r('CURL_ERROR '.$statusCode.' - '.curl_error($ch));                    
        throw new Exception('CURL_ERROR '.curl_error($ch), $statusCode);
    }
    curl_close($ch);
    return $response;
}
Max
  • 1