Want to execute url with Port number - php

Dear all please help me how can i execute following url using cURL using php?
https://telenorcsms.com.pk:27677/corporate_sms2/api/auth.jsp?msisdn=923468211085&password=Javed1212
i am tring this but no result found.
$tuCurl = curl_init();
curl_setopt($tuCurl, CURLOPT_URL, "https://telenorcsms.com.pk/corporate_sms2/api/auth.jsp?msisdn=923468211085&password=Javed1212");
curl_setopt($tuCurl, CURLOPT_PORT , 27677);
curl_setopt($tuCurl, CURLOPT_VERBOSE, 0);
curl_setopt($tuCurl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($tuCurl, CURLOPT_CONNECTTIMEOUT, 5); // 5 seconds timeout
$tuData = curl_exec($tuCurl);
curl_close($tuCurl);
echo $tuData;

It seems that your request is at the "middle" of authentication process. Perhaps, you need to consider some credentials over the request to access the needed resource.At first, set CURLOPT_VERBOSE in active mode : true(1) (to obtain the details of the request processing).Secondly, that site returns an XML response back. Use var_dump to get more details about response.Here is how it looks now:
$tuCurl = curl_init();
curl_setopt($tuCurl, CURLOPT_URL, "https://telenorcsms.com.pk/corporate_sms2/api/auth.jsp?msisdn=923468211085&password=Javed1212");
curl_setopt($tuCurl, CURLOPT_PORT , 27677);
curl_setopt($tuCurl, CURLOPT_VERBOSE, true);
curl_setopt($tuCurl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($tuCurl, CURLOPT_CONNECTTIMEOUT, 5); // 5 seconds timeout
$tuData = curl_exec($tuCurl);
curl_close($tuCurl);
var_dump($tuData);
The output:
'<?xml version="1.0" encoding="UTF-8" ?>
<corpsms>
<command>Auth_request</command>
<data>8c22d08be9bd492ea06bef31af4d62f3</data>
<response>OK</response>
</corpsms >
'

Related

Why i can't get a right response from server using Curl?

I need to get response from server using curl, but I can't.
The site: https://www.investing.com/holiday-calendar/
I can get that calendar using get request, but I need a list with custom dates. That mean I should use that datepicker. So when I press "apply" it sent post request with data I needed to get. (see the screenshots)
The DatePicker:
A post request with JSON response:
Code:
ini_set('error_reporting', E_ALL);
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
define('DIR', __DIR__);
$user_agent = isset($_SERVER['HTTP_USER_AGENT']) ? $_SERVER['HTTP_USER_AGENT'] : 'Traider';
$cookie = dirname(__FILE__).DIRECTORY_SEPARATOR.'init_cookie.txt';
$f = fopen('init_deb.txt', 'w');
$ch = curl_init();
$getUrl = 'https://www.investing.com/holiday-calendar/';
$postUrl = 'https://www.investing.com/holiday-calendar/Service/getCalendarFilteredData';
$dateFrom='2017-01-14';
$dateTo='2017-12-31';
$limit_from = 0;
$params = [
'dateFrom' => $dateFrom,
'dateTo' => $dateTo,
'county' => '',
'limit_from' => $limit_from
];
curl_setopt($ch, CURLOPT_URL, $postUrl);
curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie);
curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie);
curl_setopt($ch, CURLOPT_USERAGENT, $user_agent);
curl_setopt($ch, CURLOPT_VERBOSE, true);
curl_setopt($ch, CURLOPT_STDERR, $f);
$response = curl_exec($ch);
fclose($f);
curl_close($ch);
echo $response;
Step 1. I sent a get request, save cookies.
Step 2. I sent a post request changing $getUrl -> $postUrl. I always get the main page. Why I can't get JSON response?
after a bit of testing, the big secret is that they refuse requests that dont have the X-Requested-With:XMLHttpRequest header attached. attach that (using CURLOPT_HTTPHEADER), and you dont even need a cookie session. i guess its part of some XSS protection scheme.
working example code using hhb_curl from https://github.com/divinity76/hhb_.inc.php/blob/master/hhb_.inc.php :
<?php
declare(strict_types=1);
require_once('hhb_.inc.php');
$hc=new hhb_curl();
$hc->_setComfortableOptions();
$hc->setopt_array(array(
CURLOPT_POST=>true,
CURLOPT_POSTFIELDS=>http_build_query(array(
'dateFrom'=>'2017-01-28',
'dateTo'=>'2017-01-28',
'country'=>'',
'limit_from'=>'0'
)),
CURLOPT_HTTPHEADER=>array(
'X-Requested-With:XMLHttpRequest'
)
));
$hc->exec('https://www.investing.com/holiday-calendar/Service/getCalendarFilteredData');
hhb_var_dump($hc->getResponseBody());

Incorrect characters returned RSS/XML Feed

hoping someone out there can help. I have a small script using Curl to download a file from an rss feed.
The RSS feed validates as XML yet when I try to run my script I get the following:
‹íYsã6¶Çßó)x=5yá%Œ•=n§¸¦“^’j;=wª+´DKLK¤Š¤Ýíùôà"Ñk#™ÞªÔÕ•¸eŠàøƒƒ…‡?}_.Œ‹¤(Ó<{³‡Ü3’l’OÓlöfï“È{?ýpX”åæ*¬®’ßËʃ*/Š$«ÞìíïË*®Ò ˜äËýú·û ý=ùíÉ<βdqôƒ!ÿViµHŽª£ýri|:>6Î’djXÆ»t"oTž´¿:Üo®n¾¹H³¯GóªZ(s˜û‡ûõ‡Íï§I9)ÒU%Kyëý÷ûW4ßI«dÙüØ+ܯñ2) ?/¦If`ˆl2aãCšM/w±´93Þ'Ÿïئ–ÿé·ßÞIªêâÏu‰7÷œÄU2Ë‹Ë£“χûë´¿?ŒÏ«y^\«Öy™ûê~²‚íëÛÝòþRåµ&uy-U^2ak©Êk}­ËkͧՅwRäù×ÒR÷·*„™C¨#À¼Z.ú³66;O§g¬¾ûÆØêü4ç(*Òÿ51>Ä—ÍÓGΘ&”÷»{º5ZLò¬’ÿŸd³j~D)ÇCˆIUn½àæ Òì,—ó#¢ àÄЇԣØó˜ís98Œ
8Èu< Ø»õú«7ﺌgYRýñé—£Ãÿùâî‰û¥ùèà§ïÕ›ó";8­Òù®Í§Ù›Z³‘ÁTOÉ„ÌDجe0L%ƒ©d0L%ÃUñæ|ºú'qÿ‰#ù·*âÉפò©.ÒÉi¥z°üXöÙü<›$×/Ÿ-ò®gÕ7ù±íØÎ=×w·ÏWIvšVíwk#ÄPó›õÏòÇb&á½ïýùçÑF…Í£¾)C)!PÁÍÅÍ7
/\%vý›n^(¡˜ž¥Éôm®]vóò³t‘|”ÂÕêF= Ô„A­hÔJ= Ôz#©Ú{mì­oº±'¾ÈËó"1΋ś=ÕKÙƒå
7&ñdžù„º¯ïë6ºÎðO5àÞ|ipðçö5Ù3u/|³·î§{Fu¹JÞìÅ«•lŠ±âôþwkÓjöŒý–ÛûpßÎp?.Ê<3‚xqÙG¸›e
I've tested it with other XML links and it seems to work. The url is 'HTTPS' if that might make any difference and here is my code:
$url = "";
$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_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
$result = curl_exec($ch);
curl_close($ch);
echo $result;
Any help is greatly appreciated
The output seems to be gzip encoded, try using:
curl_setopt($ch, CURLOPT_ENCODING , "gzip");
Note:
This option sets the Accept-Encoding: gzip header on the request and uncompresses the response.

Premature end of file. XML response

I am getting a Premature end of file response to my XML request for following code.
Cant figure out where is the error.
$xml = '<?xml version="1.0" ?>';
$xml .= '<PickUpCityListRQ>';
$xml .= '<Credentials username="'.$api->username.'" password="'.$api->password.'" remoteIp="'.$api->remoteIp.'" />';
$xml .= '<Country>UK</Country>';
$xml .= '</PickUpCityListRQ>';
$url = 'https://secure.rentalcars.com/service/ServiceRequest.do?serverName=www.rentalcars.com&xml='.utf8_decode(trim($xml));
$port = 443;
$user_agent = $_SERVER['HTTP_USER_AGENT'];
$ch = curl_init(); // initialize curl handle
curl_setopt($ch, CURLOPT_URL, $url); // set url to post to
curl_setopt($ch, CURLOPT_FAILONERROR, true); // Fail on errors
if (ini_get('open_basedir') == '' && ini_get('safe_mode' == 'Off'))
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);// allow redirects
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); // return into a variable
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
curl_setopt($ch, CURLOPT_PORT, $port); //Set the port number
curl_setopt($ch, CURLOPT_TIMEOUT, 30); // times out after 15s
//curl_setopt($ch, CURLOPT_POST, true);
//curl_setopt($ch, CURLOPT_POSTFIELDS, '&xml='.$xml); // add POST fields
curl_setopt($ch, CURLOPT_USERAGENT, $user_agent);
if ($port==443) {
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
}
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type:application/xml; charset=UTF-8', 'Accept: application/xml; charset=UTF-8'));
$data = curl_exec($ch);
curl_close($ch);
Have tried sending data in post fields as well but did not work.
Response:
<!DOCTYPE DefaultRS SYSTEM 'https://xml.rentalcars.com:443//tj.dtd'><DefaultRS>
<Error id="2">
<Message>Premature end of file.</Message>
</Error>
</DefaultRS>
Any help appreciated.
Thanks
You need to url encode the xml data before you can use it as a query parameter. So the url assignment becomes:
$url = 'https://secure.rentalcars.com/service/ServiceRequest.do?
serverName=www.rentalcars.com&xml='.urlencode(utf8_decode(trim($xml)));
Note, I've just wrapped that for readability - that should obviously be on one line.
After trying a lot of variation I finally managed to make it work.
There were two things that were casuing problem.
utf8_decode should be utf8_encode
POSTFIELDS data is supposed to be an array
otherwise parser will throw Premature end of file or not send back anything.

Getting executed URL from CURL

I have a Affiliate URL Like http://track.abc.com/?affid=1234
open this link will go to http://www.abc.com
now i want to execute the http://track.abc.com/?affid=1234 Using CURL
and now how i can Get http://www.abc.com
with Curl ?
If you want cURL to follow redirect headers from the responses it receives, you need to set that option with:
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
You may also want to limit the number of redirects it follows using:
curl_setopt($ch, CURLOPT_MAXREDIRS, 3);
So you'd using something similar to this:
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://track.abc.com/?affid=1234");
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
curl_setopt($ch, CURLOPT_MAXREDIRS, 3);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
$data = curl_exec($ch);
Edit: Question wasn't exactly clear but from the comment below, if you want to get the redirect location, you need to get the headers from cURL and parse them for the Location header:
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://track.abc.com/?affid=1234");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_HEADER, true);
$data = curl_exec($ch);
This will give you the headers returned by the server in $data, simply parse through them to get the location header and you'll get your result. This question shows you how to do that.
I wrote a function that will extract any header from a cURL header response.
function getHeader($headerString, $key) {
preg_match('#\s\b' . $key . '\b:\s.*\s#', $headerString, $header);
return substr($header[0], strlen($key) + 3, -2);
}
In this case, you're looking for the value of the header Location. I tested the function by retrieving headers from a TinyURL, that redirects to http://google.se, using cURL.
$url = "http://tinyurl.com/dtrkv";
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$data = curl_exec($ch);
curl_close($ch);
$location = getHeader($data, 'Location');
var_dump($location);
Output from the var_dump.
string(16) "http://google.se"

how to run: url from php = run from browser

How to run url from php script in the same way (exactly the same behaviour) as in browser when i run url from address bar. I mean with the same header data, cookies and additional data which browser send. How to add this data in php.
I need this cause when I logged in, answers from this 2 cases are not the same:
in browser I still logged in and this is correct
from php run I am logged OUT - not correct
I've tried file_get_contents nad curl (from here) but it doesn't work properly - response is still different.
I'm calling http://127.0.0.1/check.html and here is function check:
public function check(){
echo 'begin';
// $total_rows = file_get_contents('https://127.0.0.1:8443/example.html?shopId=121');
$total_rows = $this->getUrl('https://127.0.0.1:8443/example.html', '121');
print_r($total_rows);
echo 'end';
}
function getUrl($url, $shopId ='') {
$post = 'shopId=' . $shopId;
$ch = curl_init();
$cookie_string="";
foreach( $_COOKIE as $key => $value ) {
$cookie_string .= "$key=$value;";
};
$cookie_string .= "JSESSIONIDSSO=66025D1CC9EF39ED7F5DB024B6026C61";
// echo $cookie_string;;
$ch = curl_init();
curl_setopt($ch,CURLOPT_COOKIE, $cookie_string);
// curl_setopt($ch, CURLOPT_PORT, 8443);
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
// curl_setopt ($ch, CURLOPT_CAINFO, dirname(__FILE__)."/../../files/cacert.pem");
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
// curl_setopt($ch, CURLOPT_HTTPHEADER, Array("Secure Content-Type: text/html; charset=UTF-8"));
// curl_setopt($ch, CURLOPT_HTTPHEADER, array('Host: 127.0.0.1:8443'));
$ret = curl_exec($ch);
curl_error ($ch );
curl_close($ch);
return $ret;
}
Try it:
http://www.lastcraft.com/browser_documentation.php
Or that:
http://sourceforge.net/projects/snoopy/
Or that:
php curl: how can i emulate a get request exactly like a web browser?
Hope help
You can execute the cron job using your PHP script to execute the other script.

Categories