简体   繁体   English

cURL请求未在PHP中发送

[英]cURL request is not being sent in PHP

I am trying to get a basic cURL request to work. 我正在尝试获取基本的cURL请求以进行工作。 I am running Wamp 2.5 and PHP 5.5.12. 我正在运行Wamp 2.5和PHP 5.5.12。 I've double checked the php.ini files and have made sure cURL was actually set up and ready to go. 我已经仔细检查了php.ini文件,并确保确实设置了cURL并准备就绪。 Here is my code: 这是我的代码:

$url = "https://www.google.com";
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_TIMEOUT, 30); //timeout after 30 seconds
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$status_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);   //get status code
$response = curl_exec($ch);

echo $status_code;

There is no error returned except "0", and if I do var_dump the result is "boolean false". 除“ 0”外没有返回任何错误,并且如果我执行var_dump,结果为“布尔假”。 What am I doing wrong? 我究竟做错了什么?

Happens, a stupid mistake. 发生了一个愚蠢的错误。 Happens to all of us :-) 发生在我们所有人身上:-)

Try this: 尝试这个:

<?php
$url = "https://www.google.com";
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_TIMEOUT, 30); //timeout after 30 seconds
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
$status_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);   //get status code
echo $status_code;

The output is: 302 输出为: 302

You obviously first have to execute the request before you then can get its status code. 显然,您首先必须执行请求, 然后才能获取其状态代码。

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

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