Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/64.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 使用IMGURAPI获取损坏的图像,即使url有效_Php_Mysql_Image_Api_Url - Fatal编程技术网

Php 使用IMGURAPI获取损坏的图像,即使url有效

Php 使用IMGURAPI获取损坏的图像,即使url有效,php,mysql,image,api,url,Php,Mysql,Image,Api,Url,这是我的代码,我正在使用imgurapi上传图像,并通过我的表单将它们显示在网页上。与实际显示的图像不同,只显示断页图标,如果我将断页图像复制到word或记事本中,则显示实际图像。很明显,当我将图像复制到word或记事本时,它是正确的路径。此外,我非常确定api可以工作,因为保存在数据库中的imgur url可以工作并显示图像。有什么遗漏或者我应该改变我显示图像的方式吗 if (isset($_POST['post'])) { if ($_FILES['postimg']['size']

这是我的代码,我正在使用imgurapi上传图像,并通过我的表单将它们显示在网页上。与实际显示的图像不同,只显示断页图标,如果我将断页图像复制到word或记事本中,则显示实际图像。很明显,当我将图像复制到word或记事本时,它是正确的路径。此外,我非常确定api可以工作,因为保存在数据库中的imgur url可以工作并显示图像。有什么遗漏或者我应该改变我显示图像的方式吗

if (isset($_POST['post'])) {
    if ($_FILES['postimg']['size'] == 0) {
        $postbody = $_POST['postbody'];
        $loggedInUserId = check::isLoggedIn();
        if (strlen($postbody) > 160 || strlen($postbody) < 1) {
            die('Incorrect length!');
        }

        connect::query('INSERT INTO dry_posts VALUES (null, :postbody, NOW(), 0,:postimg)', array(':postbody' => $postbody));

        // Post::createPost($_POST['postbody']);

    } else {

        $url = 'https://api.imgur.com/3/image'; // API endpoints, info: https://api.imgur.com/endpoints/image#image-upload
        $client_id = ''; // Get client_id here: https://api.imgur.com/#registerapp

        $fh = fopen($_FILES['postimg']['tmp_name'], 'r');
        $read = fread($fh, $_FILES['postimg']['size']);
        fclose($fh);
        $post = array(
            'image' => base64_encode($read)
        );
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_TIMEOUT, 30);
        curl_setopt($ch, CURLOPT_POST, true);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
        curl_setopt($ch, CURLOPT_HTTPHEADER, array(
            'Authorization: Client-ID '.$client_id
        ));
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        $json = curl_exec($ch); // Response, info: https://api.imgur.com/#responses
        curl_close($ch);
        // $image = json_decode($json, true); // Array
        $image = json_decode($json); // Object
        // var_dump($image);
        //$postimg = $image['data']['link']; // Array-style
        $postimg = $image - > data - > link; // Object-style

        connect::query('INSERT INTO dry_posts VALUES (null, \'\', NOW(), 0, :postimg)', array(':postimg' => $postimg));

    }


}
$dbposts = connect::query('SELECT * FROM dry_posts ORDER BY id DESC');
$posts = "";
//here's how I display the images/posts 
foreach($dbposts as $p) {
    if (!connect::query('SELECT post_id FROM post_likes WHERE post_id=:postid', array(':postid' => $p['id']))) {
        $posts. = "<img src='".$p['postimg'].
        "'>".htmlspecialchars($p['body']).
        " <
        form action = 'try.php?postid=".$p['
        id ']."'
        method = 'post' >
            <
            input type = 'submit'
        name = 'like'
        value = 'Like' >
            <
            span > ".$p['likes']."
        likes < /span> <
            /form> <
            hr / > < /br / >
            ";

    } else {
        $posts. = "<img src='".$p['postimg'].
        "'>".htmlspecialchars($p['body']).
        " <
        form action = 'try.php?postid=".$p['
        id ']."'
        method = 'post' >
            <
            input type = 'submit'
        name = 'unlike'
        value = 'Unlike' >
            <
            span > ".$p['likes']."
        likes < /span> <
            /form> <
            hr / > < /br / >
            ";
    }
}
这是表格

<form action="try.php" class = "forum" method="post" enctype="multipart/form-data">
        <textarea name="postbody" rows="4" cols="60" class = "text"></textarea>
         <br />Upload an image:
        <input type="file" name="postimg">
        <input type="submit" name="post" value="Post">
        <input type="submit" value="Upload">
</form>

<div class="posts">
        <?php echo $posts; ?>
</div>
我认为这就是所谓的,因此所有上传的图片都是匿名共享的,没有附加到您的帐户

为了将图像上传到您的私人帐户,您可能需要获得访问令牌,并使用承载身份验证。

我认为这是一个调用,因此所有上传的图像都是匿名共享的,而不会附加到您的帐户


要将图像上载到您的私人帐户,您可能需要获取访问令牌,并使用承载身份验证。

你能提供一个示例断开图像url吗?奇怪,刚才图像开始显示,但我没有更改代码,我确实清除了缓存,但当我这样做时,它没有立即更改。你能提供一个示例断开图像url吗?奇怪,刚才图像开始显示,但我没有更改代码,我确实清除了缓存但当我这么做的时候,它并没有马上改变