I tried to send an SMS from my PHP website to a mobile phone using the BulkSMS API. I used the code below, but I don't know what went wrong. I didn't receive the SMS and nothing is showing up in the browser, just a blank page .
<?php
$url = 'http://bulksms.vsms.net/eapi/submission/send_sms/2/2.0';
$to=some number;
$username = 'usernme';
$password = 'password';
$sender='BBMS';
$msisdn = '+968'.$to;
$content="Test";
function do_post_request($url, $data, $optional_headers = 'Content-type:application/x-www-form-urlencoded'){
$params = array('http' => array(
'method' => 'POST',
'content' => $data
));
if ($optional_headers !== null) {
$params['http']['header'] = $optional_headers;
}
$ctx = stream_context_create($params);
$fp = #fopen($url, 'rb', false, $ctx);
if (!$fp) {
throw new Exception("Problem with $url, $php_errormsg");
}
$response = #stream_get_contents($fp);
if ($response === false) {
throw new Exception("Problem reading data from $url, $php_errormsg");
}
return $response;
var_dump($response);
}
?>
Related
I couldn't come up with a better title, well follow scenario:
The client sends an API request to serverA.com, based on the userid he needs to get a json file from a specific server, let's say serverC.com
So far I'm using this code on serverA.com:
function redirect_post($url, $data, $headers = null) {
$params = [
'http' => [
'method' => 'POST',
'content' => http_build_query($data)
]
];
if (!is_null($headers)) {
$params['http']['header'] = '';
foreach ($headers as $k => $v) {
$params['http']['header'] .= "$k: $v\n";
}
}
$ctx = stream_context_create($params);
$fp = #fopen($url, 'rb', false, $ctx);
if ($fp) {
echo #stream_get_contents($fp);
die();
} else {
// Error
throw new Exception("Error loading '$url', $php_errormsg");
}
}
redirect_post('https://serverC.com', $_POST);
it works but I guess serverC.com sends the file to serverA.com first which then sends it to the client? If it's not the case and the user get's the content directly from serverC.com then how could I test it/make sure that that's the case?
But if it's not the case then is what I need even possible?
For our new reporting tool we are using Tableau, however we want our users to simply login our website in order to see the info.
On the server side of tableau everything seems to be setup correctly, however I can't seem to get any data back from Tableau on my website. Sadly I'm not getting any errors back either. I'm no backend expert, I know some basic PHP, so i'm probably overlooking something here. Any help would be apperciated!
It might be important to note that our website is SSL secure, I don't know how far this impacts this.
So this is what I'm loading after the user has succesfully logged into our system:
$server = "our server ip";
$view_url = "tableau view url";
$user = 'admin';
function get_trusted_url($user,$server,$view_url) {
$params = ':embed=yes&:toolbar=yes';
$ticket = get_trusted_ticket($server, $user, $_SERVER['REMOTE_ADDR']);
if (strcmp($ticket, "-1") != 0) {
return "http://$server/trusted/$ticket/$view_url?$params";
}
else
return 0;
}
:
function get_trusted_ticket($wgserver, $user, $remote_addr) {
$params = array(
'username' => $user,
'client_ip' => $remote_addr
);
return do_post_request("https://$wgserver/trusted", $params);
}
:
function do_post_request($url, $data, $optional_headers = null)
{
$params = array('http' => array(
'method' => 'POST',
'content' => http_build_query($data)
));
if ($optional_headers !== null) {
$params['http']['header'] = $optional_headers;
}
$ctx = stream_context_create($params);
$fp = #fopen($url, 'rb', false, $ctx);
if (!$fp) {
throw new Exception("Problem with $url, $php_errormsg");
}
$response = #stream_get_contents($fp);
if ($response === false) {
throw new Exception("Problem reading data from $url, $php_errormsg");
}
return $response;
}
And this is where I try to show the data:
<script type="text/javascript" src="http://IP/javascripts/api/viz_v1.js"></script>
<object class="tableauViz" width="1519" height="693" style="display:none;">
<param name="path" value="<?php echo get_trusted_url($user,$server,$view_url)?>" />
</object>
Trusted Authentication involves requesting a ticket from Tableau Server and then redeeming that ticket. Troubleshooting any issues that are occurring therefore boils down to determining which part of that process is failing, and then fixing the problem.
As a first step, I would suggest running through the Troubleshoot Trusted Authentication page.
That said, I noticed that your script used to show the data is pulling the viz_v1.js file over http and not https. Was that a typo?
I'm trying to access a list of most recent comments on Stack Overflow using this PHP:
<?php
function do_post_request($url, $data, $optional_headers = null)
{
$params = array('http' => array(
'method' => 'POST',
'content' => $data
));
if ($optional_headers !== null) {
$params['http']['header'] = $optional_headers;
}
$ctx = stream_context_create($params);
$fp = #fopen($url, 'rb', false, $ctx);
if (!$fp) {
throw new Exception("Problem with $url, $php_errormsg");
}
$response = #stream_get_contents($fp);
if ($response === false) {
throw new Exception("Problem reading data from $url, $php_errormsg");
}
return $response;
}
echo do_post_request("https://api.stackexchange.com/2.1/comments?order=desc&sort=creation&site=stackoverflow", "");
?>
However, when I run it, I get this error message:
$ php index.php
PHP Notice: Undefined variable: php_errormsg in /var/www/secomments/index.php on line 14
PHP Fatal error: Uncaught exception 'Exception' with message 'Problem with https://api.stackexchange.com/2.1/comments?order=desc&sort=creation&site=stackoverflow, ' in /var/www/secomments/index.php:14
Stack trace:
#0 /var/www/secomments/index.php(22): do_post_request('https://api.sta...', '')
#1 {main}
thrown in /var/www/secomments/index.php on line 14
Does anyone have any ideas as to what might be causing this, and what one would do to fix it?
The API, in this situation, should be called from the method get.
If you do visit the API at this link: https://api.stackexchange.com/2.1/comments?order=desc&sort=creation&site=stackoverflow
You are presented with a nice JSON with all the information you want.
If instead, you fix up the post params:
$params = array('http' => array(
'method' => 'POST',
'content' => $data,
'header' => 'Content-Length: ' . strlen($data)
));
You are shown this instead:
{"error_id":404,"error_name":"no_method","error_message":"this method cannot be called this way"}
Hopefully you know that you can simply just use file_get_contents to contact the API with the traditional get.
$json = json_decode(file_get_contents("https://api.stackexchange.com/2.1/comments?order=desc&sort=creation&site=stackoverflow"), true);
As the error said php_errormsg is your problem.
You have to make sure errors are tracked before assuming php_errormsg is defined.
you should turn the track_errors configuration option on before trying to get the value of php_errormsg, example :
<?php
function do_post_request($url, $data, $optional_headers = null)
{
error_reporting(E_ALL);
ini_set('track_errors', 1);
global $php_errormsg;
$params = array('http' => array(
'method' => 'POST',
'content' => $data
));
if ($optional_headers !== null) {
$params['http']['header'] = $optional_headers;
}
$ctx = stream_context_create($params);
$fp = #fopen($url, 'rb', false, $ctx);
if (!$fp) {
throw new Exception("Problem with $url, $php_errormsg");
}
$response = #stream_get_contents($fp);
if ($response === false) {
throw new Exception("Problem reading data from $url, $php_errormsg");
}
return $response;
}
echo do_post_request("https://api.stackexchange.com/2.1/comments?order=desc&sort=creation&site=stackoverflow", "");
EDIT:
Also in the php.ini conficuration file you can set :
track_errors = On
IMPORTANT: From the PHP website
While $php_errormsg is a global, it is not a superglobal.
You'll have to qualify it with a global keyword inside a function.
<?php function checkErrormsg() {
global $php_errormsg;
#strpos();
return $php_errormsg; } ?>
EDIT 2 : USE of Global variable = bad practice
link
link
I have one question, can i upload file to another server without curl..
Because not every server have CURL ...
Thanks...
Yes it is possible using pure PHP fopen together with stream_context_create. The following example comes from the online PHP manual (http://php.net/manual/en/function.stream-context-create.php):
function do_post_request($url, $postdata, $files = null)
{
$data = "";
$boundary = "---------------------".substr(md5(rand(0,32000)), 0, 10);
//Collect Postdata
foreach($postdata as $key => $val)
{
$data .= "--$boundary\n";
$data .= "Content-Disposition: form-data; name=\"".$key."\"\n\n".$val."\n";
}
$data .= "--$boundary\n";
//Collect Filedata
foreach($files as $key => $file)
{
$fileContents = file_get_contents($file['tmp_name']);
$data .= "Content-Disposition: form-data; name=\"{$key}\"; filename=\"{$file['name']}\"\n";
$data .= "Content-Type: image/jpeg\n";
$data .= "Content-Transfer-Encoding: binary\n\n";
$data .= $fileContents."\n";
$data .= "--$boundary--\n";
}
$params = array('http' => array(
'method' => 'POST',
'header' => 'Content-Type: multipart/form-data; boundary='.$boundary,
'content' => $data
));
$ctx = stream_context_create($params);
$fp = fopen($url, 'rb', false, $ctx);
if (!$fp) {
throw new Exception("Problem with $url, $php_errormsg");
}
$response = #stream_get_contents($fp);
if ($response === false) {
throw new Exception("Problem reading data from $url, $php_errormsg");
}
return $response;
}
//set data (in this example from post)
//sample data
$postdata = array(
'name' => $_POST['name'],
'age' => $_POST['age'],
'sex' => $_POST['sex']
);
//sample image
$files['image'] = $_FILES['image'];
do_post_request("http://example.com", $postdata, $files);
It is possible to use ftp_put:
define("LOCAL_FILE","PATH_TO_LOCAL_FILE");
define("FTP_ADDRESS","ftp.domain.com");
define("FTP_FILE","PATH_TO_REMOTE_FILE");
define("FTP_USERNAME","USERNAME");
define("FTP_PASSWORD","PASWORD");
$conn = ftp_connect(FTP_ADDRESS);
$login = ftp_login($conn, FTP_USERNAME, FTP_PASSWORD);
ftp_put($conn, FTP_FILE, LOCAL_FILE, FTP_ASCII);
ftp_close($conn);
Whereas, the ftp domain would supply you with the location to the ftp file as well as the username and password.
I am trying to upload video on cdn server(hwcdn.net server) through api, but getting the following error .
" 0470 Invalid file name (A-Z,a-z,0-9,-,_,',.) "
//PHP example code for calling an action:
$action = "UF"; //get system info
$user = "xxxxxx"; //my StrikeTracker user name
$pass = "xxxxxx"; //my StrikeTracker password
$apiKey ="xxxxxxxxxxxxxxxxxxxx"; //my API Key
$md5pass = md5($pass);
$queryString = "action=$action&user=$user&key=$apiKey&password=$md5pass";
$token =md5($queryString);
$directory="/folder path/";
$Filedata="testingvideo.flv";
echo $apiQuery = "action=$action&user=$user&token=$token&directory=$directory&Filedata=$Filedata";
$host="http://st-api.hwcdn.net/index.php";
echo do_post_request($host,$apiQuery);
function do_post_request($url, $data, $optional_headers = null)
{
$params = array('http' => array(
'method' => 'POST',
'content' => $data
));
if ($optional_headers !== null) {
$params['http']['header'] = $optional_headers;
}
$ctx = stream_context_create($params);
$fp = #fopen($url, 'rb', false, $ctx);
if (!$fp) {
throw new Exception("Problem with $url, $php_errormsg");
}
$response = #stream_get_contents($fp);
if ($response === false) {
throw new Exception("Problem reading data from $url, $php_errormsg");
}
return $response;
}
I think this is wrong :
$directory="/folder path/";
The space is not allowed in hwcdn paths.
The answer is in the error: the filename contains illegal chars, you have to sanitize it removing everything that is not A-Z,a-z,0-9,-,_,',..
You can use preg_replace.