Let's start with example.
if I have fixed form parameter (name , email , phone) then Guzzle Post method code would be like this :
public function test(Request $request){
$client = new \GuzzleHttp\Client();
$url = www.example.com
$res = $client->post($url.'/article',[
'headers' => ['Content-Type' => 'multipart/form-data'],
'body' => json_encode([
'name' => $request['name'],
'email' => $request['email'],
'write_article' => $request['article'],
'phone' => $request['phone'],
])
]);
}
Above code is working perfectly.
But when don't have fix form parameter then How to send data using Guzzle ?
Foe example first time when I have submited form I have name , email , phone field. next time may be fields would be name , email , phone , father_name , mother_name, interest , etc.. . next time may be it would be name , email , father name
So How to work with this dynamic form field situation ?
Try this:
public function test(Request $request)
{
$client = new \GuzzleHttp\Client();
$url = 'www.example.com';
$body = [];
// exceptions, for when you want to rename something
$exceptions = [
'article' => 'write_article',
];
foreach ($request as $key => $value) {
if (isset($exceptions[$key])) {
$body[$exceptions[$key]] = $value;
} else {
$body[$key] = $value;
}
}
$res = $client->post($url.'/article',[
'headers' => ['Content-Type' => 'multipart/form-data'],
'body' => json_encode($body)
]);
}
Related
I am using telegram library
php telegram bot sdk
And I want to send some photos in message using sendMediaGroup
Like this:
$reply = "*photos*";
$telegram->sendMediaGroup([
'chat_id' => $chat,
'media' => [
['type' => 'photo', 'media' => 'attach://photo1' ],
['type' => 'photo', 'media' => 'attach://photo2' ],
],
'photo1' => InputFile::create(file_get_contents("https://".$_SERVER['SERVER_NAME']."/newbot/screens/ctry/en1.jpg")),
'photo2' => InputFile::create(file_get_contents("https://".$_SERVER['SERVER_NAME']."/newbot/screens/ctry/en2.jpg")),
'caption'=> $reply,
'parse_mode' => 'markdown'
]);
I used without json_encode and file_get_contents, but it doesn't worked too
Why do you use that library? Here's the normal solution:
$token = ' TOKEN ';
$website = 'https://api.telegram.org/bot'.$token;
$update = file_get_contents('php://input');
$update = json_decode($update, TRUE);
$message = $update['message']['text'];
$id = $update['message']['from']['id'];
$name = $update['message']['from']['first_name'];
$surname = $update['message']['from']['last_name'];
$username = $update['message']['from']['username'];
function sendPhoto($id, $photo, $text = null){
GLOBAL $token;
$url = 'https://api.telegram.org/bot'.$token."/sendPhoto?chat_id=$id&photo=$photo&parse_mode=HTML&caption=".urlencode($text);
file_get_contents($url);
}
I also put there some useful variable and the chance to put a caption (don't give that parameter if you don't want one)
how are you ?
I'm using Guzzle to send post method but when i tried to send the post method with variable getting error so i tried to convert it to sting but still same
this is my current code
if $ mobile value is 55454545445 , them the 'numbers' => "{$mobile}" will be different not "55454545445"
$user = Auth::user();
$mobile = $user->mobile;
$client = new Client();
$booking = Booking::where('id', '=', e($id))->first();
if($booking)
{
$booking->booking_status_id = 3;
$booking->save();
$client = new Client([
'headers' => [ 'Content-Type' => 'application/json' ]
]);
$data = array('userName' => "test",
'apiKey' => "1",
'numbers' => "{$mobile}",
'userSender' => "sender",
'msg' =>'msg',
'msgEncoding' => "UTF8",);
$dataJson = json_encode($data);
$response = $client->post('https://www.test.test',
['body' => $dataJson]
);
i used this
"0{$mobile}"
instead of
"{$mobile}"
and its work
I'm creating a message on the forum using the rest api:
$point = '/forums/topics';
$endpoint = $url. 'api'. $point. '?key='. $apiKey;
$options = [
'form_params' => [
'forum' => 3,
'title' => $request->title,
'post' => $request->body,
'author' => 1
]
];
$client = new Client();
$request = $client->post($endpoint, $options);
From this API I would need to create the ID to be able to store it in $article->id_topic
but I can't find out how I can go back to the ID of the item created with POST.
I've already done some tests, failing, here are some:
$article->id_topic = $request->id;
and with json_decode
$id_topic = json_decode($request);
$idt = $request->id;
$articoli->id_topic = $idt;
$id_topic = json_decode($request->getBody());
$article->id_topic = $id_topic->id;
$article->save();
I have code that retrieves everything I need (to include user id tags) yet from var umping my responses, I see that my object_attachment is creating this error.
My code:
public function newPost(UserModel $user){
$this->fb->setDefaultAccessToken($user->accounts->where("network","facebook")->first()->access_token);
$message = 'Look at this amazing photo';
$photoUrl = 'http://weneedfun.com/wp-content/uploads/2016/11/Amazing-Pictures-4.jpg';
$photoRequester = $this->fb->request('POST', 'me/photos', ['url' => $photoUrl, 'published' => 'false']);
$photoResponseBodyId = $this->requestResponse($photoRequester)->getDecodedBody()['id'];
$friendsTagged = ['25235235', '52525222'];
$request = $this->fb->request('POST','me/feed', ["message" => $message, "tags"=> $friendsTagged, "object_attachment" => $photoResponseBodyId]);
$responseBody = $this->requestResponse($request)->getDecodedBody();
var_dump($request);
var_dump($responseBody);
die();
/*return $responseBody;*/
}
When I remove the above request and leave it as:
$request = $this->fb->request('POST','me/feed', ["message" => $message, "tags"=> $friendsTagged]);
it works just fine.
what am I doing wrong fellow stackers?
Ok, don't ask me why but FB wants all returning values in an array (even if there's 1 image.)
Here's the request working:
$request = $this->fb->request('POST','me/feed', [
'message' => $message,
'tags' => $friendsTagged,
'attached_media[0]' => '{"media_fbid":"'.$photoResponseBodyId.'"}'
]);
I am using sync (local driver) for pushing up a queue in a update method of EmailCampaignController, which uses another method of the same controller named emailQueue
like this
Queue::push('EmailNewsletterController#emailQueue', array('campaign_id' => $campaign_id));
The emailQueue uses a foreach loop which runs correctly for once after that it gives error as if the $campaign_id is undefined
here is the emailQueue method
public function emailQueue($job, $data) {
// Queue Starts here
$emailCampaign = EmailCampaign::find($data['campaign_id']);
$emailCampaign->status = 'In Progress';
$emailCampaign->last_activity = Carbon::now();
$emailCampaign->save();
$data = $emailCampaign->emailCampaignNewsletter;
$contacts = $emailCampaign->contactList->contacts;
foreach ($contacts as $contact) {
$emailBody = [
'message' => [
'subject' => $data['email_title'],
'html' => $data['email_body'],
'from_email' => $data['from_email'],
'to' => [['email' => $contact['email_address']]]
]
];
$response = Mandrill::request('messages/send', $emailBody);
EmailCampaignRecord::create([
'email_campaign_id' => $data['campaign_id'],
'mandrill_email_id' => $response[0]->_id,
'status' => $response[0]->status,
'to_email' => $contact['email_address']
]);
$contact->last_activity = Carbon::now();
$contact->save();
}
$emailCampaign->status = 'Sent';
$emailCampaign->save();
$job->delete();
// Ends here
}
What am I doing wrong here? why is it not working like a normal loop ?
The problem was with email_campaign_id to be null because $data['campaign_id'] was null the correct foreign key was $data['email_campaign_id'] that's what stopped the process - I should have tested it before putting it in the queue
after changing the code
EmailCampaignRecord::create([
'email_campaign_id' => $data['campaign_id'],
'mandrill_email_id' => $response[0]->_id,
'status' => $response[0]->status,
'to_email' => $contact['email_address']
]);
to
EmailCampaignRecord::create([
'email_campaign_id' => $data['email_campaign_id'],
'mandrill_email_id' => $response[0]->_id,
'status' => $response[0]->status,
'to_email' => $contact['email_address']
]);
the problem was solved