我想使用下面的命令:
curl -X POST 'https://graphcdn.frankenergie.nl/' -H 'Content-Type: application/json' -H 'User-Agent: Integromat/production' -d '{
"query": "query MarketPrices {ntmarketPricesElectricity(startDate: "2023-01-12", endDate: "2023-01-15") {n tilln fromn marketPricen priceIncludingMarkupnt}ntmarketPricesGas(startDate: "2023-01-11", endDate: "2023-01-11") {n fromn tilln marketPricen priceIncludingMarkupn }n}"
}'
当我在命令行中执行此命令时,它工作正常(result
)现在我尝试在PHP脚本中使用相同的命令,但我得到错误:PHP解析错误:语法错误,意外标识符"curl_setopt"在/home/scripts/test.php第11行
我尝试的脚本是:
<?php
// API URL
$url = 'https://graphcdn.frankenergie.nl/';
// Create a new cURL resource
$ch = curl_init($url);
$payload = '{"query": "query MarketPrices {ntmarketPricesElectricity(startDate: "2023-01-12", endDate: "2023-01-15") {n tilln fromn marketPricen priceIncludingMarkupnt}ntmarketPricesGas(startDate: "2022-06-09", endDate: "2022-06-10") {n fromn tilln marketPricen priceIncludingMarkupn }n}"}'
// Attach encoded JSON string to the POST fields
curl_setopt($ch, CURLOPT_POSTFIELDS, $payload);
// Set the content type to application/json
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json', 'User-Agent: Integromat/production'));
// Return response instead of outputting
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
// Execute the POST request
$result = curl_exec($ch);
// Close cURL resource
curl_close($ch);
?>
我们所有人都遇到过这种情况,但是这个错误表明你在有效负载的末尾缺少一个分号。
我写过的最短的回答:
改变:
$payload = '{"query": "query MarketPrices {ntmarketPricesElectricity(startDate: "2023-01-12", endDate: "2023-01-15") {n tilln fromn marketPricen priceIncludingMarkupnt}ntmarketPricesGas(startDate: "2022-06-09", endDate: "2022-06-10") {n fromn tilln marketPricen priceIncludingMarkupn }n}"}'
$payload = '{"query": "query MarketPrices {ntmarketPricesElectricity(startDate: "2023-01-12", endDate: "2023-01-15") {n tilln fromn marketPricen priceIncludingMarkupnt}ntmarketPricesGas(startDate: "2022-06-09", endDate: "2022-06-10") {n fromn tilln marketPricen priceIncludingMarkupn }n}"}';