Final Effective URL - PHP (Proxy) - php

I apologize in advance for my English. I have small problem.
I want to get Final Effective URL from page
streamuj.tv/video/00e276bf5841bf77c8de?streamuj=original&authorize=ac13bb77d3d863ca362315b9b4dcdf3e
When you put a link into the browser gives me to .flv file
But when I put it through PHP gives me s3.streamuj.tv/unauthorized.flv
When I try it through this: getlinkinfo.com/info?link=http%3A%2F%2Fwww.streamuj.tv%2Fvideo%2F00e276bf5841bf77c8de%3Fstreamuj%3Doriginal%26authorize%3Dac13bb77d3d863ca362315b9b4dcdf3e&x=49&y=11
So everything is fine indicates that
s4.streamuj.tv:8080/vid/d0fe77e1020b6414a16aa5316c759add/58aaf1dd/00e276bf5841bf77c8de_hd.flv?start=0
My PHP CODE:
<?php
session_start();
include "simple_html_dom.php";
$proxy = array("189.3.93.114:8080");
$proxyNum = 0;
$proxy = explode(':', $proxy[$proxyNum]);
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, 'http://www.streamuj.tv/video/00e276bf5841bf77c8de?streamuj=original&authorize=ac13bb77d3d863ca362315b9b4dcdf3e');
curl_setopt($curl, CURLOPT_FILETIME, true);
curl_setopt($curl, CURLOPT_NOBODY, true);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_HEADER, true);
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, TRUE);
curl_setopt($curl, CURLOPT_PROXY, $proxy[0]);
curl_setopt($curl, CURLOPT_PROXYPORT, $proxy[1]);
$header = curl_exec($curl);
$info = curl_getinfo($curl);
curl_close($curl);
$u1 = $info['url'];
echo "u1: $u1</br>";
$u2 = str_replace("flv?start=0","flv",$u1);
echo $u2;
?>
Where is the problem? Why it makes unauthorized.flv?

Solution
Server was checking client legitimacy via user-agent HTTP header parameter.
Using custom user-agent solved the problem.
curl_setopt($curl, CURLOPT_HTTPHEADER, array( 'user-agent:Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/56.0.2950.0 Iron Safari/537.36' ));
Original post:
Most likely the generated flv URL is not pointing to static place. It
probably uses sessionID + cookie / verifies IP (one of these, or
both).
Without knowing what header you have to request with via CURL, you
probably won't get a relevant response.

Related

why am i getting Forbidden when i try to use curl in php file

I m trying to integrate a payment method on a website, the first thing I did, I tried a curl code to test it using git console and it works just fine, then I tried to execute the curl command using PHP. I created a file then I used this code:
<?php
$endpoint_url = 'https://secure.payinspect.com';
$params = [
'action'=>'SALE',
'order_id'=>'ORDER12345',
'order_amount'=>'1.99',
'order_currency'=>'USD',
'order_description'=>'Product',
'card_number'=>'4111111111111111',
'card_exp_month'=>'05',
'card_exp_year'=>'2020',
'card_cvv2'=>'000',
'payer_first_name'=>'John',
'payer_last_name'=>'Doe',
'payer_address'=>'BigStreet',
'payer_country'=>'US',
'payer_state'=>'CA',
'payer_city'=>'City',
'payer_zip'=>'123456',
'payer_email'=>'doe#example',
'payer_phone'=>'199999999',
'payer_ip'=>'123.123.123.123',
'term_url_3ds'=>'http://client.site.com/return.php',
'recurring_init'=>'N',
'hash'=>'e3dd86f469f40a5cfedf96a82ff257af'
];
$buff = [];
foreach ($params as $k => $v) {
array_push($buff, "{$k}={$v}");
}
$url = $endpoint_url . implode('&', $buff);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_close($ch);
if ($result===false){ print curl_error($curl); }
$response = json_decode($result, true);
echo $result;
?>
but I got this error :
Forbidden
You don't have permission to access
I googled for this error and I tried to add this line
curl_setopt($ch,CURLOPT_USERAGENT,'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/60.0.3112.113 Safari/537.36');
$result = curl_exec($ch);
but i still got the same error . so what causes this problem and how cauld i fix it
Assuming everything else is correct - this might fix the problem.
<?php
$endpoint_url = 'https://secure.payinspect.com';
$params = [
'action'=>'SALE',
'order_id'=>'ORDER12345',
'order_amount'=>'1.99',
'order_currency'=>'USD',
'order_description'=>'Product',
'card_number'=>'4111111111111111',
'card_exp_month'=>'05',
'card_exp_year'=>'2020',
'card_cvv2'=>'000',
'payer_first_name'=>'John',
'payer_last_name'=>'Doe',
'payer_address'=>'BigStreet',
'payer_country'=>'US',
'payer_state'=>'CA',
'payer_city'=>'City',
'payer_zip'=>'123456',
'payer_email'=>'doe#example',
'payer_phone'=>'199999999',
'payer_ip'=>'123.123.123.123',
'term_url_3ds'=>'http://client.site.com/return.php',
'recurring_init'=>'N',
'hash'=>'e3dd86f469f40a5cfedf96a82ff257af'
];
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $endpoint_url);
// -- this sets the request method to POST ----
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($params));
// --- end ----
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_close($ch);
if ($result===false) { print curl_error($curl); }
$response = json_decode($result, true);
echo $result;
I can't say for certain (which means this isn't a great answer) but by suffixing the parameters to the URL, in the way you're doing currently, you're creating a GET request rather than a POST one.
It's quite likely that the receiving service is expecting to see your hash value (and everything else) in the POST data - and as it doesn't see it there, it rejects your request completely.

PHP cURL with post data login not working where wget with shell_exec is

I'm trying to access remote files on a website that requires me to log in to it. The current way I'm doing this is with a wget ran from shell_exec. This 'works' but is definitly not the way I want to do it in. I would prefer to do it using cURL, but for some reason this will not work for me.
What happens instead of getting the page I requested is I'm redirected to the login page, which does not happen with the wget method, which should be making the exact same request...
What am I doing wrong?
First of all this is the current (not nice but) working method of accessing the page:
shell_exec("wget --post-data='serviceLoginUser=something&serviceLoginPass=something&qq=login' -p https://internal.website.com/somepage.php?get=request -O return.json");
This is my download function which does not work but I want to replace the old way with: (updated after suggestion from Abkarino)
public function download($url, $postData = NULL){
//Set cURL options
curl_setopt($this->ch, CURLOPT_URL, $url);
curl_setopt($this->ch, CURLOPT_HEADER, 0);
//To act like a normal browser (needed for intranet)
curl_setopt($this->ch, CURLOPT_USERAGENT,'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.17 (KHTML, like Gecko) Chrome/24.0.1312.52 Safari/537.17');
curl_setopt($this->ch, CURLOPT_AUTOREFERER, true);
curl_setopt($this->ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($this->ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($this->ch, CURLOPT_VERBOSE, 1);
//TODO: remove
curl_setopt($this->ch, CURLOPT_SSL_VERIFYPEER, false);
//If POST data was supplied:
if ($postData != NULL){
curl_setopt($this->ch,CURLOPT_POSTFIELDS, $postData);
}
//execute post
return curl_exec($this->ch);
}
Where the following code will form the request: (currenly this code is only to test if the cURL actually returns the correct data)
include 'Downloader.php';
$dl = new Downloader;
$postData = array(
'serviceLoginUser' => 'something',
'serviceLoginPass' => 'something',
'qq' => 'login'
);
echo $dl->download("https://internal.website.com/somepage.php?get=request", $postData);

Php not redirecting right in kodi

I have a script that gathers a session id, puts it together with a URL and then redirects to the URL. This works perfectly in the browser and mx player for Android. But on kodi, there seems to be an error. Kodi seems to use my server as the host of the file. So instead of using: streamsite.com/index.m3u8, it uses MYSERVER.com/index.m3u8. This is driving me crazy since I do not even know how to code. This is my script:
<?php
$url = link.tojson
$cURL = curl_init();
curl_setopt($cURL,CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($cURL, CURLOPT_URL, $url); curl_setopt($cURL, CURLOPT_HTTPGET, true);
curl_setopt($cURL, CURLOPT_RETURNTRANSFER, true);
curl_setopt($cURL, CURLOPT_HTTPHEADER, array( 'Content-Type: application/json', 'Accept: application/json' ));
$result = curl_exec($cURL);
curl_close($cURL);
$json=json_decode($result,true);
$pre=$json[0]['id'];
$stream='streamsite.com/index.m3u8?&sessionId='.$pre. '';
ini_set('user_agent', 'Mozilla/5.0 (Linux; Android 6.0; en-US; Nexus 5 Build/Veneno ROM) MXPlayer/1.8.3
');
header("Location:$stream");
die();
?>
Try using a proper URL:
$stream="http://streamsite.com/index.m3u8?sessionId=$pre";
header("Location:$stream");
Also I don't know what you think that call to ini_set() will accomplish, but it won't.

Scrape website with javascript using cURL

I try to scrape data of this website:
http://ntthnue.edu.vn/tracuudiem
First, when I insert the SBD field with data 'TS4740', I can successfully get the result. However, when I try to run this code:
Here is my PHP cURL code:
<?php
function getData($id) {
$url = 'http://ntthnue.edu.vn/tracuudiem';
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, ['sbd' => $id]);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($ch);
curl_close($ch);
return $result;
}
echo getData('TS4740');
I just got the old page. Can anybody explain why? Thank you!
Make sure you add all the necessary headers and input data. The server that is processing this request can do all kinds of checks to see if it's a "valid" form request. As such you need to spoof the request to be as close to a regular browser request as possible.
Use tools like Chrome Dev Tools to see both the request and respons headers that are sent between the server and your browser to better understand what you curl setup should be like. And further use a app like Postman to make the request simulation super easy and to see what works and not.
Working example:
<?php
function getData($id) {
$url = 'http://ntthnue.edu.vn/tracuudiem';
$ch = curl_init($url);
$postdata = 'namhoc=2015-2016&kythi_name=Tuy%E1%BB%83n+sinh+v%C3%A0o+l%E1%BB%9Bp+10&hoten=&sbd='.$id.'&btnSearch=T%C3%ACm+ki%E1%BA%BFm';
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postdata);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8',
'Origin: http://ntthnue.edu.vn',
'User-Agent: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/43.0.2357.130 Safari/537.36',
'Content-Type: application/x-www-form-urlencoded',
'Referer: http://ntthnue.edu.vn/tracuudiem',
));
$result = curl_exec($ch);
curl_close($ch);
return $result;
}
echo getData('TS4740');

Curl isn't woking with Nagios

I'm currently developing a nagios plugin with PHP and cURL.
My problem is that my script is working well when i use it with PHP like this :
#php /usr/local/nagios/plugins/script.php
I mean it returns me a 200 HTTP CODE.
But with nagios it returns me a 0 HTTP CODE. It's strange because the php is working with NAGIOS (i can read variables...). So the problem is that Nagios can't use cURL.
Can someone give me a clue ? Thanks.
Here you can see my code.
<?php
$widgeturl = "http://google.com";
$agent = "Mozilla/5.0 (Windows; U; Windows NT 6.0; en-US; rv:1.9.0.12) Gecko/2009070611 Firefox/3.0.12";
if (!function_exists("curl_init")) die("pushMeTo needs CURL module, please install CURL on your php.");
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $widgeturl);
$page = curl_exec($ch); //or die("Curl exe failed");
$code=curl_getinfo($ch, CURLINFO_HTTP_CODE);
if ($code==200) {
fwrite(STDOUT, $page.'Working well : '.$code);
exit(0);
}
else {
fwrite(STDOUT, $page.'not working : '.$code);
exit(1);
}
curl_close($ch);
Solution :
It was because the proxy was basically set on my OS (centOS), but Nagios was not using it instead of PHP. So i just had to put : curl_setopt($ch, CURLOPT_PROXY, 'myproxy:8080'); curl_setopt($ch, CURLOPT_PROXYUSERPWD, "user:pass"); Hope it could help someone
curl_setopt($ch, CURLOPT_PROXY, 'myproxy:8080');
curl_setopt($ch, CURLOPT_PROXYUSERPWD, "user:pass")
Can you try making the CURL request like this (i.e. header only request):
<?php
// config
$url = 'http://www.google.com/';
// make request & parse response
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_FILETIME, true);
curl_setopt($curl, CURLOPT_NOBODY, true);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
$response_header = curl_exec($curl);
$response_info = curl_getinfo($curl);
curl_close($curl);
// debug
echo "<b>response_header</b>\r\n";
var_dump($response_header);
echo "<b>response_info</b>\r\n";
var_dump($response_info);
The above will output the following:

Categories