PHP cURL GET 请求和请求的正文

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17230246/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 12:32:23  来源:igfitidea点击:

PHP cURL GET request and request's body

phpcurlhttp-get

提问by itsme

i'm trying using cURL for a GET request like this:

我正在尝试将 cURL 用于这样的 GET 请求:

function connect($id_user){
    $ch = curl_init();
    $headers = array(
    'Accept: application/json',
    'Content-Type: application/json',

    );
    curl_setopt($ch, CURLOPT_URL, $this->service_url.'user/'.$id_user);
    curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
    curl_setopt($ch, CURLOPT_HEADER, 0);
    $body = '{}';

    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET"); 
    curl_setopt($ch, CURLOPT_POSTFIELDS,$body);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

    // Timeout in seconds
    curl_setopt($ch, CURLOPT_TIMEOUT, 30);

    $authToken = curl_exec($ch);

    return $authToken;
}

As you an see i want to pass $body as the request's body , but i don't know if its correct or not and i can't debug this actually, do you know if is the right to use curl_setopt($ch, CURLOPT_POSTFIELDS,$body);with a GET request?

正如您所看到的,我想将 $body 作为请求的正文传递,但我不知道它是否正确,而且我实际上无法调试,您知道是否可以curl_setopt($ch, CURLOPT_POSTFIELDS,$body);与 GET 请求一起使用 吗?

Cause this enteire code works perfect with POST, now i'm trying change this to GET as you can see

因为这个完整的代码与 POST 完美配合,现在我正在尝试将其更改为 GET,如您所见

采纳答案by Burhan Khalid

CURLOPT_POSTFIELDSas the name suggests, is for the body (payload) of a POSTrequest. For GETrequests, the payload is part of the URL in the form of a query string.

CURLOPT_POSTFIELDS顾名思义,是针对POST请求的主体(payload)。对于GET请求,有效负载是查询字符串形式的 URL 的一部分。

In your case, you need to construct the URL with the arguments you need to send (if any), and remove the other options to cURL.

在您的情况下,您需要使用需要发送的参数(如果有)构造 URL,并删除 cURL 的其他选项。

curl_setopt($ch, CURLOPT_URL, $this->service_url.'user/'.$id_user);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_HEADER, 0);

//$body = '{}';
//curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET"); 
//curl_setopt($ch, CURLOPT_POSTFIELDS,$body);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

回答by Dan

The accepted answer is wrong. GETrequests can indeed contain a body. This is the solution implemented by WordPress, as an example:

接受的答案是错误的。GETrequests 确实可以包含一个正文。这是WordPress 实现的解决方案,例如:

curl_setopt( $ch, CURLOPT_CUSTOMREQUEST, 'GET' );
curl_setopt( $ch, CURLOPT_POSTFIELDS, $body );

EDIT: To clarify, the initial curl_setoptis not necessary in this instance, but does no harm. It was included to fully illustrate the example code being referenced.

编辑:澄清一下,curl_setopt在这种情况下不需要首字母,但没有害处。包含它是为了充分说明所引用的示例代码。

回答by Siddharth Shukla

  <?php
  $post = ['batch_id'=> "2"];
  $ch = curl_init();
  curl_setopt($ch, CURLOPT_URL,'https://example.com/student_list.php');
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($post));
  $response = curl_exec($ch);
  $result = json_decode($response);
  curl_close($ch); // Close the connection
  $new=   $result->status;
  if( $new =="1")
  {
    echo "<script>alert('Student list')</script>";
  }
  else 
  {
    echo "<script>alert('Not Removed')</script>";
  }

  ?>

回答by Danny Sofftie

For those coming to this with similar problems, this request libraryallows you to make external http requests seemlessly within your php application. Simplified GET, POST, PATCH, DELETE and PUT requests.

对于那些遇到类似问题的人,这个请求库允许您在 php 应用程序中无缝地发出外部 http 请求。简化的 GET、POST、PATCH、DELETE 和 PUT 请求。

A sample request would be as below

示例请求如下

use Libraries\Request;

$data = [
  'samplekey' => 'value',
  'otherkey' => 'othervalue'
];

$headers = [
  'Content-Type' => 'application/json',
  'Content-Length' => sizeof($data)
];

$response = Request::post('https://example.com', $data, $headers);
// the $response variable contains response from the request

Documentation for the same can be found in the project's README.md

可以在项目的README.md 中找到相同的文档

回答by DevZer0

you have done it the correct way using

你已经用正确的方法完成了

curl_setopt($ch, CURLOPT_POSTFIELDS,$body);

but i notice your missing

但我注意到你的失踪

curl_setopt($ch, CURLOPT_POST,1);