相关疑难解决方法(0)

如何使用PHP发送POST请求?

实际上我想读完搜索查询后的内容.问题是URL只接受POST方法,并且不对GET方法采取任何操作......

我必须在domdocument或的帮助下阅读所有内容file_get_contents().是否有任何方法可以让我用POST方法发送参数然后通过读取内容PHP

php post http request

622
推荐指数
14
解决办法
91万
查看次数

函数调用前的@字符

PHP中这两个函数调用有什么区别?

init_get($somevariable);

@init_get($somevariable);
Run Code Online (Sandbox Code Playgroud)

php

126
推荐指数
5
解决办法
5万
查看次数

使用file_get_contents的HTTP请求,获取响应代码

我正在尝试file_get_contentsstream_context_createPOST 一起使用.我的代码到目前为止:

    $options = array('http' => array(
        'method'  => 'POST',
        'content' => $data,
        'header'  => 
            "Content-Type: text/plain\r\n" .
            "Content-Length: " . strlen($data) . "\r\n"
    ));
    $context  = stream_context_create($options);
    $response = file_get_contents($url, false, $context);
Run Code Online (Sandbox Code Playgroud)

它工作正常,但是,当发生HTTP错误时,它会发出警告:

file_get_contents(...): failed to open stream: HTTP request failed! HTTP/1.0 400 Bad Request
Run Code Online (Sandbox Code Playgroud)

并返回false.有办法:

  • 抑制警告(我打算在失败的情况下抛出自己的异常)
  • 从流中获取错误信息(至少是响应代码)

php http file stream

76
推荐指数
3
解决办法
7万
查看次数

即使它返回404,也可以在PHP中下载URL的内容

我想使用PHP下载URL的内容,即使HTTP响应代码是404. file_get_contents将出错,我无法使用Google找到答案.我怎样才能做到这一点?

php http-status-code-404

16
推荐指数
2
解决办法
1万
查看次数

如何避免file_get_contents错误:"无法解析主机名"

我成功地获取网站

file_get_contents("http://www.site.com");
Run Code Online (Sandbox Code Playgroud)

但是,如果网址不存在或无法访问,我会收到

Warning: file_get_contents(http://www.site.com) [function.file-get-contents]: 
failed to open stream: operation failed in /home/track/public_html/site.php 
on line 773
Run Code Online (Sandbox Code Playgroud)

是否可以echo "Site not reachable";代替错误?

php

6
推荐指数
2
解决办法
5314
查看次数

使用try catch块无法捕获PHP file_get_contents错误

我正在尝试使用file_get_contents函数获取图像,但它给出了错误。为了处理错误,我使用了try catch块,但它没有捕获错误并失败。

我的代码:

try {
     $url = 'http://wxdex.ocm/pdd.jpg'; //dummy url
     $file_content = file_get_contents($url);
}
catch(Exception $e) {
     echo 'Error Caught';           
}
Run Code Online (Sandbox Code Playgroud)

错误:

Warning: file_get_contents(): php_network_getaddresses: getaddrinfo failed: No such host is known
Warning: file_get_contents(http://wxdex.ocm/pdd.jpg): failed to open stream: php_network_getaddresses: getaddrinfo failed: No such host is known.
Run Code Online (Sandbox Code Playgroud)

注意::我能够在遥控器上获取任何其他有效的图像URL。

php error-handling

5
推荐指数
1
解决办法
1883
查看次数

file_get_contents失败时如何跳过代码?

当我使用如下所示的有效URL时,file_get_contents将返回html代码

$html = file_get_contents("http://www.google.com/");
Run Code Online (Sandbox Code Playgroud)

但如果网址无效:

$html = file_get_contents("http://www.go1235ogle.com/"); //Invalid URL. This line give Error Message
Run Code Online (Sandbox Code Playgroud)

我的代码将输出一条错误消息 Notice: Trying to get property of non-object

如果file_get_contents无法打开给定的URL,我想隐藏它正在输出的错误消息,并用if else语句跳过下面的代码。

if (FALSE === $html) 
{
  //skip the code in here ?
}
Run Code Online (Sandbox Code Playgroud)

但是上面的代码没有帮助。您能告诉我如何解决此问题吗?

php

1
推荐指数
1
解决办法
1162
查看次数

PHP catch无法打开流:HTTP请求失败!HTTP/1.1 401未经授权

我正在尝试执行以下操作.

当我尝试使用oAuth访问服务时,如果我收到错误,因为我使用了错误的密钥,我想不显示错误消息,而是将用户发送到身份验证页面.

但是使用try catch fn不起作用.

这是我的代码:

$url = 'https://api.soundcloud.com/me.json?'.http_build_query(array(
        'oauth_token' => $token,
    ));
    //
    try{
        $user = json_decode(file_get_contents($url));
        return array(
            'uid' => $user->id,
            'nickname' => $user->username,
            'name' => $user->full_name,
            'location' => $user->country.' ,'.$user->country,
            'description' => $user->description,
            'image' => $user->avatar_url,
            'urls' => array(
                'MySpace' => $user->myspace_name,
                'Website' => $user->website,
            ),
        );
    }
    catch(Services_Soundcloud_Invalid_Http_Response_Code_Exception $e)
    {
        log_message('error', 'EXCEPTION: '.$e);
        return FALSE;
    }
Run Code Online (Sandbox Code Playgroud)

我希望这可以返回用户数组或FALSE.

有任何想法吗?

php error-handling oauth oauth-2.0

0
推荐指数
1
解决办法
7638
查看次数