cURL和PHP发布请求

时间:2020-05-15 19:07:32

标签: php curl

我是cURL的新手,无法理解。例如,我有这段代码

curl -d '
{
  "data": {
    "title": "Bubble Nebula",
    "body": "It`s found today at 21:00",
    "icon": "https://peter-gribanov.github.io/serviceworker/Bubble-Nebula.jpg",
    "image": "https://peter-gribanov.github.io/serviceworker/Bubble-Nebula_big.jpg",
    "click_action": "https://www.nasa.gov/feature/goddard/2016/hubble-sees-a-star-inflating-a-giant-bubble"
  }
  "to": "YOUR-TOKEN-ID"
}' \
    -H "Content-Type: application/json" \
    -H "Authorization: key=AAAAaGQ_q2M:APA91bGCEOduj8HM6gP24w2LEnesqM2zkL_qx2PJUSBjjeGSdJhCrDoJf_WbT7wpQZrynHlESAoZ1VHX9Nro6W_tqpJ3Aw-A292SVe_4Ho7tJQCQxSezDCoJsnqXjoaouMYIwr34vZTs" \
    -X POST "https://fcm.googleapis.com/fcm/send"

我应该如何在php上很好地工作?

1 个答案:

答案 0 :(得分:0)

在大多数情况下,我只是使用file_get_contents()来处理请求,但是可悲的是,这种技巧仅用于GET请求。

但是,我最近发现了一个新技巧,可用于Post和Get请求(包括curl可以进行的其他操作),并且仍然避免了可怕的php库phpcURL,而最好的部分是,您甚至不需要更改curl命令。尝试下面的代码。

<?php

$curl = "your curl command" // paste your curl command here

exec("$curl > curloutput.txt");
$response = file_get_contents("curloutput.txt");

echo($response);

?>

此代码应执行您给定的curl命令并返回输出。是的,我知道这听起来像是一种很笨拙的方法,但是它每次都能正常运行,而且超级易于使用。