PHP, cURL, and Roblox. cURL continues to redirect to error page - php

I am making an HTTP API for use in Roblox. I am using PHP and cURL to do so.
However, there is one method on which I am stuck. That is sending messages through roblox in PHP. I have captured the message sending request using Fiddler. I have put all the headers into my cURL request.
I also am fetching the .ROBLOSECURITY cookie, the X-CSRF Token, and the RBXSessionTracker and __RequestVerification cookies.
Here is the messaging code for those of you want to help:
$RecipientId=$_POST['RecipientId'];
$Subject=$_POST['Subject'];
$Message=$_POST['Message'];
$AuthCookie=decodeJSON($_POST['Login']);
$AuthCookie=$AuthCookie[0];
$TokenCurl=curl_init('roblox.com/build/upload?groupId=1');
curl_setopt_array($TokenCurl,array(
CURLOPT_HTTPGET => 1,
CURLOPT_FRESH_CONNECT => 1,
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_HEADER => 1,
CURLOPT_HTTPHEADER => array('Cookie: .ROBLOSECURITY='.$AuthCookie),
));
$TokenResponse=curl_exec($TokenCurl);
$TokenHeaderSize=curl_getinfo($TokenCurl,CURLINFO_HEADER_SIZE);
$TokenCurlError=curl_error($TokenCurl);
$Cookies=GetCookies(substr($TokenResponse,0,$TokenHeaderSize));
$SessionTracker=$Cookies['RBXSessionTracker'];
$RequestVerification=$Cookies['__RequestVerificationToken'];
$AuthCookie='.ROBLOSECURITY='.$AuthCookie.'; RBXSessionTracker='.$SessionTracker.'; __RequestVerificationToken='.$RequestVerification.';';
curl_close($TokenCurl);
if($TokenCurlError!==''){
die($TokenCurlError);
};
$Cache=time();
preg_match("\`<script\stype=\"text/javascript\">\s*?Roblox\.XsrfToken\.setToken\('(.*?)'\);\s*?</script>\`",$TokenResponse,$Matches);
$Token=$Matches[1];
if(empty($Token)){
die("Could not get X-CSRF Token");
};
$PostData=json_encode(array(subject=>$Subject,body=>$Message,recipientid=>$RecipientId,cacheBuster=>$Cache));
$Curl=curl_init('roblox.com/messages/send');
curl_setopt_array($Curl,array(
CURLOPT_USERAGENT => 'Mozilla/5.0 (Windows NT 6.3; WOW64; rv:39.0) Gecko/20100101 Firefox/39.0',
CURLOPT_POST => 1,
CURLOPT_FRESH_CONNECT => 1,
CURLOPT_HEADER => 1,
CURLOPT_POSTFIELDS => $PostData,
CURLOPT_HTTPHEADER => array('Content-Length: '.strlen($PostData),'X-CSRF-TOKEN: '.$Token,'Referer: www.roblox.com/messages/compose?recipientId='.$RecipientId,'X-Requested-With: XMLHttpRequest','Cookie: '.$AuthCookie,'Accept: application/json, text/javascript, */*; q=0.01','Accept-Language: en-US,en;q=0.5','Accept-Encoding: gzip, deflate','Connection: keep-alive','Pragma: no-cache','Cache-Control: no-cache'),
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_FOLLOWLOCATION => 1,
CURLOPT_VERBOSE => 1
));
$ResponseBody=curl_exec($Curl);
echo $ResponseBody;
$HTTPResponseCode=curl_getinfo($Curl,CURLINFO_HTTP_CODE);
$HTTPHeaderSize=curl_getinfo($Curl,CURLINFO_HEADER_SIZE);
$ResponseHeader=substr($ResponseBody,0,$HTTPHeaderSize);
$CurlError=curl_error($Curl);
curl_close($Curl);
if($CurlError!==''){
die($CurlError);
};
$MessageResult=decodeJSON(substr($ResponseBody,$HTTPHeaderSize));
echo json_encode(array(Message=>$MessageResult['message']));
I removed the 'http://' from the links because of reputation.
Everything works, it just will not send the message. Does anyone know what exactly you need to send a message?

Related

Why curl giving 1020 error while postman giving response [duplicate]

This question already has an answer here:
curl 1020 error when trying to scrape page using bash script
(1 answer)
Closed yesterday.
`I have sent a GET (Also POST) method to get data from api but the response is error 1020 and at the same time post man tried and its giving response then what is wrong here.
I am not able to share the api url due to security reasons.
<?php
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => 'url_with_parameter',
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => '',
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 0,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => 'GET',
CURLOPT_HTTPHEADER => array(
'Cookie: PHPSESSID=f1r22a2srpgamnidkflgqsuem2; csrf_token=c31d6332c0066fa652b79948c43e5dd7_d5ec9637ea9c6914f3cebeab7fa4ad4d',
'Host' => "localhost",
'User-Agent' => 'PostmanRuntime/7.31.0',
'Accept' => '*/*',
'Accept-Encoding' => 'gzip, deflate, br',
'Connection' => 'keep-alive'
),
));
$response = curl_exec($curl);
curl_close($curl);
echo $response;
When 1020 error is returned by curl, it means typically that the request was blocked by security settings, which can include challenges like CAPTCHAs or browser checks.
Try to add UA header into your curl request, like
$headers = [
'User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/110.0.0.0 Safari/537.36',
];
curl_setopt($curl_handle, CURLOPT_HTTPHEADER, $headers);

Download Whatsapp Business Media Image API

I am Using Whatsapp Media Api to get Image send from user to business number using webhooks and i also get the Image ID and url and Image as well in Postman but when I use that curl request made from Postman it alwasy shows facebook error:
Sorry, something went wrong.
We're working on it and we'll get it fixed as soon as we can.
But its working fine in Postman what am i doing wrong?
<?php
$ua = 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.16 (KHTML, like Gecko) \
Chrome/24.0.1304.0 Safari/537.16';
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => 'Image_URL_from_Api',
CURLOPT_USERAGENT => $ua,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => '',
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 0,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => 'GET',
CURLOPT_SSL_VERIFYPEER => false,
CURLOPT_HTTPHEADER => array(
'Authorization: Bearer My_Token'
),
));
$response = curl_exec($curl);
curl_close($curl);
echo $response;
OK, I've got the answer I needed to give the user_agent value which is in postman it is automatically putting CURLOPT_USERAGENT => 'PostmanRuntime/7.26.10' in the header but for curl, I've added CURLOPT_USERAGENT => 'curl/7.64.1', and it worked.
https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/User-Agent

curl request works from one server and give 403 on another

I am using curl to hit a url and get contents from it and below is the code
$url = 'http://uber.com';
$ch = curl_init();
curl_setopt_array($ch, array(
CURLOPT_URL => $url,
CURLOPT_HEADER => false,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_SSL_VERIFYPEER => false,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_USERAGENT => 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.16 (KHTML, like Gecko) \ Chrome/24.0.1304.0 Safari/537.16',
CURLOPT_CONNECTTIMEOUT => 180,
CURLOPT_TIMEOUT => 180 + 60,
CURLOPT_COOKIE => 'lang=en;'
));
$response = curl_exec($ch);
var_dump($response);
It gives me the content when I execute the script on local server but while executing this on staging and production server, situated in US, it give 403-forbidden error. I also tested the same from another server in US and it works fine.
Any help is appreciated.

PHP cURL login fails

There is a website http://www.tremorgames.com and I want to do a cURL login. I have a code which I use on my server http://shalva97.x10.mx/tremorlogin/ but it just does not work, but after a lot of testing I decided to install Apache on my PC and tested the exactly same code and it worked.
Here is the code :
<?php
$url="http://www.tremorgames.com/index.php";
$postdata = "loginuser=shalva&loginpassword=shalva&Submit=&saveme=1";
$ch = curl_init();
$f = fopen('request.txt', 'w');
curl_setopt_array($ch, array(
CURLOPT_URL => $url,
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_FOLLOWLOCATION => 1,
CURLOPT_VERBOSE => 1,
CURLOPT_STDERR => $f,
CURLOPT_USERAGENT => 'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:37.0) Gecko/20100101 Firefox/37.0',
CURLOPT_POST => true,
CURLOPT_POSTFIELDS => $postdata,
CURLOPT_HEADER => 1,
CURLOPT_HTTPHEADER => array("Accept-Language: en-US;q=0.6,en;q=0.4", "Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8", "Connection: keep-alive"),
CURLOPT_COOKIEFILE => "cookies.txt", //Save cookies
CURLOPT_COOKIEJAR => "cookies.txt", //Cookies located
CURLOPT_REFERER => 'http://www.tremorgames.com/index.php',
CURLOPT_ENCODING => 'gzip,deflate'
));
echo curl_exec($ch);
fclose($f);
curl_close($ch);
?>
Executing this same code on both my PC and server, I get different headers, on PC it receives multiple "Set-Cookie" header, but on server it is only one.
Why it does not work on the server? Why it works on my PC?
well after a lot of thinking i found out that it is not only headers is sent but the ip and my location. The site does not allow logins from other countries thats why it didnot loged in, the code is correct and it was all about server location. i hope this will help others who also try to do same thing.

curl_setopt_array throwing warning message

I am trying to login into a website through cURL and have it return to me the actual page. So far I can only get the footer of the page with the feedback link and this error:
Warning: curl_setopt_array() [function.curl-setopt-array]: Array keys
must be CURLOPT constants or equivalent integer values on line 18
Loading
line 18 is the ");" at the end of the curl array
So far I've been able to figure out that if I just put in the website name with the access_token in my url from my browser I'll automatically be logged in so I'm trying to get cURL to emulate that.
<?php
$ckfile =' __utma=173730677.1410450142.1370837396.1370843059.4; __utmz=173730677.1370843059.4.3.utmcsr=web.com|utmccn=(referral)|utmcmd=referral|utmcct=/ou; __utma=64278953.892306882.1370766510.1370838026.4; __utmz=64278953.4.3.utmcsr=.com|utmccn=(referral)|utmcmd=referral|utmcct=/signin; __utmc=64278953; __utmc=173730677; __utmb=173730677.5.10.1370843059; __utmb=64278953.1.10';
$ch = curl_init();
curl_setopt_array(
$ch,
array(
CURLOPT_SSL_VERIFYPEER => false,
CURLOPT_URL => 'https://web.com',
CURLOPT_COOKIEFILE=> $ckfile,
CURLOPT_USERAGENT =>'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:21.0) Gecko/20100101 Firefox/21.0',
CURLOPT_GET=> true,
CURLOPT_REFERER => 'https://.com/signin',
CURLOPT_GETFIELDS=>'#access_token=',
CURLOPT_RETURNTRANSFER => true
)
);
$response = curl_exec($ch);
curl_close($ch);
echo '<pre>';
print_r($response);
?>
CURLOPT_GET and CURLOPT_GETFIELDS are not a valid options for cURL. The options may exist for POST, but they do not for GET. Just add the query string to the URL instead.
Also, make sure all the options are set to what cURL expects them to be set to. CURLOPT_COOKIEFILE should be set to a filename, not a string of cookie values. You want CURLOPT_COOKIE instead.
curl_setopt_array($ch, array(
CURLOPT_SSL_VERIFYPEER => false,
CURLOPT_URL => 'https://web.com?access_token=abc',
CURLOPT_COOKIE=> $ckfile,
CURLOPT_USERAGENT =>'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:21.0) Gecko/20100101 Firefox/21.0',
CURLOPT_REFERER => 'https://web.com/signin',
CURLOPT_RETURNTRANSFER => true
));
Please see the PHP cURL docs for all the options: http://php.net/manual/en/function.curl-setopt.php

Categories