Requesting page returns 403 Bad Behavior - php

I wrote a small script to verify if an url exists. I am using get_headers to retrieve the headers. The issue is that with some url, example this one: https://forum.obviousidea.com the response is 403 Bad Behavior, while if i open the page with browser it works.
Example output:
$headers = get_headers(https://forum.obviousidea.com);
print_r($headers);
(
[0] => HTTP/1.1 403 Bad Behavior
[Server] => nginx/1.6.2
[Date] => Tue, 04 Jun 2019 21:56:27 GMT
[Content-Type] => text/html; charset=ISO-8859-1
[Content-Length] => 913
[Connection] => close
[Set-Cookie] => Array
(
[0] => bb_lastvisit=1559685385; expires=Wed, 03-Jun-2020 21:56:25 GMT; Max-Age=31536000; path=/; secure
[1] => bb_lastactivity=0; expires=Wed, 03-Jun-2020 21:56:25 GMT; Max-Age=31536000; path=/; secure
[2] => PHPSESSID=cqtkdcfpm0k2s8hl4cup6epa37; path=/
)
[Expires] => Thu, 19 Nov 1981 08:52:00 GMT
[Cache-Control] => private
[Pragma] => private
[Status] => 403 Bad Behavior
)
How can I get the right status code using get_headers ?
Note using the user agent suggested in the answer now this website works.
But for example this url still doesn't work: https://filezilla-project.org/download.php?type=client

You may have changed the UserAgent header in php.ini or by ini_set
check it or set UserAgent like like the example below
ini_set('user_agent', '');
$headers = get_headers('https://forum.obviousidea.com');
I prefer use bellow curl function:
/**
* #param string $url
* #param array $headers
* #return array
* #throws Exception
*/
function curlGetHeaders(string $url, array $headers = [])
{
$url = trim($url);
if (!filter_var($url, FILTER_VALIDATE_URL)) {
throw new Exception("$url is not a valid URL", 422);
}
$url = explode('?', $url);
$curl = curl_init();
curl_setopt_array($curl, [
CURLOPT_URL => $url[0],
CURLOPT_RETURNTRANSFER => true,
CURLOPT_NOBODY => true,
CURLOPT_HEADER => true,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "GET",
]);
if (isset($url[1])) {
curl_setopt($curl, CURLOPT_POSTFIELDS, $url[0]);
}
if (!empty($headers)) {
foreach($headers as $key => $header) {
$curlHeaders[] = "$key:$header";
}
curl_setopt($curl, CURLOPT_HTTPHEADER, $curlHeaders);
}
$response = rtrim(curl_exec($curl));
$responseCode = curl_getinfo($curl, CURLINFO_RESPONSE_CODE);
curl_error($curl);
curl_close($curl);
$headers = [];
$data = explode("\r\n", $response);
$headers['Response-Code'] = $responseCode;
$headers['Status'] = $data[0];
array_shift($data);
foreach($data as $part) {
$middle = explode(":", $part, 2);
if (!isset($middle[1])) {
$middle[1] = null;
}
$headers[trim($middle[0])] = trim($middle[1]);
}
return $headers;
}

Related

How to get data from third party api with PHP cURL?

I want to fetch json data from one API but I am getting following error
HTTP/1.1 500 Internal Server Error
Cache-Control: no-cache
Pragma: no-cache
Content-Type: application/json; charset=utf-8
Expires: -1
Server: Microsoft-IIS/7.5
X-AspNet-Version: 4.0.30319
X-Powered-By: ASP.NET
Date: Wed, 27 Mar 2019 06:07:40 GMT
Content-Length: 36
{"Message":"An error has occurred."}
Its works with postman.Is there any syntax error while sending request?
This is my code
<?php
// Initiate curl
//$post = "NoofAdult=1&NoofChild=1&NoofInfant=1&FromAirportCode=AMD&ToAirportCode=BOM&DepartureDate=21/06/2019&ReturnDate&TripType=1&FlightClass=Y&SpecialFare=0&AirlineType=A";
$postData = array(
'NoofAdult' => '1',
'NoofChild' => '1',
'NoofInfant' => '1',
'FromAirportCode' => 'AMD',
'ToAirportCode' => 'BOM',
'DepartureDate' => '21/06/2019',
'ReturnDate' => '',
'TripType' => '1',
'FlightClass' => 'Y',
'SpecialFare' => '0',
'AirlineType' => 'A'
);
$header_data = array(
"Content-Type: application/json",
"Accept-Encoding: gzip, deflate",
"InterfaceCode:1",
"InterfaceAuthKey:1",
"AgentCode:",
"Password:"
);
$ch = curl_init();
$curlOpts = array(
CURLOPT_URL => 'http://stagingv2.flightmyweb.com/API/FlightAvailibility',
//CURLOPT_URL => 'http://localhost/akshay/sampleapi.php',
CURLOPT_RETURNTRANSFER => true,
CURLOPT_HTTPHEADER => $header_data,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_POST => true,
CURLOPT_POSTFIELDS => $postData,
CURLOPT_HEADER => 1,
);
curl_setopt_array($ch, $curlOpts);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Length: 0'));
$answer = curl_exec($ch);
// If there was an error, show it
if (curl_error($ch)) {
die(curl_error($ch));
}
curl_close($ch);
echo '<pre>';
print_r($answer);
echo '</pre>';
// Will dump a beauty json :3
//var_dump(json_decode($result, true));
//echo json_encode($outp);
?>
I need json output:

bukalapak.com login using php curl

im using curl to login at bukalapak.com .
but always redirect "you are being redirected" & i solve it use curl followlocation true, but why not loged in after redirect?
help me & thanks before
<?php
function curl($url, $post){
$curl = curl_init();
curl_setopt_array($curl,
array(CURLOPT_URL => $url, //put your url here
CURLOPT_SSL_VERIFYPEER => false,
CURLOPT_SSL_VERIFYHOST => false,
CURLOPT_RETURNTRANSFER => true,
//CURLOPT_ENCODING => "",
CURLOPT_POSTREDIR => 3,
CURLOPT_HEADER => true,
//CURLOPT_MAXREDIRS => 10,
//CURLOPT_TIMEOUT => 30,
//CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "GET",
CURLOPT_POST => true,
CURLOPT_FOLLOWLOCATION => false,
CURLOPT_COOKIEFILE => "cookie.txt",
CURLOPT_COOKIEJAR => "cookie.txt",
CURLOPT_POSTFIELDS => $post,
CURLOPT_HTTPHEADER => array(
"cache-control: no-cache",
"Content-type: text/html; charset=UTF-8"
),
));
$response = curl_exec($curl);
$info = curl_getinfo($curl);
$err = curl_error($curl);
curl_close($curl);
return $response;
}
$p = array(
"user_session[username]"=>"Myuser#gmail.com",
"user_session[password]"=>"Blablabla",
"commit"=>"Login",
"comeback"=>"%2F"
);
$login = curl("https://www.bukalapak.com/user_sessions", http_build_query($p));
echo $login;
?>
HTTP/1.1 302 Found Server: nginx Date: Fri, 03 Feb 2017 06:28:03 GMT Content-Type: text/html; charset=utf-8 Location: https://www.bukalapak.com/ Transfer-Encoding: chunked Connection: keep-alive Status: 302 Found Cache-Control: max-age=0, private, must-revalidate, no-transform X-We-Need-To-Talk: Hey, you need to check out https://www.bukalapak.com/careers :) Vary: Origin X-Permitted-Cross-Domain-Policies: none X-XSS-Protection: 1; mode=block X-Request-Id: 90f9ca9a-4ade-4c3d-a0ea-592effd5c2ee X-Download-Options: noopen X-Found-Something: Found any vulnerabilities in our system? https://www.bukalapak.com/bug_reports X-Frame-Options: SAMEORIGIN X-Content-Type-Options: nosniff Set-Cookie: _mkra_ctxt=8891a37006424c9c608a41bf3f6558de--302; path=/; max-age=5; HttpOnly Set-Cookie: lskjfewjrh34ghj23brjh234=RjhGMUlkUEpHb0NOaTNLeUFoT2x2RmxZeWxDTWRySEV6aUc4a0tBOWhqbGdZVS9kWUlOZHFOSXNVZWt2UmJCTkFBRXZLVW8wd21rUktrYXZQR05lUU01aHRuemdzVi91WnFqcUFid2s0MVhUbGhmQXk0cGIrVU5iaXR3UW1HQ21LeHJHRHhMRGZucGo3UUluZ2FLbjJMSWZiZVExODgyRmtUQXJaMTZjYVQxMXZSOWg5U3FGeHZZTGdxVllNTFp4MGtDcnJiaEEwL0NkTVNNT2lTWE8yQXNtNEIzWTVKc0h4NVdVeEVKYnZMSVFMQ29KcysyUGdnK2VZaWNLaVozL3NXYzVIcXlHWkY4SDZESjF4RXp6ak5wVXVOeXRXdjZ0YkZ1UWhPQVJNcjA9LS1od2k3aTgzM3pBSEx5TDVqcE9ZWUZ3PT0%3D--0f995ca0f739bbb3f94b4e92b6628da326a2cb5c; domain=.bukalapak.com; path=/; HttpOnly Strict-Transport-Security: max-age=31536000; includeSubdomains You are being redirected.
first off, to login, you need a valid cookie session id and "authenticity_token" combo. each cookie session has its own unique authenticity_token , and if you don't post the correct 1, you won't be able to log in.
your code makes no attempt to attain an authenticity_token.
you are also missing a bunch of login parameters, which might make login impossible. to mention a few, utf8, comeback, ab_keep_login and commit (and there's more)
also, your code use multipart/form-data encoding, but the website use application/x-www-form-urlencoded encoding, so fix that (use http_build_query to generate a valid x-www-form-urlencoded string)
first make a GET request to the login page, make sure cookies are enabled, parse out all the <input> tags that belong the the login request (but warning, there are several forms in the html containing <input> tags, you only want the inputs belonging to the login form, aka id new_user_session), their names and values (use DOMDocument for that), modify the username/password variables, and send the login POST request with all those tags, and make sure to use application/x-www-form-urlencoded encoding (aka what http_build_query gives you), just like a real browser does.
using hhb_curl from https://github.com/divinity76/hhb_.inc.php/blob/master/hhb_.inc.php , here's an example (it should work, provided a real username/password)
<?php
declare(strict_types = 1);
require_once ('hhb_.inc.php');
$hc = new hhb_curl ();
$hc->_setComfortableOptions ();
$hc->exec ( 'https://www.bukalapak.com/user_sessions' );
$html = $hc->getResponseBody ();
$domd = #DOMDocument::loadHTML ( $html );
$inputs = array ();
foreach ( $domd->getElementById ( "new_user_session" )->getElementsByTagName ( "input" ) as $input ) {
$inputs [$input->getAttribute ( "name" )] = $input->getAttribute ( "value" );
}
var_dump ( $inputs );
assert ( array_key_exists ( 'authenticity_token', $inputs ) );
$inputs ['user_session[username]'] = 'Myuser#gmail.com';
$inputs ['user_session[password]'] = 'Blablabla';
$hc->setopt_array ( array (
CURLOPT_POST => true,
CURLOPT_POSTFIELDS => http_build_query ( $inputs ),
CURLOPT_URL => 'https://www.bukalapak.com/user_sessions',
CURLOPT_HTTPHEADER => array (
'Accept:text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8'
)
) );
$hc->exec ();
$html = $hc->getResponseBody ();
hhb_var_dump ( $hc->getStdErr (), $hc->getResponseBody () );

find the redirected url using curl

I am trying to find the redirected url from a source url to get this I am using the following code...
$url="http://www.idealo.fr/go/737821809.html?categoryId=12313&pos=1&price=499.99&productid=4716350&sid=26246&type=offer";
$ch = curl_init();
$timeout = 0;
curl_setopt ($ch, CURLOPT_URL, $url);
curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
curl_setopt($ch, CURLOPT_HEADER, TRUE);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_BINARYTRANSFER, 1);
$header = curl_exec($ch);
$retVal = array();
$fields = explode("\r\n", preg_replace_callback ('/\x0D\x0A[\x09\x20]+/', ' ', $header));
foreach( $fields as $field ) {
if( preg_match('/([^:]+): (.+)/m', $field, $match) ) {
$match[1] = preg_replace_callback ('/(?<=^|[\x09\x20\x2D])./e', 'strtoupper("\0")', strtolower(trim($match[1])));
if( isset($retVal[$match[1]]) ) {
$retVal[$match[1]] = array($retVal[$match[1]], $match[2]);
} else {
$retVal[$match[1]] = trim($match[2]);
}
}
}
echo '<pre>';
print_r($retVal);
echo '</pre>';
if (isset($retVal['Location'])){
echo $retVal['Location'];
} else {
echo $_GET[$urlKey];
}
curl_close($ch);
Now, it returns me the following output...
Array (
[date] => Tue, 06 Sep 2016 15:34:27 GMT
[server] => idealoAppServer
[location] => http://track.effiliation.com/servlet/effi.redir?id_compteur=13087834&url=http://www.priceminister.com/offer/buy/1134677256/canon-eos-750d-appareil-photo-numerique.html
[content-type] => text/html; charset=UTF-8
[content-length] => 0
[set-cookie] => Array
(
[0] => Array
(
[0] => oop_mvp_2=A; domain=.idealo.fr; path=/; expires=Thu, 05-Dec-2016 15:34:27 GMT
[1] => ipcuid=01jo0lb800isrmzsx0; domain=.idealo.fr; path=/; expires=Mon, 27-Aug-2018 15:34:27 GMT
)
[1] => icda=1; domain=.idealo.fr; path=/; expires=Wed, 06-Sep-2017 15:34:27 GMT
)
[vary] => Accept-Encoding,User-Agent
[connection] => close )
Now, from this array I just need the following output...
http://www.priceminister.com/offer/buy/1134677256/canon-eos-750d-appareil-photo-numerique.html
Can anyone please help me to form the array so that I just get the url only...I have recently upgraded to php7 from php 5...is that may be one of the reason...
You can use parse_url to parse the url you receive in location key
$url_parse = parse_url($retVal['location']);
After that you will have somehing like this in $url_parse:
array (
'scheme' => 'http',
'host' => 'track.effiliation.com',
'path' => '/servlet/effi.redir',
'query' => 'id_compteur=13087834&url=http://www.priceminister.com/offer/buy/1134677256/canon-eos-750d-appareil-photo-numerique.html',
)
So the query is in query key. Now we need to parse it and you cna use parse_str
parse_str($url_parse['query'], $output);
And now in $output you will have something like this:
array (
'id_compteur' => '13087834',
'url' => 'http://www.priceminister.com/offer/buy/1134677256/canon-eos-750d-appareil-photo-numerique.html',
)
So the url that you want is in $output['url']
echo $output['url']; //here is the url that you want.

CURL Ajax - XML Response - Charset

I'm about to scrape a Website with several Tabs. Up on each Tab click an AJAX-Request gets send to their server returning the data of the Tab which will be displayed.
Since I need to fetch those Data I checked the HTTP-Requests and manipulated the Header with "hurl.it"(website) to check the response.
I'm receiving the correct results but when i set up my Curl Session with the same Header the response is not the same/readable.
With the Live HTTP Headers Add On I was able to extract the AJAX - URL
Request
POST http://xxxx.xxx.xx/Organisation/AjaxScopeQualification/0e69a479-63e3-4d64-9340-f2e9cc8d84df?tabIndex=3
HEADERS
Content-Type: application/xml
X-Requested-With: XMLHttpRequest
Referer: http://xxxx.xxx.xx/Organisation/Details/41283
Response via hurl.it
200 OK 646 bytes 547 ms
HEADERS
Cache-Control: private
Content-Encoding: gzip
Content-Length: 382
Content-Type: application/json; charset=utf-8
Date: Fri, 29 Jan 2016 01:36:42 GMT
Server: Microsoft-IIS/7.5
Set-Cookie: .ASPXANONYMOUS=fsbx3gX1CykkKL2OIvPFH9GcPj97KEPkK-6WVTA24eI87k0F3gjpt0fyVA2P90S8heeaoqjUps9-UFtzgm8mRAiPqnbS50kytk_NY5K4yHPwa-5l0kCqNzPAo0yjBsPmbisbg3N7P7h6Oz5EdRaN8Fkr0y3G6wdIILI8yMQBj1S1X4GULf9rpQ8IvvSo13KB0; expires=Fri, 29-Jan-2016 03:36:42 GMT; path=/; HttpOnly
X-Aspnet-Version: 4.0.30319
X-Aspnetmvc-Version: 3.0
X-Powered-By: ASP.NET
BODY
{"data":[
{"Id":"9fe29051-31e6-4bfa-a2f1-194d70c0aab9","NrtId":"930ec525-2199-44a9-bc27-c1b28524c9bf","RtoId":"0e69a479-63e3-4d64-9340-f2e9cc8d84df","TrainingComponentType":2,"Code":"TLI41210","Title":"Certificate IV in Transport and Logistics (Road Transport - Car Driving Instruction)","IsImplicit":false,"ExtentId":"01","Extent":"Deliver and assess","StartDate":new Date(2011,11,7,0,0,0),"EndDate":new Date(2016,11,6,0,0,0),"DeliveryNsw":true,"DeliveryVic":true,"DeliveryQld":true,"DeliverySa":true,"DeliveryWa":true,"DeliveryTas":true,"DeliveryNt":true,"DeliveryAct":true,"ScopeDecisionType":0,"ScopeDecision":"Deliver and assess"}],"total":1}
**Response from CURL - var_dump() **
string(382) "�m��j�0�_E蔀����|+�=�B�Kz(=��q8���ICȻWζiq�t��������{ ����y�r;��r�D���#��P���t����Ǚ.�Z������ZaX�;�N�z����~(�[Jor��������7F��H1h������E~�!����aJ#��'䭮�>���Mg�Vr��Ǚ��ȊK�S��A��&݇L�evu���Sl3;�ᱴd]�4�pR�.�]��1�#�`�X��?��ty����p�8����1�R=�t(S�6�[�+-����Vr9��#���f�4���������2#�Ew��їѯ� ���r��FGZ�O��\���.䲰�7���f^�W���[��;Z���"
Is that a charset problem or am I setting my Curl Options wrong?
CURL
$url = http://xxxx.xxx.xx/Organisation/AjaxDetailsLoadScope/e11d03e7-37e7-49e8-be54-0bed8eb1c247?_=1454029562507&tabIndex=3
$header = array(
'Accept: */*',
'Accept-Encoding: gzip, deflate',
'Content-Length: 0',
'Content-Type: application/xml',
'X-Requested-With: XMLHttpRequest',
"Referer: http://xxxx.xxx.xx/Organisation/Details/$this->code"
);
//..
//$header and $url are saved in arrays and then passed to curlMulti()
function curlMulti($urls, $headers = false) {
$mh = curl_multi_init();
// For each of the URLs in array
foreach ($urls as $id => $d) {
$ch[$id] = curl_init();
$url = (is_array($d) && !empty($d['url'])) ? $d['url'] : $d;
if (is_array($headers) && $headers[$id] != false) {
curl_setopt($ch[$id], CURLOPT_POST, 1);
curl_setopt($ch[$id], CURLOPT_HTTPHEADER, $headers[$id]);
}
curl_setopt($ch[$id], CURLOPT_URL, $url);
curl_setopt($ch[$id], CURLOPT_RETURNTRANSFER, TRUE);
curl_multi_add_handle($mh, $ch[$id]);
}
$running = NULL; // Set $running to NULL
do {
curl_multi_exec($mh, $running);
} while ($running > 0); // While $running is greater than zero
foreach ($ch as $id => $content) {
$results[$id] = curl_multi_getcontent($content);
curl_multi_remove_handle($mh, $content);
}
curl_multi_close($mh);
return $results;
}
I was playing a little bit around with the Headers and got it working now..
had to delete 'Accept: */*',
'Accept-Encoding: gzip, deflate' in the header
$header = array(
'Content-Length: 0',
'Content-Type: application/xml',
'X-Requested-With: XMLHttpRequest',
"Referer: http://xxxx.xxx.xx/Organisation/Details/$this->code"
);
works like a charm:
stdClass Object
(
[data] => Array
(
[0] => stdClass Object
(
[Id] => 9fe29051-31e6-4bfa-a2f1-194d70c0aab9
[NrtId] => 930ec525-2199-44a9-bc27-c1b28524c9bf
[RtoId] => 0e69a479-63e3-4d64-9340-f2e9cc8d84df
[TrainingComponentType] => 2
[Code] => TLI41210
[Title] => Certificate IV in Transport and Logistics (Road Transport - Car Driving Instruction)
[IsImplicit] =>
[ExtentId] => 01
[Extent] => Deliver and assess
[DeliveryNsw] => 1
[DeliveryVic] => 1
[DeliveryQld] => 1
[DeliverySa] => 1
[DeliveryWa] => 1
[DeliveryTas] => 1
[DeliveryNt] => 1
[DeliveryAct] => 1
[ScopeDecisionType] => 0
[ScopeDecision] => Deliver and assess
)
)
[total] => 1
)

php get location header after post request

I need to get the location header the following request:
This is what the request looks like from the chrome network dev tool when I execute it in chrome.
here is the redirect request in chrome network dev tool, just in case its needed for some reason.
I've tried the following things:
this seems to only give me an empty answer:
<?php
$url = $_GET["url"];
if (0 === preg_match('/form_build_id" value="form-([^"]*)"/', file_get_contents('http://anything2mp3.com/'), $matches)) exit("false");
$id = $matches[1];
$params = array('http' => array(
'method' => 'POST',
'content' => array(
'url' => $url,
'op' => "Convert",
'form_build_id' => "form-" . $id,
'form_id' => "videoconverter_convert_form"
),
'max_redirects' => "0",
'header' => "Content-Type:application/x-www-form-urlencoded"
));
stream_context_set_default($params);
$headers = get_headers("http://anything2mp3.com/?q=&mobile_detect_caching_notmobile&mobile_detect_caching_nottablet", false, $ctx);
echo implode("\n", $headers);
?>
i've also tried this:
<?php
$url = $_GET["url"];
if (0 === preg_match('/form_build_id" value="form-([^"]*)"/', file_get_contents('http://anything2mp3.com/'), $matches)) exit("false");
$id = $matches[1];
$params = array('http' => array(
'method' => 'POST',
'content' => array(
'url' => $url,
'op' => "Convert",
'form_build_id' => "form-" . $id,
'form_id' => "videoconverter_convert_form"
),
'max_redirects' => "0",
'header' => "Content-Type:application/x-www-form-urlencoded"
));
$ctx = stream_context_create($params);
$file = fopen("http://anything2mp3.com/?q=&mobile_detect_caching_notmobile&mobile_detect_caching_nottablet", 'rb', false, $ctx);
$headers = $http_response_header;
echo implode("\n", $headers);
?>
the last one gives me the following result:
HTTP/1.1 200 OK
Date: Sun, 26 Jul 2015 18:20:20 GMT
Content-Type: text/html; charset=utf-8
Connection: close
Set-Cookie: __cfduid=db7807d30a61600d31a18cc1a725150811437934820; expires=Mon, 25-Jul-16 18:20:20 GMT; path=/; domain=.anything2mp3.com; HttpOnly
Vary: Accept-Encoding
X-Cookie-Domain: .anything2mp3.com
Expires: Tue, 24 Jan 1984 08:00:00 GMT
Last-Modified: Sun, 26 Jul 2015 18:20:20 GMT
Cache-Control: no-cache, must-revalidate, post-check=0, pre-check=0
ETag: W/"1437934820"
Content-Language: en
X-Device: normal
X-GeoIP-Country-Code: US
X-GeoIP-Country-Name: United States
X-Speed-Cache-Key: /?q=&mobile_detect_caching_notmobile&mobile_detect_caching_nottablet
X-NoCache: Method
X-This-Proto: http
X-Server-Name: anything2mp3.com
Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0
Server: cloudflare-nginx
CF-RAY: 20c21e3333eb23a8-IAD
as you can see, there is no location header.
note that I've seen some similar posts already (like question 6532753, I couldn't link it because I'm already at my 2 link limit), but they don't seem to work for me.
How do I retrieve the location header from request like marked in the original screenshot? What am I missing?
edit: I messed up pretty hard when copying my code, sorry for that
I eventually got it to work using curl like #frz3993 suggested (and after sending in a blank 'cookies' header, the server didn't seem to like it without that)
here is the code that I use now:
<?php
$url = $_GET["url"];
if (0 === preg_match('/form_build_id" value="form-([^"]*)"/', file_get_contents('http://anything2mp3.com/'), $matches)) exit("false");
$longid = $matches[1];
$ch = curl_init("http://anything2mp3.com/?q=&mobile_detect_caching_notmobile&mobile_detect_caching_nottablet");
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query(array(
'url' => $url,
'op' => "Convert",
'form_build_id' => "form-" . $longid,
'form_id' => "videoconverter_convert_form"
)));
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
"Content-Type:application/x-www-form-urlencoded",
"Cookie:"
));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, false);
$data = curl_exec($ch);
if (0 === preg_match('/Location: (.*id=(\d+))/', $data, $matches)) exit("false");
$redirect = $matches[1];
$id = $matches[2];
echo $redirect;
echo " - ";
echo $longid;
die();
?>
note that even tho the second suggestion from my OP gave me headers, it did not give me the location header which curl does now. (don't ask me why :p)

Categories