简体   繁体   English

PHP curl没有错误或结果

[英]PHP curl no error or result

When running the URL in browser prodivding the same credentials as in below code it generates an XML. 在浏览器中运行URL并提供与以下代码相同的凭据时,它将生成XML。 However when running the below code from server I get no error message or any result. 但是,从服务器运行以下代码时,我没有收到错误消息或任何结果。 What am I doing wrong is it a IP-block, code error or? 我在做什么错是IP块,代码错误还是?

According to documentation authentication is "basic HTTP/1.0 authentication request". 根据文档,身份验证是“基本HTTP / 1.0身份验证请求”。

 <?php ini_set('display_startup_errors',1); ini_set('display_errors',1); error_reporting(-1); $username="myUsername"; $password="myPassword"; $url="http://domain.com?getFeed.php?id=3"; $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_ANYSAFE); curl_setopt($ch, CURLOPT_USERPWD, "$username:$password"); $file = curl_exec($ch); curl_close($ch); print $file; ?> 

同时添加以下代码,您将获得有关失败原因的正确信息。

curl_setopt($curlhandle, CURLOPT_VERBOSE, true);

let me quote documentation : 让我引用文档

Returns TRUE on success or FALSE on failure. 成功返回TRUE,失败返回FALSE。 However, if the CURLOPT_RETURNTRANSFER option is set, it will return the result on success, FALSE on failure. 但是,如果设置了CURLOPT_RETURNTRANSFER选项,则成功时将返回结果,失败时将返回FALSE。

as you do not set this option, $file is either true or false. 由于未设置此选项,因此$file为true或false。 but you just print it. 但您只打印它。

if ($file) {
    // success
    // get the body
} else {
    // failure
    // check curl_error()
}

One error you have is that you use the wrong HTTP authentication method, you use CURLAUTH_ANYSAFE , but you write that it should be 您遇到的一个错误是您使用了错误的HTTP身份验证方法,使用了CURLAUTH_ANYSAFE ,但是您写道:

basic HTTP/1.0 authentication request 基本的HTTP / 1.0身份验证请求

Thus, set the CURLOPT_HTTPAUTH to, 因此,请将CURLOPT_HTTPAUTH设置为

url_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);

(Or leave it blank since CURLAUTH_BASIC is the default) (或将其留空,因为默认为CURLAUTH_BASIC

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

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