How to use post method for xml data using php - php

I have a api which sends messages to the people ,and the api in the form of url ,which takes user login details and the phone number as input for the url .At a time a time I am able to send 10 sms ,but I want to implement it for bulk sms .
I am trying this code ,but it's working only for 10sms,if we are providing more than 10 mobile numbers it's not working ,can anyone suggest me what mistake I making in my code
Code:
$numbersarray=explode(",",$numbers); //stores numbers as array
/*XML API by Aditya*/
$numbers_xml_string=""; //stores XML string of numbers and message
foreach($numbersarray as $num){
$numbers_xml_string.="<Message><To>".$num."</To><Text>".$text."</Text></Message>";
}
//XML string to be encoded
$xmlstring="<SmsQueue><Account><User>".$user."</User><Password>".$password."</Password></Account><MessageData><SenderId>".$api_id."</SenderId><Gwid>1</Gwid><DataCoding>0</DataCoding></MessageData><Messages>".$numbers_xml_string."</Messages></SmsQueue>";
$xmlstring=urlencode($xmlstring);//encode the string
//prepare URL
$url="http://login.smsgatewayhub.com/xmlapi/pushsms.aspx?data=".$xmlstring;
echo "url".$url;
$options = array(
'http' => array(
'header' => "Content-type: application/x-www-form-urlencoded\r\n",
'method' => 'POST',
'content' => http_build_query($numbersarray),
),
);
$context = stream_context_create($options);
$result = file_get_contents($url, false, $context);
var_dump($result);

There may be two reasons according to your sample code:
First you may only have permissions to send 10 SMS messages.
Second you can only send 10 messages every a certain time. In this case you should know what is the time you should wait and run your script each this time to send sms to the remaining numbers.

Related

Unable to fetch response from file_get_contents using PHP

Gurus,
I am trying to fetch tracking details using the austpost.com.au website for which I have written the below code however, it doesn't seem to fetch anything.
$austpost_url = 'https://digitalapi.auspost.com.au/shipmentsgatewayapi/watchlist/shipments/99702032243801004670904';
$options = array(
'method' => 'get',
'contentType' => 'application/json',
'muteHttpExceptions' => true,
'headers' => array ('Accept' => 'application/json, text/plain, */*',
'Accept-Encoding' => 'gzip, deflate, br',
'AP_CHANNEL_NAME' => 'WEB_DETAIL',
'api-key' => 'd11f9456-11c3-456d-9f6d-f7449cb9af8e',
'Connection' => 'keep-alive',
'Origin' => 'https://auspost.com.au',
'Referer' => 'https://auspost.com.au/mypost/track/'),
);
$context = stream_context_create($options);
$html = file_get_contents($austpost_url, false, $context);
echo $html;
Direct Tracking Link: https://auspost.com.au/mypost/track/#/details/99702032243801004670904
I found this by going to the NETWORK tab in Chrome and figured out which request is loading the tracking details. Basically I am just after getting the status of the shipment. Snapshot below:
Any help in this would be much appreciated as I am using PHP to drive my code where I want to update the WordPress posts statuses based on the shipment status (that I can handle) however, stuck here on how I can get this shipment status before I move on.
It is because you need a valid recaptcha token and proof that you are not a robot. Won't be that simple, as you think. The easiest, but paid method, would be to use some captcha solving companies, like https://2captcha.com (not a sponsor of that answer) and their PHP libs.
If you want to use their api, you need to auth with username and password via basic auth. More details in here: https://developers.auspost.com.au/apis/shipping-and-tracking/reference/track-items
First check the fopen is on or off , add the phpinfo(); code in main file and check this .
Otherwise use the Curl function instead of file_get_contents();

Errors using MailChimp API adding email address to list

I am trying to add email addresses to my mailchimp mailing list but am getting an error saying
Access Denied
You don't have permission to access "http://us9.api.mailchimp.com/3.0/lists/######/members/" on this server.
Reference #18.1e89655f.1546511382.213522b5
I have just created the API key and haven't done much with MailChimp before. I think the list ID is correct, I just grabbed it from the URL of my actual list in a browser https://us9.admin.mailchimp.com/lists/members/?id=######
<?php
$email = '#########.##';
$authToken = '#######################-us9';
$email_list = '######';
$postData = array(
"email_address" => "$email",
"status" => "subscribed",
"merge_fields" => array(
"NAME"=> "",
"PHONE"=> "")
);
$ch = curl_init('https://us9.api.mailchimp.com/3.0/lists/'.$email_list.'/members/');
curl_setopt_array($ch, array(
CURLOPT_POST => TRUE,
CURLOPT_RETURNTRANSFER => TRUE,
CURLOPT_HTTPHEADER => array(
'Authorization: apikey '.$authToken,
'Content-Type: application/json'
),
CURLOPT_POSTFIELDS => json_encode($postData)
));
$response = curl_exec($ch);
var_dump($response);
?>
The API list ID is not the same as the browser one. (Don't ask me why). To find out the API list ID, use the API Playground. https://developer.mailchimp.com/
I think the IP of your server is blacklisted for Mailchimp. You should contact Mailchimp and ask if the server IP is blacklisted for them.
It was a transient error for me, which went away after a couple of minutes. If it doesn't then somehow Akamai has probably blacklisted you and you need to contact Mailchimp (as Akamai does not handle Mailchimp specific requests directly).
If you look in the response headers, it says Server: AkamaiGHost, which I assume is the proxy Mailchimp is using. Also the response is text/html, which does not match Mailchimp's own documented error formats–another sign that it is the proxy blocking you and not Mailchimp itself.

GET works but POST doesn't for silent login

We have 2 systems one in PHP and one in asp.net which currently require separate logins even though both use the same username/password information.
As a quick (and temporary) fix I thought I might be able to amend the PHP login page to pass the login details to the asp.net page behind the scenes so the user could be logged in automatically to both systems rather than having to enter their credentials again when navigating to a different page.
This works fine when I use a GET to pass the parameters but of course the GET parameters including the password get stored in the server log which is not a good idea.
So I would like to use POST instead, but I can't get it to work. I used the CURL-less method in How do I send a POST request with PHP? (code below) and it appears to work but the asp.net login process creates secure cookies and while they are stored if I GET the page they are not being stored when I POST to it instead.
$url = 'https://server.com/path';
$data = array('key1' => 'value1', 'key2' => 'value2');
// use key 'http' even if you send the request to https://...
$options = array(
'http' => array(
'header' => "Content-type: application/x-www-form-urlencoded\r\n",
'method' => 'POST',
'content' => http_build_query($data),
),
);
$context = stream_context_create($options);
$result = file_get_contents($url, false, $context);
var_dump($result);
I have tried something similar with CURL in the past so I know that CURL creates it's own browser context but I thought a straight POST request would work, however it seems to be behaving much the same as CURL.
Is there some way to get this to work or do I have a fundamental misunderstanding of how POST works? If so could somebody point me at a good explanation.

php- how to do async. processing to add tasks in queue?

I have an SMS API that enables to send text messages. I want to use it to send SMSs in bulk.
User can enter upto 30,000 numbers at once and send SMSs. What I am using might be a really bad approach:
foreach ($targets as $target) {
sendSms($target,$text,$extra_parms):
}
It takes 10 minutes to process for 10,000 requests (SMSs) and it's too much. What I want is when a user should click 'Send' button, he should get a message like:
"Your SMS(s) have been added to queue to be sent"
And all the SMSs should be sent in background. How can I do that?
Thanks for the help.
Follow below Process:
Instead of directly calling API, Insert all the data into your
database.
Once numbers and text added in your DB, Show user message
"Your SMS(s) have been added to queue to be sent"
User Background process which will take data from DB and call asynchronous API requests using http://www.php.net/manual/en/function.curl-multi-exec.php
Update/Delete processed API records in database so next time you can fetch only data which are not processed.
Please note: Normal CURL request use synchronous and it wait for for response which make delay.
Create queue i.e. in database and put all your SMS actions there. Create separate script that will be sending SMSes (in batches, as whole whatever) and have it periodically started (i.e. using Curl) to deal with your queue.
That's what I use:
function fast_post($url,$data){
ignore_user_abort(true);
$ch = curl_init();
$defaults = array(
CURLOPT_POST => 1,
CURLOPT_HEADER => 0,
CURLOPT_URL => $url,
CURLOPT_FRESH_CONNECT => 1,
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_FORBID_REUSE => 1,
CURLOPT_TIMEOUT => 1,
CURLOPT_POSTFIELDS => http_build_query($data)
);
curl_setopt_array($ch, $defaults);
curl_exec($ch);
curl_close($ch);
}
Note that in this function $data is an array.

Salesforce creates record but responds with a 405

I've written a Wordpress Plug-in that interacts with Salesforce via the REST API. It successfully gets an Instance URL and an Authentication Token using a username/password.
I'm able to submit queries and create objects using wp_remote_post with GET and POST respectively.
However, when creating objects, though successfully created in the Salesforce instance, I get the following in response to my POST:
{"message":"HTTP Method 'POST' not allowed. Allowed are HEAD,GET,PATCH,DELETE","errorCode":"METHOD_NOT_ALLOWED"}
Using the same json body content from these requests, I am able to submit and create via the Salesforce Workbench with no problems at all. I get a proper response that looks like this:
{
"id" : "003E000000OubjkIAB",
"success" : true,
"errors" : [ ]
}
Is there something in the Headers that I'm sending that Salesforce only partially disagrees with? Here are some other arguments that are getting sent as a result of using wp_remote_post - http://codex.wordpress.org/HTTP_API#Other_Arguments
Here's the php code that's calling it:
$connInfo['access_token'] = get_transient('npsf_access_token');
$connInfo['instance_url'] = get_transient('npsf_instance_url');
$url = $connInfo['instance_url'] . $service;
$sfResponse = wp_remote_post($url, array(
'method' => $method,
'timeout' => 5,
'redirection' => 5,
'httpversion' => 1.0,
'blocking' => true,
'headers' => array("Authorization" => "OAuth ". $connInfo['access_token'], "Content-type" => "application/json"),
'body' => $content,
'cookies' => array()
)
);
The $content is being encoded via json_encode before it gets to this point.
Update:
It is specific to one of the extra CURL options being sent by the WP_Http_Curl class. I haven't yet narrowed down which one Salesforce is having a problem with.
The solution is disable redirection in the request. You have it as 5 (the default) -- it needs to be set to 0 for this to work.
The initial request works but Salesforce sends a location header as a part of the response (the URL of the newly created object). WordPress thinks it is being told that the URL moved and that it should try again at this new URL. The response you're seeing is the result of that second request to the actual object you just created. That URL doesn't accept POST requests apparently.
It's a bit odd for Salesforce to be sending such a header, but there's also some discussion going on on the WordPress side that WordPress shouldn't follow location headers for non-301/302 responses which would solve this.
Thanks for posting this by the way. You update made me start debugging WP_Http_Curl which made me realize it was actually making a second HTTP request.

Categories