dongyou6768 2017-11-20 20:14
浏览 228

使用PHP将图像上传到telegra.ph

I don't know the Python language. Can someone help me understand whether I correctly converted this to PHP? I want to upload a file to http://telegra.ph with PHP, but I don't know how to upload from PHP.

This code is an example from a Python Telegraph API wrapper library:

import requests

with open('/Users/python273/Desktop/123345.jpeg', 'rb') as f:
    print(
        requests.post(
            'http://telegra.ph/upload',
            files={'file': ('file', f, 'image/jpeg')}  # image/gif, image/jpeg, image/jpg, image/png, video/mp4
        ).json()
    )

And this is my PHP code that doesn't work. Is there something wrong?

$url = 'http://example.com/image.jpg';
$img = 'http://telegra.ph/upload/';
$ch = curl_init($url);
$fp = fopen($img, 'wb');
curl_setopt($ch, CURLOPT_FILE, $fp);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_exec($ch);
curl_close($ch);
fclose($fp);

When I run this code, I see the following error:

Warning: copy(telegra.ph/upload): failed to open stream: HTTP wrapper does not support writeable connections

  • 写回答

1条回答 默认 最新

  • doulan4371 2017-11-21 00:34
    关注

    Your code:

    $img = 'http://telegra.ph/upload/';
    ...
    $fp = fopen($img, 'wb');
    

    PHP fopen() documentation is here http://php.net/manual/en/function.fopen.php , check what $mode parameter means. Why are you trying to open remote file for writing, if you only need to read it?

    评论

报告相同问题?

悬赏问题

  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥15 想问一下树莓派接上显示屏后出现如图所示画面,是什么问题导致的
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化