【问题标题】:cURL not POSTing w/ PHP variablecURL 不使用 PHP 变量发布
【发布时间】:2022-06-17 23:01:15
【问题描述】:

所以我有以下cURL 实际发布的请求(有效):

$curl = curl_init();
curl_setopt_array($curl, array(
    CURLOPT_URL => 'https://pi.pardot.com/api/v5/objects/prospects?fields=email,campaignId',
    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 =>'{
        "email":"Test@test.com",
        "campaignId":3855
    }',
    CURLOPT_HTTPHEADER => array(
        "Authorization: Bearer 00D4x000006qMhY!ARIAQBMDpjRs__cOrNj",
        "Pardot-Business-Unit-Id: 0Uv4x000000GmjQCAS",
        "Content-Type: application/json",
    ),
));
$response = curl_exec($curl);
curl_close($curl);

return $response;

现在,如果我使用以下 cURL 请求传入作为字符串输出的 $access_token 变量,它将不起作用:

$access_token = $this->get_oauth_token();
$curl = curl_init();
curl_setopt_array($curl, array(
    CURLOPT_URL => 'https://pi.pardot.com/api/v5/objects/prospects?fields=email,campaignId',
    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 =>'{
        "email":"Test@test.com",
        "campaignId":3855
    }',
    CURLOPT_HTTPHEADER => array(
        "Authorization: Bearer {$access_token}",
        "Pardot-Business-Unit-Id: 0Uv4x000000GmjQCAS",
        "Content-Type: application/json",
    ),
));
$response = curl_exec($curl);
curl_close($curl);

return $response;

有谁知道有什么区别吗?

【问题讨论】:

  • 显然,$this->get_oauth_token();不返回正确的令牌。还有什么可能?
  • 另外,“它不起作用”到底是什么意思?
  • 它确实返回了一个有效的令牌,但 POST 只是没有执行 - 无需投票,POST 请求永远不会参与。我在问内部 PHP 变量是否必须以任何特定方式格式化?

标签: php curl


猜你喜欢
  • 1970-01-01
  • 2011-12-19
  • 2020-12-09
  • 2015-07-29
  • 1970-01-01
  • 2011-05-04
  • 1970-01-01
  • 2012-03-20
相关资源
最近更新 更多