2

I'm trying to send a string like "@mystr" with php using CURL POST method but if when I just send with following code:

        $url_str = $url->getUrl();
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url_str);
        curl_setopt($ch, CURLOPT_POST, count($fields));
        curl_setopt($ch, CURLOPT_POSTFIELDS, $fields);
        curl_setopt($ch, CURLOPT_HTTPHEADER, ['Content-Type:multipart/form-data']);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);

        curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);

        $out = curl_exec($ch);
        curl_close($ch);

It will throw an error that says:

Deprecated: curl_setopt(): The usage of the @filename API for file uploading is deprecated. Please use the CURLFile class instead...

Note: $fields are something like this:

$fields=['chat_id'=>'@someusername','text'=>'hello'];

It's for telegram bot api, if I use %40 instead of @ in GET method it will be ok and telegram will sends my message to my channel, but I have to use POST method.

Is there anyway to send strings starts with @ in CURL ?

Omid
  • 2,014
  • 4
  • 28
  • 46
  • Possible duplicate of http://stackoverflow.com/questions/10060093/special-characters-like-and-in-curl-post-data – Jan Nov 16 '15 at 19:28
  • 2
    @Jan no it's not, this is not that CURL this is PHP curl class – Omid Nov 16 '15 at 19:30
  • 1
    It is against RFC standards to use `@` in a GET parameter or URI string unless it is used to symbolize information before the resource path such as in the URI `ftp://user:pass@ftp.domain.com/folder`. Posting is another story, but just wanted to explain why you were forced to use `%40` in GET. This question is a duplicate of another, just not the one Jan flagged. One second on proper duplicate flag.... – skrilled Nov 16 '15 at 19:38
  • Possible duplicate of [PHP + curl, HTTP POST sample code?](http://stackoverflow.com/questions/2138527/php-curl-http-post-sample-code) – skrilled Nov 16 '15 at 19:39
  • 1
    @Omid: php curl is just a wrapper around the exact same library that command line curl uses. the behavior is essentially identical. `@` at the start of ANY field's value is a curl shortcut to signify a file upload. if you want that `@` to NOT be treated as a file upload, you have to url-encode it. – Marc B Nov 16 '15 at 19:41
  • look i have use urlencode but telegram wont decode it with post method, telegram just decode our parameters only when we use GET method, and if I have to send my files to my channel, it is not possible at all with GET because the parameter point channel user name must contain a chat_id or a channel username, and channel username starts with @ – Omid Nov 16 '15 at 19:43
  • @marcB have you any idea? – Omid Nov 16 '15 at 19:48

1 Answers1

3

I have found the solution here: http://php.net/manual/en/function.curl-setopt.php

CURLOPT_SAFE_UPLOAD

TRUE to disable support for the @ prefix for uploading files in CURLOPT_POSTFIELDS, which means that values starting with @ can be safely passed as fields. CURLFile may be used for uploads instead.

Added in PHP 5.5.0 with FALSE as the default value. PHP 5.6.0 changes the default value to TRUE.

    $url_str = $url->getUrl();
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_SAFE_UPLOAD, TRUE); //<- HERE
    curl_setopt($ch, CURLOPT_URL, $url_str);
    curl_setopt($ch, CURLOPT_POST, count($fields));
    curl_setopt($ch, CURLOPT_POSTFIELDS, $fields);
    curl_setopt($ch, CURLOPT_HTTPHEADER, ['Content-Type:multipart/form-data']);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);

    curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);

    $out = curl_exec($ch);
    curl_close($ch);
Omid
  • 2,014
  • 4
  • 28
  • 46