Telegram Bot API sendPhoto url contains characters like right single quotation mark (’) - url

I am using standard Telegram Bot API for sending messages. When a photo url contains a special character like a right single quotation mark (’) and left single quote (‘), the message is not sent, as it is a Bad Request.
When php rawurlencode is run, ’ shows as %E2%80%99 and ‘ as %E2%80%98
$photourl='https://example.com/some/path/name-of-image-with‘quotes’.jpg';
$array =[
'chat_id' => $uid,
'photo' => $photourl,
'caption' => $caption
];
file_get_contents("https://api.telegram.org/bot$api/sendPhoto?".http_build_query($array) );
it gives a
PHP WARNING --
failed to open stream: HTTP request failed! HTTP/1.1 400 Bad Request

Try this:
$photourl = 'https://example.com/some/path/name-of-image-with‘quotes’.jpg';
$filename = basename($photourl);
$photourl = str_replace($filename, urlencode($filename), $photourl); //Replace file name wirth url encoded filename
$array = [
'chat_id' => $uid,
'photo' => $photourl,
'caption' => $caption
];
file_get_contents("https://api.telegram.org/bot$api/sendPhoto?" . http_build_query($array));
Using characters like ’,‘ or * in photo parameter seem to cause a problem.
I don't have any explanation why this error occurs or how this fixes it. But you can url encode the URL's file name part before passing your query to http_build_query(), (which will encode it even a second time).

Related

Zappier - Webhook by Zappier GET - URL With space character

The context :
- I want to send request to the service layer of a Hana data base
- I use the Webhook from Zappier to sent POST or GET method (I suppose this is the good component ?)
- I use classic GET or Custom Request with the Webhook from Zapier
My problem :
- it seems that if the URL contains a space character, it doesn't work properly :(
example :
https://<<the_beginning_of_the_url>>/PurchaseOrders?$select=DocEntry,DocNum,CardCode,CardName&$orderby=CardName => WORK FINE(NO SPACE)
https://<<the_beginning_of_the_url>>/PurchaseOrders(30)?$select=DocEntry,DocNum,CardCode,CardName&$orderby=CardName => WORK FINE(NO SPACE)
https://<<the_beginning_of_the_url>>/PurchaseOrders?$select=DocEntry,DocNum,CardCode,CardName&$orderby=CardName desc => **DOESN'T WORK** (SPACE BEFORE 'desc')
https://<<the_beginning_of_the_url>>/PurchaseOrders?$select=DocEntry,DocNum,CardCode,CardName&$orderby=CardName%20desc => **DOESN'T WORK** (SPACE BEFORE 'desc')
https://<<the_beginning_of_the_url>>/PurchaseOrders?$select=DocEntry,DocNum,CardCode,CardName&$orderby=CardName+desc => **DOESN'T WORK** (SPACE BEFORE 'desc')
All this (except the last with '+') work fine with Postman
I've tried to use Formatter by Zappier and Text/URL Encode method before sending the URL, but it doesn't work
Any idea ?
It's seems that Webhook By Zapier encode the URL before to send it.
So the true URL which is send contains '+' even if we submit a space or %20
Is it possible not to encode URL before Webhook send it ?
We really need to send a URL with %20 or space

Amazon: is it possible to specify zip code in the URL for Amazon search results?

I have noticed an issue. If I copy Amazon URL with search results and somebody with another IP opens it then the results can be different.
For example:
https://www.amazon.com/s/ref=sr_nr_p_36_0?lo=toys-and-games&rh=n%3A165793011%2Cp_72%3A1248964011&sort=price-desc-rank&low-price=34.99&high-price=34.99
If you open this URL in from Dallas IP you'll get 102 pages with results.
If you open it with Honolulu IP you'll get 101 pages.
If you open it from Russian IP you'll get 93 pages.
Is that possible to specify US ZIP code for shipping right in the url so that it displays same results for every IP address?
Another little issue I have noticed - it displays different page layout for different people. Sometimes it's default blue links, sometimes it has silver buttons. Maybe somebody knows how to lock the design to one layout with url parameters? :)
There is no simple solution, so here is my complicated way.
The idea is: you must send the same request which is get sent when you manually change ZIP in your browser. Then your ZIP code will be remembered for you session.
Here is my solution in PHP using GuzzleHttp Client:
$jar = new \GuzzleHttp\Cookie\CookieJar();
$client = new \GuzzleHttp\Client([
'headers' => [
'accept' => 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
'accept-language' => 'en;q=0.8',
'user-agent' => '', //set some User-Agent or just leave it empty cos it works too
'x-requested-with' => 'XMLHttpRequest'
],
'cookies' => $jar,
]);
try {
$client->post('https://www.amazon.com/gp/delivery/ajax/address-change.html', [
'form_params' => [
'locationType' => 'LOCATION_INPUT',
'zipCode' => '11219', //YOUR ZIP HERE
'storeContext' => 'office-products',
'deviceType' => 'web',
'pageType' => 'Detail',
'actionSource' => 'glow',
]
]);
} catch (RequestException $e) {
echo "Failed to set ZIP";
}
$response = $client->get('...'); //get any other page from Amazon, now it will have proper ZIP
I'm using awesome Guzzle feature - cookies container: http://docs.guzzlephp.org/en/stable/request-options.html#cookies
It can remember and process cookies between requests just like browser would do.
In all further requests you should keep using these cookies and it will return you results for your ZIP.
Of course you can process cookies manually, Guzzle isn't required but makes things simpler.

Drupal 8 Guzzle Format Query String

Forgive me for my ignorance, this is my first attempt at Drupal 8 and I'm not a good php developer to begin with. But I've been reading and searching for hours. I'm trying to do a post using the new Guzzle that replaces the drupal_http_request(). I've done this using Curl but can't seem to get this going in the right direction here. I'm just not "getting it".
Here is a sample of the array I have that pulls data from a custom form. I also tried this with a custom variable where I built the string.
$fields = array(
"enroll_id" => $plan,
"notice_date" => $date,
"effective_date" => $date,
);
$client = \Drupal::httpClient();
$response = $client->post('myCustomURL', ['query' => $fields]);
$data = $response->getBody()->getContents();
try {
drupal_set_message($data);
} catch (RequestException $e) {
watchdog_exception('MyCustomForm', $e->getMessage());
}
This indeed returns the result of REJECTED from my API in $data below - but it doesn't append the URL to included the query => array. I've tried numerous combinations of this just putting the fully built URL in the post (that works with my API - tested) and I still receive the same result from my API. In the end what I'm trying to accomplish is
https://myCustomURL?enroll_id=value&notice_date=12/12/12&effective_date=12/12/12
Any direction or tips would be much appreciated.
Thanks for the responses guys. I was able to get it to work correctly by changing a few things in my post. First changing client -> post to a request('POST', XXX) and then changing "query" to "form_params" as "body" has been deprecated.
http://docs.guzzlephp.org/en/latest/quickstart.html#query-string-parameters
$client = \Drupal::httpClient();
$response = $client->request('POST','https://myURL.html', ['form_params' => $fields]);
$data = $response->getBody()->getContents();
Using $client->post will send a POST request. By looking at the URL that you tested directly you want a GET request.
Either use $client->get or $client->request with the GET parameter. More information and examples in the Guzzle documentation.

Why does this twitter oauth API token request fail

[Note: all oauth tokens/secrets below were created randomly; they are
NOT my actual tokens/secrets]
curl -o /tmp/test.txt 'https://api.twitter.com/oauth/request_token?
oauth_timestamp=1345141469&
consumer_key=UEIUyoBjBRomdvrVcUTn&oauth_access_token_secret=YePiEkSDFdYAOgscijMCazcSfBflykjsEyaaVbuJeO&oauth_access_token=47849378%2drZlzmwutYqGypbLsQUoZUsGdDkVVRkjkOkSfikNZC&oauth_nonce=1345141469&
consumer_secret=rUOeZMYraAapKmXqYpxNLTOuGNmAQbGFqUEpPRlW&
oauth_version=1%2e0&
oauth_signature_method=HMAC%2dSHA1&oauth_signature=H0KLLecZNAAz%2bXoyrPRiUs37X3Zz%2bAcabMa5M4oDLkM'
[I added newlines for clarity; actual command is one single line]
Assuming all the other data is valid, why does the command above yield
"Failed to validate oauth signature and token" (even when I use my
real data)?
In particular, is my signature
"H0KLLecZNAAz%2bXoyrPRiUs37X3Zz%2bAcabMa5M4oDLkM" invalid, or am I
doing something more fundamentally wrong.
The program I used to generate this:
#!/bin/perl
use Digest::SHA;
%twitter_auth_hash = (
"oauth_access_token" => "47849378-rZlzmwutYqGypbLsQUoZUsGdDkVVRkjkOkSfikNZC",
"oauth_access_token_secret" => "YePiEkSDFdYAOgscijMCazcSfBflykjsEyaaVbuJeO",
"consumer_key" => "UEIUyoBjBRomdvrVcUTn",
"consumer_secret" => "rUOeZMYraAapKmXqYpxNLTOuGNmAQbGFqUEpPRlW"
);
# if uncommented, pull my actual data
# require "bc-private.pl";
$twitter_auth_hash{"oauth_signature_method"} = "HMAC-SHA1";
$twitter_auth_hash{"oauth_version"} = "1.0";
$twitter_auth_hash{"oauth_timestamp"} = time();
$twitter_auth_hash{"oauth_nonce"} = time();
for $i (keys %twitter_auth_hash) {
push(#str,"$i=".urlencode($twitter_auth_hash{$i}));
}
$str = join("&",#str);
# thing to sign
$url = "GET $str";
# signing it
$sig = urlencode(Digest::SHA::hmac_sha256_base64($url, "rUOeZMYraAapKmXqYpxNLTOuGNmAQbGFqUEpPRlW&YePiEkSDFdYAOgscijMCazcSfBflykjsEyaaVbuJeO"));
# full URL incl sig
$furl = "https://api.twitter.com/oauth/request_token?$str&oauth_signature=$sig";
# system("curl -o /tmp/testing.txt '$furl'");
print "FURL: $furl\n";
print "STR: $str\n";
print "SIG: $sig\n";
sub urlencode {
my($str) = #_;
$str=~s/([^a-zA-Z0-9])/"%".unpack("H2",$1)/iseg;
$str=~s/ /\+/isg;
return $str;
}
Note: I realize there are many other possible reasons this is failing,
but current question is: am I sending the parameters correctly and am
I computing the signature correctly.
Twitter asks that you do a POST for the request token.

How do I format a retweet request through the Abraham twitteroauth php class?

I'm damned if I can make this work. Your help would be appreciated. I have valid access tokens, and can use twitteroauth to post status updates. However: every way I've tried to come at retweets has failed.
$parameters = array('id' => $status_id);
$retweet = $connection->post('statuses/retweet', $parameters);
Gets an error response of "not found." I'm not sure what's not found - the id of the tweet that I'm trying to retweet, or the method I'm calling (statuses/retweet). I'm passing valid ID's through the request (I can find them on Twitter), and so on. Any ideas?
Here's the documentation:
http://dev.twitter.com/doc/post/statuses/retweet/:id
I've also tried:
$parameters = array('id' => $status_id);
$retweet = $connection->post('statuses/retweet/', $parameters);
$parameters = array('id' => $status_id);
$retweet = $connection->post('statuses/retweet/:', $parameters);
and...
$retweet = $connection->post('statuses/retweet/:123456.json');
With either null responses (??) or the same enigmatic "not found."
$retweet = $connection->post('statuses/retweet/123456');
:id is a variable syntax that similar to PHP's $id so you replace it in its entirety with the value.
$parameters is only used when the key value pairs are getting added as URL parameters like ?key=value not in the URL path.
The format is automatically handled by the library so you should not include .json manually.
Another tip on this issue is to reference "id_str" rather than the "id" as the "id" integer is sometimes wrong.

Resources