1

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

Cy Rossignol
  • 16,216
  • 4
  • 57
  • 83
Reza Fahmi
  • 270
  • 4
  • 11
  • I would suggest removing the request for someone to convert code to PHP, and describing more specifically what you mean by "doesn't work". – Don't Panic Nov 20 '17 at 20:17
  • request is from modules that installed in python, and what i mean with "doesnt work" is the file not uploaded to telegra.ph host.. – Reza Fahmi Nov 20 '17 at 20:24
  • Okay. Do you get any errors? Is PHP error reporting enabled? – Don't Panic Nov 20 '17 at 20:38
  • Yes this is error Warning: copy(http://telegra.ph/upload/): failed to open stream: HTTP wrapper does not support writeable connections @Don'tPanic – Reza Fahmi Nov 20 '17 at 20:42

1 Answers1

1

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?

Oleksandr Palii
  • 846
  • 7
  • 13