我正在尝试使用PHP发送带有以下选项的cURL请求:
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,"https://news-api.apple.com/channels/485ae91a-2212-4276-9d07-4f6a9dc82da7/articles");
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS,$data);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$headers = array();
$headers[] = 'POST';
$headers[] = 'Accept: application/json';
$headers[] = 'Content-Type: multipart/form-data';
$headers[] = 'Authorization: HHMAC; key={$key}; signature={$api_signature}; date={$date}';
$headers[] = 'Content-Type: application/json';
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
我的问题是我的变量没有显示为他们的值 - 就像这样:
Authorization: HHMAC; key={$key}; signature={$api_signature}; date={$date}
我该如何解决这个问题?
答案 0 :(得分:3)
将该行更改为使用双引号,PHP变量不会在单引号字符串中展开
$headers[] = "Authorization: HHMAC; key={$key}; signature={$api_signature}; date={$date}";
答案 1 :(得分:1)
使用双引号,因此变量可以扩展,即:
$headers[] = "Authorization: HHMAC; key={$key}; signature={$api_signature}; date={$date}";
注意:
curl_setopt($ch, CURLOPT_POST, 1);
没用,因为当您使用curl_setopt($ch, CURLOPT_POSTFIELDS,$data);