简体   繁体   English

PHP cURL GET 请求和请求的正文

[英]PHP cURL GET request and request's body

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);正如你所看到的,我想将 $body 作为请求的正文传递,但我不知道它是否正确,我实际上无法调试,你知道使用curl_setopt($ch, CURLOPT_POSTFIELDS,$body); with a GET request?使用 GET 请求?

Cause this enteire code works perfect with POST, now i'm trying change this to GET as you can see因为这整个代码与 POST 完美配合,现在我正在尝试将其更改为 GET,如您所见

The accepted answer is wrong.接受的答案是错误的。 GET requests can indeed contain a body. GET请求确实可以包含一个正文。 This is the solution implemented by WordPress , as an example:这是WordPress 实现的解决方案,例如:

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

EDIT: To clarify, the initial curl_setopt is necessary in this instance, because libcurl will default the HTTP method to POST when using CURLOPT_POSTFIELDS (see documentation ).编辑:澄清一下,在这种情况下,初始curl_setopt是必要的,因为 libcurl 在使用CURLOPT_POSTFIELDS时会将 HTTP 方法默认为POST (请参阅文档)。

CURLOPT_POSTFIELDS as the name suggests, is for the body (payload) of a POST request. CURLOPT_POSTFIELDS顾名思义,用于POST请求的主体(有效负载)。 For GET requests, the payload is part of the URL in the form of a query string.对于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);
  <?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>";
  }

  ?>

For those coming to this with similar problems, this request library allows you to make external http requests seemlessly within your php application.对于那些遇到类似问题的人, 这个请求库允许您在 php 应用程序中无缝地发出外部 http 请求。 Simplified GET, POST, PATCH, DELETE and PUT requests.简化的 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 中找到相同的文档

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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM