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 ,$机构);有GET请求?
Cause this enteire code works perfect with POST, now i'm trying change this to GET as you can see
因为这个enteire代码与POST完美配合,现在我正在尝试将此更改为GET,如您所见
4 个解决方案
#1
22
CURLOPT_POSTFIELDS
as the name suggests, is for the body (payload) of a POST
request. For GET
requests, the payload is part of the URL in the form of a query string.
CURLOPT_POSTFIELDS顾名思义,是针对POST请求的正文(有效负载)。对于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);
#2
17
The accepted answer is wrong. GET
requests can indeed contain a body. This is the solution implemented by WordPress, as an example:
接受的答案是错误的。 GET请求确实可以包含一个正文。这是WordPress实现的解决方案,例如:
curl_setopt( $ch, CURLOPT_CUSTOMREQUEST, 'GET' );
curl_setopt( $ch, CURLOPT_POSTFIELDS, $body );
EDIT: To clarify, the initial curl_setopt
is not necessary in this instance, but does no harm. It was included to fully illustrate the example code being referenced.
编辑:为了澄清,在这种情况下,初始curl_setopt不是必需的,但没有坏处。包含它是为了充分说明被引用的示例代码。
#3
2
<?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);
$new= $result->status;
if( $new =="1")
{
echo "<script>alert('Student list')</script>";
}
else
{
echo "<script>alert('Not Removed')</script>";
}
?>
#4
-1
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);
#1
22
CURLOPT_POSTFIELDS
as the name suggests, is for the body (payload) of a POST
request. For GET
requests, the payload is part of the URL in the form of a query string.
CURLOPT_POSTFIELDS顾名思义,是针对POST请求的正文(有效负载)。对于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);
#2
17
The accepted answer is wrong. GET
requests can indeed contain a body. This is the solution implemented by WordPress, as an example:
接受的答案是错误的。 GET请求确实可以包含一个正文。这是WordPress实现的解决方案,例如:
curl_setopt( $ch, CURLOPT_CUSTOMREQUEST, 'GET' );
curl_setopt( $ch, CURLOPT_POSTFIELDS, $body );
EDIT: To clarify, the initial curl_setopt
is not necessary in this instance, but does no harm. It was included to fully illustrate the example code being referenced.
编辑:为了澄清,在这种情况下,初始curl_setopt不是必需的,但没有坏处。包含它是为了充分说明被引用的示例代码。
#3
2
<?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);
$new= $result->status;
if( $new =="1")
{
echo "<script>alert('Student list')</script>";
}
else
{
echo "<script>alert('Not Removed')</script>";
}
?>
#4
-1
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);