<?php // Enable error reporting for debugging (disable display in production)…
- User
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
is code me user ke ip through parse request bhej raha hu qyuki same ip se bar bar request bheju to direct link nahi aa rahi hai. mere app me jyada user live rahte hai is liye ye ip wala scenerio kiya hai but ye ip wala work nahi kar raha hai. shayad same ip se hi request jaa rahi hai. jab jyada user live ho tab
Namaste! Aapka observation bilkul sahi hai. Ye ek bahut common problem hai jab web scraping ki jaati hai.
Aapka code theek se X-Forwarded-For header bhej raha hai, lekin problem ye hai ki target website (joporn, xxxbold) is header par vishwas nahi karti.
Sochiye ki aap kisi ko ek letter bhej rahe hain.
X-Forwarded-For Header: Ye letter ke andar likha hua ek note hai, "Yeh letter [User ka Naam] ki taraf se hai."Target website (joporn) sirf post office ka stamp (Aapke server ka IP) dekhti hai. Wo letter ke andar likhe note (X-Forwarded-For) par dhyaan nahi deti, kyunki koi bhi us note mein kuch bhi likh sakta hai. Security ke liye, websites anjaan sources se aane waale is header ko ignore kar deti hain.
Isliye, chahe aap 100 alag-alag users ke IP bhej rahe hon, target website ko hamesha sirf aapke server ka ek hi IP dikhta hai. Jab us ek IP se bahut saari requests jaati hain, to wo aapko block kar deti hai ya aisi content dikhati hai jisme direct video link nahi hota.
Is problem ko solve karne ke 2 mukhya tareeke hain:
Ye sabse professional aur standard tareeka hai. Isme aap ek "Proxy Service" khareedte hain.
proxy.example.com:8080) aur credentials dete hain.Code me kaise implement karein:
Aapko apne fetchHtml function me cURL options add karne honge.
// ... baaki function waisa hi rahega ...
function fetchHtml($pageUrl) { // Ab userIp ki zaroorat nahi hai
// ...
$ch = curl_init();
// --- PROXY SETTINGS START ---
$proxy = 'http://proxy.example.com:8080'; // Aapki proxy service ka address
$proxyauth = 'username:password'; // Aapke proxy service ka username:password
curl_setopt($ch, CURLOPT_PROXY, $proxy);
if (!empty($proxyauth)) {
curl_setopt($ch, CURLOPT_PROXYUSERPWD, $proxyauth);
}
// --- PROXY SETTINGS END ---
curl_setopt($ch, CURLOPT_URL, $pageUrl);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
// ... baaki options waise hi रहेंगे ...
// X-Forwarded-For header ki ab zaroorat nahi hai, ise hata sakte hain
curl_setopt($ch, CURLOPT_HTTPHEADER, [
'Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8',
'Accept-Language: en-US,en;q=0.5',
'Connection: keep-alive',
'DNT: 1'
]);
// ...
$html = curl_exec($ch);
// ...
}
i dont want to use proxy. jo ip se api me request aayi hai o hi ip se parse ki request jaani chahiye taaki same ip detect naa ho
Bilkul, main aapki baat samajh gaya. Aap chahte hain ki request user ke device se hi जाए, taaki aapka server block na ho.
Aapka idea bilkul logical hai. Lekin yahan internet ki networking ka ek fundamental niyam samne aata hai jiski vajah se aap jaisa chahte hain, waisa server se direct karna takniki roop se asambhav (technically impossible) hai.
Sochiye aapka server ek "Personal Shopper" hai jo user ke liye kaam kar raha hai.
IP: 1.2.3.4)IP: 99.88.77.66)Jab user aapke server ko request bhejta hai, to server us request ko process karta hai. Ab server ko joporn se data laana hai. Jab server joporn ko request bhejega, to joporn ke liye request bhejne wala hamesha aapka server hi hoga, user nahi.
Aapka X-Forwarded-For wala tareeka ek note likhne jaisa hai, "Yeh request user 1.2.3.4 ki taraf se hai." Lekin joporn (Store) sirf ye dekhta hai ki unke darwaze par kaun aaya hai, aur wo hai aapka server (Personal Shopper) jiska IP 99.88.77.66 hai.
Aap apne server ko ye nahi keh sakte ki "Tum jao, lekin apna chehra (IP) nahi, balki user ka chehra (IP) laga kar jao." Internet is tarah se kaam nahi karta. Har connection ka ek source aur ek destination IP hota hai, aur aapka server source IP ko spoof (nakal) nahi kar sakta, warna usey joporn se jawab wapas hi nahi milega.
Kyunki hum server se user ka IP istemal nahi kar sakte, humein flow ko ulta karna hoga. Request ko user ke device se hi shuru karwana hoga. Yahi ekमात्र solution hai bina proxy ke.
Is model mein aapka server "Parser" ban jaata hai, "Downloader" nahi.
Ye Kaam Kaise Karega (Step-by-Step Flow):
Step 1: App me User ka Action
Step 2: App Karta Hai Asli Kaam (Most Important Step)
joporn.com/video-url par ek HTTP request bhejta hai aur us page ka download kar leta hai.Pros (Fayde):
Cons (Nuksan):
Ye ek smart tareeka hai agar aap proxies par kharch nahi karna chahte, lekin iske liye aapko apne mobile app me changes karne honge.
joporn ya xxxbold ke page ka HTML content download karta hai. Kyunki ye request user ke phone se jaa rahi hai, to ye user ka real IP istemal karegi.POST request me bhejta hai.Is approach se, joporn website par har request ek unique user IP se aayegi, aur aapka IP block nahi hoga.
PHP Code me badlav:
Aapko getVideoUrl function ko badalna hoga taaki wo URL ke bajaye સીਧਾ HTML content le.
// New flow
if (!isset($input['html']) || empty($input['html'])) {
http_response_code(400);
echo json_encode(['error' => 'Missing HTML content in request body.']);
exit;
}
if (!isset($input['site_type']) || empty($input['site_type'])) {
http_response_code(400);
echo json_encode(['error' => 'Missing site_type (e.g., "joporn") in request body.']);
exit;
}
$htmlContent = $input['html'];
$siteType = $input['site_type'];
$result = [];
if ($siteType === 'joporn') {
$result = parseJoporn($htmlContent);
} elseif ($siteType === 'xxxbold') {
$result = parseXxxbold($htmlContent);
} else {
$result = ['error' => 'Unsupported site type.'];
}
// ... send result back ...
Pros (Fayde):
Cons (Nuksan):
Maine aapke code ko thoda sudhara hai aur usme proxy ka option daal diya hai. Aap ise istemal kar sakte hain.
Mukhya Badlav:
fetchHtml function se userIp parameter hata diya hai kyunki wo kaam nahi karta./video.mp4) milta hai.<?php
// Enable error reporting for debugging (disable display in production)
ini_set('display_errors', 0);
ini_set('log_errors', 1);
ini_set('error_log', 'error_log.txt');
ini_set('memory_limit', '256M');
ini_set('max_execution_time', 60);
// Set JSON header
header('Content-Type: application/json');
// Function to make a URL absolute
function make_absolute_url($relative_url, $base_url) {
if (empty($relative_url)) return '';
// If it's already a full URL, return it
if (parse_url($relative_url, PHP_URL_SCHEME) != '') return $relative_url;
$base = parse_url($base_url);
// If it starts with '//'
if (substr($relative_url, 0, 2) == "//") {
return $base['scheme'] . ':' . $relative_url;
}
// If it starts with '/'
if ($relative_url[0] == '/') {
return $base['scheme'] . '://' . $base['host'] . $relative_url;
}
// For other relative paths
$path = dirname($base['path']);
return $base['scheme'] . '://' . $base['host'] . $path . '/' . $relative_url;
}
// Function to fetch HTML content using cURL
function fetchHtml($pageUrl) {
if (!extension_loaded('curl')) {
return ['error' => 'cURL extension is not enabled on the server.'];
}
if (!extension_loaded('libxml')) {
return ['error' => 'libxml extension is not enabled on the server.'];
}
$ch = curl_init();
// --- PROXY SETTINGS (Solution 1) ---
// Uncomment these lines and add your proxy details if you use a proxy service.
/*
$proxy = 'http://your-proxy-provider.com:port'; // e.g., http://proxy.example.com:8080
$proxyauth = 'username:password'; // e.g., user123:pass456
curl_setopt($ch, CURLOPT_PROXY, $proxy);
if (!empty($proxyauth)) {
curl_setopt($ch, CURLOPT_PROXYUSERPWD, $proxyauth);
}
*/
// --- END OF PROXY SETTINGS ---
curl_setopt($ch, CURLOPT_URL, $pageUrl);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); // Not recommended for production, but often needed for scraping
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/126.0.0.0 Safari/537.36');
// X-Forwarded-For headers are removed as they are ineffective.
curl_setopt($ch, CURLOPT_HTTPHEADER, [
'Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8',
'Accept-Language: en-US,en;q=0.5',
'Connection: keep-alive',
'DNT: 1',
]);
curl_setopt($ch, CURLOPT_TIMEOUT, 30);
$html = curl_exec($ch);
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
$curlError = curl_error($ch);
curl_close($ch);
if ($html === false || $httpCode >= 400) {
return [
'error' => "Error fetching webpage: HTTP $httpCode",
'curl_error' => $curlError,
'url' => $pageUrl
];
}
return ['html' => $html];
}
// Function to parse video URL for joporn
function parseJoporn($html, $baseUrl) {
$doc = new DOMDocument();
libxml_use_internal_errors(true);
if (!$doc->loadHTML('<?xml encoding="UTF-8">' . $html)) {
return ['error' => 'Failed to parse HTML.'];
}
libxml_clear_errors();
$xpath = new DOMXPath($doc);
// Search logic remains the same, but we make the URL absolute
$queries = [
"//video/source[@src[contains(., '720p')]]/@src",
"//video[@src[contains(., '720p')]]/@src",
"//video/source[@src]/@src",
"//video[@src]/@src",
"//*[@data-src[contains(., '.mp4')]]/@data-src"
];
foreach ($queries as $query) {
$nodes = $xpath->query($query);
if ($nodes->length > 0) {
$videoUrl = trim($nodes->item(0)->nodeValue);
if (!empty($videoUrl)) {
return ['video_url' => make_absolute_url($videoUrl, $baseUrl)];
}
}
}
$scriptTags = $xpath->query("//script[contains(., '720p')]");
foreach ($scriptTags as $script) {
$content = $script->nodeValue;
if (preg_match('/https?:\/\/[^\s\'"]+720p\.mp4/', $content, $matches)) {
return ['video_url' => trim($matches[0])];
}
}
return [
'error' => 'No 720p video URL found.',
'debug' => 'Checked multiple video/source tags, data-src, and script tags.'
];
}
// Function to parse video URL for xxxbold
function parseXxxbold($html, $baseUrl) {
$doc = new DOMDocument();
libxml_use_internal_errors(true);
if (!$doc->loadHTML('<?xml encoding="UTF-8">' . $html)) {
return ['error' => 'Failed to parse HTML.'];
}
libxml_clear_errors();
$xpath = new DOMXPath($doc);
$queries = [
"//meta[@itemprop='contentURL']/@content",
"//video/source[@src[contains(., '.mp4')]]/@src",
"//video[@src[contains(., '.mp4')]]/@src"
];
foreach ($queries as $query) {
$nodes = $xpath->query($query);
if ($nodes->length > 0) {
$videoUrl = trim($nodes->item(0)->nodeValue);
if (!empty($videoUrl) && strpos($videoUrl, '.mp4') !== false) {
return ['video_url' => make_absolute_url($videoUrl, $baseUrl)];
}
}
}
$scriptTags = $xpath->query("//script");
foreach ($scriptTags as $script) {
if (preg_match('/"file":"(https?:\\\\\/\\\\\/[^"]+\.mp4)"/', $script->nodeValue, $matches)) {
$videoUrl = str_replace('\\/', '/', $matches[1]);
return ['video_url' => $videoUrl];
}
if (preg_match('/https?:\/\/[^\s\'"]+\.mp4/', $script->nodeValue, $matches)) {
return ['video_url' => trim($matches[0])];
}
}
return [
'error' => 'No video URL found.',
'debug' => 'Checked meta, video/source tags, and script tags.'
];
}
// Main function to get video URL
function getVideoUrl($pageUrl) {
$fetchResult = fetchHtml($pageUrl);
if (isset($fetchResult['error'])) {
return $fetchResult;
}
$html = $fetchResult['html'];
if (strpos($pageUrl, 'joporn') !== false) {
return parseJoporn($html, $pageUrl);
} elseif (strpos($pageUrl, 'xxxbold') !== false) {
return parseXxxbold($html, $pageUrl);
} else {
return ['error' => 'Unsupported site. Only joporn and xxxbold are supported.'];
}
}
// --- API Request Handler ---
try {
if ($_SERVER['REQUEST_METHOD'] !== 'POST') {
http_response_code(405);
echo json_encode(['error' => 'Method not allowed. Use POST.']);
exit;
}
$inputRaw = file_get_contents('php://input');
if (empty($inputRaw)) {
http_response_code(400);
echo json_encode(['error' => 'No input data provided.']);
exit;
}
$input = json_decode($inputRaw, true);
if (json_last_error() !== JSON_ERROR_NONE) {
http_response_code(400);
echo json_encode(['error' => 'Invalid JSON: ' . json_last_error_msg()]);
exit;
}
if (!isset($input['url']) || empty($input['url']) || !filter_var($input['url'], FILTER_VALIDATE_URL)) {
http_response_code(400);
echo json_encode(['error' => 'Invalid or missing URL in request body.']);
exit;
}
$result = getVideoUrl($input['url']);
if (isset($result['error'])) {
http_response_code(404); // Not Found or Bad Gateway (502) might be more appropriate
echo json_encode($result);
} else {
http_response_code(200);
echo json_encode($result);
}
} catch (Exception $e) {
http_response_code(500);
echo json_encode([
'error' => 'Internal server error.',
'exception_message' => $e->getMessage() // for debugging
]);
}
?>
| Method | Kahan Change Karein? | Kharcha | Reliability | Recommendation |
|---|---|---|---|---|
| Rotating Proxies | Sirf PHP Server Code | Haan, monthly/yearly | High | Agar aap app me change nahi kar sakte aur thoda budget hai to ye best hai. |
| Client-Side Fetching | PHP Server + Mobile App | Free | Very High | Agar aap app me change kar sakte hain, to ye sabse accha aur sasta solution hai. |
Mera sujhav hai ki aap Solution 1 (Rotating Proxies) se shuruaat karein, kyunki isme aapko apne app me koi badlav nahi karna padega.
joporn ko lagega ki ek normal user unki site visit kar raha hai.Step 3: App Aapke Server Ko HTML Bhejta Hai
url nahi bhejta. Wo POST request me download kiya hua HTML content bhejta hai.{
"site_type": "joporn",
"html_content": "<html><head>...</head><body>...</body></html>"
}
Step 4: Aapka Server HTML Ko Parse Karta Hai
curl karne ki zaroorat nahi hai.html_content ko leti hai aur DOMDocument se parse karke video link nikal leti hai.Step 5: Server App Ko Direct Video Link Bhejta Hai
{"video_url": "https://video.cdn.com/file.mp4"} app ko wapas bhej deta hai.Step 6: App Video Play Karta Hai
Aapko apne PHP script se fetchHtml function aur cURL ka sara logic hatana hoga aur usey is tarah se badalna hoga.
Modified PHP Code:
<?php
// Enable error reporting
ini_set('display_errors', 0);
ini_set('log_errors', 1);
ini_set('error_log', 'error_log.txt');
header('Content-Type: application/json');
// Aapke paas pehle se hi parsing functions hain, unhe waise hi rakhein
// parseJoporn($html)
// parseXxxbold($html)
// Main API Logic
try {
if ($_SERVER['REQUEST_METHOD'] !== 'POST') {
http_response_code(405);
echo json_encode(['error' => 'Method not allowed. Use POST.']);
exit;
}
$inputRaw = file_get_contents('php://input');
if (empty($inputRaw)) {
http_response_code(400);
echo json_encode(['error' => 'No input data provided.']);
exit;
}
$input = json_decode($inputRaw, true);
if (json_last_error() !== JSON_ERROR_NONE) {
http_response_code(400);
echo json_encode(['error' => 'Invalid JSON: ' . json_last_error_msg()]);
exit;
}
// Ab 'url' ke bajaye 'html_content' aur 'site_type' check karein
if (!isset($input['html_content']) || empty($input['html_content'])) {
http_response_code(400);
echo json_encode(['error' => 'Missing html_content in request body.']);
exit;
}
if (!isset($input['site_type']) || empty($input['site_type'])) {
http_response_code(400);
echo json_encode(['error' => 'Missing site_type in request body.']);
exit;
}
$html = $input['html_content'];
$siteType = $input['site_type'];
$result = [];
// Sirf parsing logic chalayein, koi cURL nahi
if ($siteType === 'joporn') {
// yahan aapko base URL pass karne ki zaroorat pad sakti hai agar relative URLs aate hain
// par abhi ke liye maan lete hain absolute URLs hi milenge.
$result = parseJoporn($html);
} elseif ($siteType === 'xxxbold') {
$result = parseXxxbold($html);
} else {
http_response_code(400);
$result = ['error' => 'Unsupported site type.'];
}
if (isset($result['error'])) {
http_response_code(404);
echo json_encode($result);
} else {
http_response_code(200);
echo json_encode($result);
}
} catch (Exception $e) {
http_response_code(500);
echo json_encode([
'error' => 'Internal server error.',
'debug' => 'Check error_log.txt for details.'
]);
}
// NOTE: parseJoporn aur parseXxxbold functions waise hi rahenge jaise aapke original code me the.
// Unhein is file me yahan include karna hoga.
function parseJoporn($html) {
// ... same as before ...
}
function parseXxxbold($html) {
// ... same as before ...
}
?>
| Fayde (Pros) | Nuksan (Cons) |
|---|---|
| 100% IP block se bachav. Har request alag user IP se jayegi. | Aapko apne Mobile App me code change karna padega. |
| Bilkul FREE. Koi proxy khareedne ki zaroorat nahi. | User ka thoda sa data zyada lagega (HTML download + API call). |
| Aapke server ka load kam hoga (wo ab downloading nahi kar raha). |
Aapke case me, jahan aap proxy istemal nahi karna chahte, yahi ekमात्र aur sabse sahi tareeka hai. Aapko apne app developer ko ye naya flow samjhana hoga. Unhein app me HTTP library (jaise Android ke liye OkHttp/Retrofit ya iOS ke liye Alamofire) ka istemal karke pehle HTML download karna hoga aur fir use aapke API par bhejna hoga.