繁体   English   中英

CURLOPT_POSTFIELDS可变格式

[英]CURLOPT_POSTFIELDS varaible format

当我尝试使用以下类型的格式时,我尝试使用cURL发布参数: CURLOPT_POSTFIELDS =>“ label = sample”

我确实在服务器中使用“ sample”作为其值获取了“ label”键,但是当我将其作为变量发送出去时,却在服务器中将其清空了。 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);

我的主要问题是变量的CURLOPT_POSTFIELDS格式!

Doc谈到CURLOPT_POSTFIELDS:

此参数可以作为urlencoded字符串(如“ para1 = val1&para2 = val2&...”)传递,也可以作为字段名称为键且字段数据为值的数组传递。

所以你可以:

  1. CURLOPT_POSTFIELDS => ['label' => $email],替换: CURLOPT_POSTFIELDS => "label=$email",如果您需要更多数据作为POST字段传递,则只需添加另一对$key => $value或在设置curl选项之前对其进行准备。

  2. 通过http_build_query设置POST字段:

curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data)); 其中$ ch是curl句柄,$ data是$ key => $ value对的数组,其中$ key是字段名称。

但请记住:

将数组传递给CURLOPT_POSTFIELDS会将数据编码为multipart / form-data,而传递URL编码的字符串会将数据编码为application / x-www-form-urlencoded。

非常感谢最后我不得不简单地使用Guzzle库

 $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

            ] 

非常非常有用

你可以试试这个

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;
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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