Omid Omid - 4 years ago 363
PHP Question

Send string starts with "@" in php curl as string

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 ?

Answer Source

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);
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download