its*_*sme 28 php curl http-get
我正在尝试使用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;
}
Run Code Online (Sandbox Code Playgroud)
当你看到我想传递$ body作为请求的正文,但我不知道它是否正确而我实际上无法调试,你知道是否有权使用 curl_setopt($ch, CURLOPT_POSTFIELDS,$body);GET请求吗?
因为这个enteire代码与POST完美配合,现在我正在尝试将此更改为GET,如您所见
Dan*_*Dan 27
接受的答案是错误的.GET请求确实可以包含一个正文.这是WordPress实现的解决方案,例如:
curl_setopt( $ch, CURLOPT_CUSTOMREQUEST, 'GET' );
curl_setopt( $ch, CURLOPT_POSTFIELDS, $body );
Run Code Online (Sandbox Code Playgroud)
编辑:为了澄清,curl_setopt在这种情况下,初始是没有必要的,但没有伤害.包含它是为了充分说明所引用的示例代码.
Bur*_*lid 24
CURLOPT_POSTFIELDS顾名思义,是针对POST请求的正文(有效负载).对于GET请求,有效负载是查询字符串形式的URL的一部分.
在您的情况下,您需要使用您需要发送的参数(如果有)构造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);
Run Code Online (Sandbox Code Playgroud)
小智 5
<?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>";
}
?>
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
139847 次 |
| 最近记录: |