相关疑难解决方法(0)

我可以尝试/发出警告吗?

我需要从一些php本机函数中捕获一些警告,然后处理它们.

特别:

array dns_get_record  ( string $hostname  [, int $type= DNS_ANY  [, array &$authns  [, array &$addtl  ]]] )
Run Code Online (Sandbox Code Playgroud)

当DNS查询失败时,它会发出警告.

try/ catch不起作用,因为警告不是例外.

我现在有2个选择:

  1. set_error_handler 看起来有点矫枉过正,因为我必须使用它来过滤页面中的每个警告(这是真的吗?);

  2. 调整错误报告/显示,以便这些警告不会回显到屏幕,然后检查返回值; 如果是false,则没有找到主机名的记录.

这里的最佳做法是什么?

php error-handling try-catch

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

将PERMALINK存储在PHP变量中

我有这个代码来计算我帖子中所有社交网站的份额.代码的问题是永久链接是静态的.

如果你注意到,我将永久链接存储在$myurl变量中get_permalink();,代码的问题就是我改变了链接" http://www.bendaggers.com/how-to-write-a-killer-resume-that-lands-采访/ ",它给了我很多错误,如:

file_get_contents(http://graph.facebook.com/?id= $ myurl):无法打开流:HTTP请求失败!

这是我的代码:

$myurl = get_permalink();
$url = urlencode($url);

$facebook_like_share_count = function ( $url ) {
    $api = file_get_contents( 'http://graph.facebook.com/?id=' . $url );
    $count = json_decode( $api );
    return $count->shares;
};


$twitter_tweet_count = function ( $url ) {
    $api = file_get_contents( 'https://cdn.api.twitter.com/1/urls/count.json?url=' . $url );
    $count = json_decode( $api );
    return $count->count;
};


$pinterest_pins = function ( $url ) {
    $api = file_get_contents( 'http://api.pinterest.com/v1/urls/count.json?callback%20&url=' . $url );
    $body = preg_replace( …
Run Code Online (Sandbox Code Playgroud)

php variables

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

Telegram + PHP (Windows 7):无法打开流:HTTP 请求失败!HTTP/1.1 404 未找到

我正在尝试迈出 Telegram 的第一步,而且我也是 PHP 的新手......

我已经在我的 Windows 7 电脑上配置了带有 PHP 5.6.14 和 SSL 的 Apache 2.4,并且它在 http 和 https 中运行良好。

然后我尝试遵循此 Telegram Bot 教程https://www.youtube.com/watch?v=hJBYojK7DO4。一切都工作正常,直到我必须创建一个像这样的简单 PHP 程序

<?php
  $botToken = "<my_bot_token>";
  $website = "https://api.telegram.org/bot".$botToken; 
  $update = file_get_contents($website."/getUpates"); 
  print_r($update);
?>
Run Code Online (Sandbox Code Playgroud)

当我尝试输入浏览器时

https://localhost/Telegram/MyYouTubeTutorialBot/YouTubeTutorialBot.php
Run Code Online (Sandbox Code Playgroud)

响应是

Warning: file_get_contents(https://api.telegram.org/<my_bot_token>/getupates): failed to open stream: HTTP request failed! HTTP/1.1 404 Not Found in <my_php_file_location> on line 6
Run Code Online (Sandbox Code Playgroud)

我在网上搜索了类似的问题,但没有解决任何问题:最有趣的响应是这个问题file_get_contents - failed to open stream: HTTP request failed! HTTP/1.1 404 Not Found但我不明白如何使其适应我的情况。

在其他回复中,建议使用curl,但我想解决持续的file_get_contents 函数。

我认为这不是 PHP …

php apache2.4 php-5.6 telegram telegram-bot

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

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
查看次数