PHP-cURL --> follow a ruby redirection - php

I am using trying to extract the wiki documentation from a page on my website (redmine). I successfully retrieved the redirect to the login page, send post username and password field, but then I arrive on a page with a ruby redirect from redmine and I can't go further. The ruby code in the file "response.rb" of redmine is:
def redirect(url, status)
self.status = status
self.location = url.gsub(/[\r\n]/, '')
self.body = "<html><body>You are being redirected.</body></html>"
end
I don't kbow enough about ruby to modify redmine's code, so is there a way to do this in php?

curl_setopt($curlHandle, CURLOPT_FOLLOWLOCATION, true);
Would tell cURL to follow any properly configured redirects (anything that sends the Location: header).

Found an alternate solution!
IF anybody else has that problem with redmine or any other redirect:
Use cURL to login first
Save your session to a cookie
Use cURL to get your secured page that needs authentification.
<?php
$tempFilename = tempnam("/tmp", "COOKIE");
// Login
$curlHandle = curl_init("http://bob.com/login");
curl_setopt($curlHandle, CURLOPT_COOKIEJAR, $tempFilename);
curl_setopt($curlHandle, CURLOPT_RETURNTRANSFER, true);
$dataArray = array(
"username" => "bob",
"password" => "password"
);
curl_setopt($curlHandle, CURLOPT_POSTFIELDS, $dataArray);
$output = curl_exec($curlHandle);
// Getting the wiki
$curlHandle = curl_init("http://bob.com/wiki");
curl_setopt($curlHandle, CURLOPT_COOKIEFILE, $tempFilename);
curl_setopt($curlHandle, CURLOPT_RETURNTRANSFER, true);
$output = curl_exec($curlHandle);
echo $output;
?>

Related

Is there a way of sending/receiving SMS(s) from a Huawei B315 LTE Router with PHP?

I tried to access the router's interface with curl but I am a new coder and I struggled to make this happen.
<?php
$data = array( "User name" => "admin", "password" => "*****", "form" => "submit" );
// Initialized the cURL session with Target URL
$ch = curl_init("192.168.8.1");
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, "true"); // Allow redirections
curl_setopt($ch, CURLOPT_POST, true); // We are making a post request
/*curl_setopt($ch, CURLOPT_POSTFIELDS, $data);*/
curl_setopt($ch, CURLOPT_COOKIEJAR, 'cookie.txt');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, "true");
$result = curl_exec($ch);
curl_close($ch);
echo $result;
?>
I know this answer is a few years late, but for anyone still searching for answers to this, there is a PHP script on Github that allows for sending and receiving SMS via the Huawei-B315s-22 HTTP API. See https://github.com/Zili0/Huawei-B315s-22
I noticed that it calls the following endpoints:
/api/webserver/SesTokInfo
/api/user/state-login
/api/monitoring/status
/api/user/authentication_login
/api/device/information
/api/net/current-plmn
/api/monitoring/traffic-statistics
/api/sms/sms-count
/api/sms/sms-list
/api/wlan/multi-basic-settings
/api/device/control
/api/user/logout
There's also two other similar scripts I found, both written in Python that do similar things. Hopefully one of these will provide the kind of thing you need:
https://github.com/shaan1337/huawei_b315s22_sms
https://github.com/chenwei791129/Huawei-LTE-Router-SMS-to-E-mail-Sender

How to access odata that produced by Nav webservice into html layout using PHP

I try to get odata produced by a navision webservice. When I directly access the url given using chrome browser, the page asks for user name and password and then chrome shows xml data as expected.
But when i use a PHP script, it always returns "1".
My code looks like this:
$url = 'http://103.7.1.182:14048/DynamicsNAV71-6/OData/Company(\'Unit%20G%205\')/Item_Master_on_hand_no_Desc';
$login = 'Gem-gae\senzo1:Bsbsenzo2018';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_USERPWD, $login);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type:application/x-www-form-urlencoded',
'Content-Length: ' . strlen(1))
);
$output = curl_exec($ch);
curl_close($ch);
echo $output;
Is there any mistakes with that code ?
Thanks for your help
There's no mistake on your code, it successfully connects to the external server via cURL and retrieves the data.
The value in $output (if you do a var_dump) is true (that's why you see 1 if you echo it).
Said that, the issue is on the server you are contacting (103.7.1.182:14048).
You probably need to send some data or something, I can't tell you exactly what because I don't know what is in that server.
To send data, you can add a curl_setopt line:
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
Where $data is an array.

Authenticating Cookies with PHP and Curl

I am writing code against the Space-Track API, and the curl request is failing to authenticate. My code is as follows:
$curl = curl_init();
$url = "https://www.space-track.org/basicspacedata/query/class/tle/EPOCH/%3C1969-12-18/NORAD_CAT_ID/4/orderby/EPOCH%20desc/limit/1/format/3le/metadata/true";
$c = curl_init('https://www.space-track.org/auth/login?identity=XXX&password="XXX"');
curl_setopt($c, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($c, CURLOPT_COOKIEJAR, 'cookies.txt');
$page = curl_exec($c);
curl_close($c);
$c = curl_init($url);
curl_setopt($c, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($c, CURLOPT_COOKIEFILE, 'cookies.txt');
$page = curl_exec($c);
curl_close($c);
echo $page;
The response received is: "You must be logged in to complete this action"
The authentication is apparently successful, however, as the cookies.txt file is filled with an authentication message.
I have read a number of tutorials on curl authentication, and I have also tried other users' implementations of the space-track API in php (eg) but have not managed to make these work with my request:
https://www.space-track.org/basicspacedata/query/class/tle/EPOCH/%3C1969-12-18/NORAD_CAT_ID/4/orderby/EPOCH%20desc/limit/1/format/3le/metadata/true
The request works in the browser (direct access) when authenticated and via terminal, but not when run in the script. Direct browser access returns this correct response:
0 EXPLORER 1
1 00004U 58001 A 69351.29911760 .00162519 +00000-0 +00000-0 0 9991
2 00004 033.1571 316.5155 0244636 013.0841 347.6023 15.32472396567852
Any assistance in correctly authenticating and receiving this response via PHP gratefully received.

PHP: cURL or header - faster and better way to send cross domain data and redirect

I create one small API what collect contact form informations from one website and store in database on another website in managment application what I also build.
On website where is contact form is this code:
// collect all fields
$save = array(
'oAuth'=>'{KEY}',
'secret'=>'{SECRET-KEY}',
'category'=>'Category',
'name'=>'Jon Doe',
'email'=>'jon#doe.com',
'phone'=>'123 456 7890',
// ... etc. other fields
);
// made GET request
$fields=array();
foreach($save as $key=>$val){
$fields[]=$key."=".rawurlencode($val);
}
// Set cross domain URL
$url='http://api.mydomain.com/';
// Send informations in database
$cURL = curl_init();
curl_setopt($cURL,CURLOPT_URL, $url.'?'.join("&",$fields));
curl_setopt($cURL, CURLOPT_RETURNTRANSFER, true);
curl_setopt($cURL, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($cURL, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($cURL, CURLOPT_CONNECTTIMEOUT, 5);
curl_setopt($cURL, CURLOPT_TIMEOUT, 2);
curl_setopt($cURL, CURLOPT_HTTPHEADER, array('Accept: application/json'));
$output=curl_exec($cURL);
curl_close($cURL);
usleep(1000);
// redirect
header("Location: http://somemysite.com/thank-you-page");
session_destroy();
exit;
My question is do to use cURL like now for this or to use header() function to send GET?
I ask because I not use output here and sometimes redirection start before cURL finish request.
What is faster way to send GET info and not lost data?
The redirection will not start before cURL finishes. What may happen is that cURL fails to load you remote page, and you don't handle that error. You should add something like this:
if($output === false || curl_getinfo($cURL, CURLINFO_HTTP_CODE) != 200) {
// Here you handle your error
}
If you are looking for an alternative to load remote webpages, you can set allow_url_fopen to true in your php.ini and then use this simple function instead of cURL:
$output = file_get_contents($url.'?'.join("&",$fields));

php+curl to send a post request with fields

trying to send post request to api, to get an image back.
example url:
https://providers.cloudsoftphone.com/lib/prettyqr/createQR.php?user=1003123&format=png&cloudid=asdasdasd&pass=123123123
the above url works fine in the browser,
the api doesnt care if the request is get/post,
result of my code is always 'invalid input'.
code:
$url='https://providers.cloudsoftphone.com/lib/prettyqr/createQR.php';
$u = rand();
$p = rand();
$fields = array(
'user'=> urlencode($u),
'pass'=> urlencode($p),
'format'=> urlencode('jpg'),
'cloudid' => urlencode('test')
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $fields);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
$output = curl_exec($ch);
curl_close($ch);
echo $output;
on a side note: is there a way to debug the request in order to see what is being sent ?
The URL provided isn't working for POST request. Here is resulting screenshot (I tried using Advance Rest Client)
However Its working perfectly with GET method. So you can continue using GET request method to generate QR code.
I agree that GET isn't much secure compare to POST method but in your case while requesting from curl user won't get to know about such URL parameters (userid, password). Because curl request will be sending from your web server and not from client/user's browser.
Later you can just output the response image you got from the api.

Categories