如何使用curl在php中调试get请求 [英] How to debug a get request in php using curl

查看:66
本文介绍了如何使用curl在php中调试get请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用curl在php中发出get请求.这就是我正在做的:

I'm trying to make a get request in php using curl. This is what I'm doing:

$curl = curl_init();

curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($curl, CURLOPT_USERPWD, "username:password");

curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);

$result = curl_exec($curl);
curl_close($curl);

printf($result);

但是$ result不会输出任何内容,也不会显示成功或失败消息.我已经通过邮递员和网络浏览器成功到达端点,因此我知道它可以工作.打印$ curl打印:资源#1",这使我认为curl已正确安装在服务器上.

But $result doesn't print out anything, no success or failure message. I've successfully reached the endpoint via postman and in a web browser so I know it works. Printing out $curl prints: "Resource #1" which makes me think curl is properly installed on the server.

我不确定下一步该怎么做.

I'm not sure what steps to take next to make things work.

推荐答案

添加更多用于故障排除的选项.

Add a few more option for troubleshooting purposes.

检查错误响应.

如果没有错误,请获取详细信息:

If no error, get the details:

curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);
curl_setopt($ch, CURLOPT_TIMEOUT,10);
curl_setopt($ch, CURLOPT_FAILONERROR,true);
curl_setopt($ch, CURLOPT_ENCODING,"");

curl_setopt($ch, CURLOPT_VERBOSE, true);
curl_setopt($ch, CURLINFO_HEADER_OUT, true);
curl_setopt($ch, CURLOPT_HEADER, true);

$data = curl_exec($ch);
if (curl_errno($ch)){
    $data .= 'Retreive Base Page Error: ' . curl_error($ch);
}
else {
  $skip = intval(curl_getinfo($ch, CURLINFO_HEADER_SIZE)); 
  $head = substr($data,0,$skip);
  $data = substr($data,$skip);
  $info = curl_getinfo($ch);
  $info = var_export($info,true);
}
echo $head;
echo $info;

这篇关于如何使用curl在php中调试get请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆