I'm trying to do a simple POST curl request to an IP without a domain. The response is as shown:
Error:Failed to connect to 213.177.9.75 port 9998: Connection refused
My code is:
$jud='BC';
$nr ='22';
$lit='BAC';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'http://213.177.9.75:9998/verf_nr');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS,
"jud=$jud&nr_id=$nr&lit=$lit&_token=V6O6u3Nl9Fc26VkUqspnQXFB2sXrwbPkYcqD0fYl");
curl_setopt($ch, CURLOPT_ENCODING, 'gzip, deflate');
$headers = array();
$headers[] = 'Connection: keep-alive';
$headers[] = 'Pragma: no-cache';
$headers[] = 'Cache-Control: no-cache';
$headers[] = 'Upgrade-Insecure-Requests: 1';
$headers[] = 'Origin: http://213.177.9.75:9998';
$headers[] = 'Content-Type: text/plain';
$headers[] = 'User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.75 Safari/537.36';
$headers[] = 'Accept: */*';
$headers[] = 'Referer: http://213.177.9.75:9998/';
$headers[] = 'Accept-Language: ro-RO,ro;q=0.9,en-US;q=0.8,en;q=0.7,de;q=0.6';
$headers[] = 'Sec-Fetch-Site: cross-site';
$headers[] = 'Sec-Fetch-Mode: cors';
$headers[] = 'Sec-Fetch-Dest: font';
$headers[] = 'Content-Length: 0';
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$result = curl_exec($ch);
if (curl_errno($ch)) {
echo 'Error:' . curl_error($ch);
}
curl_close($ch);
print_r($result);
It won't connect at all even though through a browser, it all works perfectly. I simply want to get the HTML returned by the redirect of the request above and use DOMDocument on it.
http://213.177.9.75:9998/publica
write BC in 'Judet', 22 in 'Nr.' and BAC in 'AAA' and check network tab for the specific request.
I appreciate any ideas. Thank you!
Related
I am trying to call an API using PHP and cURL. The GraphQL-API returns the above mentioned error message. My PHP-code looks like this:
// Generated by curl-to-PHP: http://incarnate.github.io/curl-to-php/
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://app-money.tmx.com/graphql');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, "^{^^operationName^^\":^^\"getDividendsForSymbol^^\",^^\"variables^^\":^{^^\"symbol^^\":^^\"TRP^^\",^^\"page^^\":1,^^\"batch^^\":10^},^^\"query^^\":^^\"query");
curl_setopt($ch, CURLOPT_ENCODING, 'gzip, deflate');
$headers = array();
$headers[] = 'Authority: app-money.tmx.com';
$headers[] = 'Accept: */*';
$headers[] = 'Accept-Language: en-US,en;q=0.9,de-CH;q=0.8,de;q=0.7';
$headers[] = 'Authorization: ';
$headers[] = 'Content-Type: application/json';
$headers[] = 'Locale: en';
$headers[] = 'Origin: https://money.tmx.com';
$headers[] = 'Referer: https://money.tmx.com/';
$headers[] = 'Sec-Ch-Ua: ^^.Not/A)Brand^^\";v=^^\"99^^\",';
$headers[] = 'Sec-Ch-Ua-Mobile: ?0';
$headers[] = 'Sec-Ch-Ua-Platform: ^^Windows^^\"\"';
$headers[] = 'Sec-Fetch-Dest: empty';
$headers[] = 'Sec-Fetch-Mode: cors';
$headers[] = 'Sec-Fetch-Site: same-site';
$headers[] = 'User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/103.0.0.0 Safari/537.36';
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$result = curl_exec($ch);
var_dump($result);
if (curl_errno($ch)) {
echo 'Error:' . curl_error($ch);
}
curl_close($ch);
What am I doing wrong?
$username = "login";
$password = "pass";
$cookie = $username.".txt";
$useragent = "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Ubuntu Chromium/50.0.2661.102 Chrome/50.0.2661.102 Safari/537.36";
$url = 'https://instagram.com/';
$arrSetHeaders = array(
"User-Agent: $useragent",
'Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
'Accept-Language: en-US,en;q=0.5',
'Accept-Encoding: deflate, br',
'Connection: keep-alive',
'cache-control: max-age=0'
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_HTTPHEADER, $arrSetHeaders);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_COOKIEJAR, dirname(__FILE__)."/".$cookie);
curl_setopt($ch, CURLOPT_COOKIEFILE, dirname(__FILE__)."/".$cookie);
curl_setopt($ch, CURLOPT_USERAGENT, $useragent);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_POSTREDIR, 3);
$page = curl_exec($ch);
curl_close($ch);
echo $page;
sleep(5);
preg_match_all('/^Set-Cookie:\s*([^;]*)/mi', $page, $matches);
$cookieFileContent = '';
foreach($matches[1] as $item)
{
$cookieFileContent .= "$item; ";
}
$cookieFileContent = rtrim($cookieFileContent, '; ');
$cookieFileContent = str_replace('sessionid=; ', '', $cookieFileContent);
$oldContent = file_get_contents(dirname(__FILE__)."/".$cookie);
$oldContArr = explode("\n", $oldContent);
if(count($oldContArr)) {
foreach($oldContArr as $k => $line){
if(strstr($line, '# ')){
unset($oldContArr[$k]);
}
}
$newContent = implode("\n", $oldContArr);
$newContent = trim($newContent, "\n");
file_put_contents( dirname(__FILE__)."/".$cookie, $newContent);
}
preg_match('|csrftoken(.*)|', file_get_contents(dirname(__FILE__)."/".$cookie), $csrf);
$crf = trim($csrf[1]);
$arrSetHeaders = array(
"User-Agent: $useragent",
"X-CSRFToken: $crf",
"x-csrftoken: $crf",
'X-Instagram-AJAX: 1',
'X-Requested-With: XMLHttpRequest',
"Referer: https://instagram.com",
'Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
'Accept-Language: en-US,en;q=0.5',
'Accept-Encoding: deflate, br',
'Connection: keep-alive',
'cache-control: max-age=0',
);
$post = array('username' => $username, 'password' => $password, 'csrfmiddlewaretoken' => $crf);
$url = 'https://instagram.com/accounts/login/ajax/';
curl_setopt($ch, CURLOPT_HTTPHEADER, $arrSetHeaders);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_COOKIEJAR, dirname(__FILE__)."/".$cookie);
curl_setopt($ch, CURLOPT_COOKIEFILE, dirname(__FILE__)."/".$cookie);
curl_setopt($ch, CURLOPT_USERAGENT, $useragent);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_REFERER, 'https://instagram.com/');
curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
curl_setopt($ch, CURLOPT_POSTREDIR, 3);
$page = curl_exec($ch);
curl_close($ch);
var_dump($page);
returns bool(false)
what is wrong? how to log in instagram using curl request to accounts/login/ajax/
UPDATE:
I'm able to do a request in this way, i'm using curl.
I don't use any sort of cookie. Just the straight request
curl -H "Content-Type: application/x-www-form-urlencoded"
-H "Accept: */*"
-H "Accept-Encoding: gzip, deflate, br"
-H "Host: www.instagram.com"
-H "Origin: https://www.instagram.com"
-H "User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_6) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/14.0.2 Safari/605.1.15"
-H "Referer: https://www.instagram.com/accounts/login/"
-H "Connection: keep-alive"
-H "X-Requested-With: XMLHttpRequest"
-H "X-IG-WWW-Claim: 0"
-H "X-CSRFToken: $token"
--data "username=$us&enc_password=%23PWD_INSTAGRAM_BROWSER%3A0%3A$rand%3A$line&queryParams=%7B%7D&optIntoOneTap=false" --compressed "https://www.instagram.com/accounts/login/ajax/"
^^^ ^^^^^ ^^^^^
$token is a 32 LENGTH ALPHANUMERIC STRING
Example: ZHMFltOnFJZMPdXyFdCCTsJrzEfSmGCg
$us is the username in PLAIN ASCII TEXT
Example: myaccount
$rand is a 10 LENGTH DIGIT STRING
Example: 9719014829
$line is the password in PLAIN ASCII TEXT
Example: mypassword
There are 3 possibile responses if the username is correct
authenticated:true -> Password was ok
authenticated:false -> Password not ok
checkpoint_required -> Password ok but request was blocked, the user has 2FA or
the ip is blacklisted
:)
I need to track a tracking number in this tracking website. However, the form is in iFrame div, how do I POST request using CURL in PHP? Below is my code.
iFrame src:
https://home.abxexpress.com.my/track_multiple.html
Original src:
https://abxexpress.com.my/Home/Tracking
Controller:
public function track($trackingID)
{
$data = array(
'tairbillno' => $trackingID
);
print_r($data);
$url = file_get_contents('https://home.abxexpress.com.my/track_multiple.html');
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
$result = curl_exec($curl);
curl_close($curl);
echo $result;
}
Sample of tracking number:
99951513362
you can use this php-curl Code to send a simple post request to callback-page to get tracking info in html format,
after you can print content direct in your website(its work correctly,with the original style ), also you can use this content to extract all info you want , by any DOM extract librery like : php DOMDocument
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://home.abxexpress.com.my/track_multipleResult.asp?vsearch=True');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, "tairbillno=99951513362");//replace number your traking number
curl_setopt($ch, CURLOPT_ENCODING, 'gzip, deflate');
$headers = array();
$headers[] = 'Connection: keep-alive';
$headers[] = 'Pragma: no-cache';
$headers[] = 'Cache-Control: no-cache';
$headers[] = 'Upgrade-Insecure-Requests: 1';
$headers[] = 'User-Agent: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.122 Safari/537.36';
$headers[] = 'Origin: https://home.abxexpress.com.my';
$headers[] = 'Content-Type: application/x-www-form-urlencoded';
$headers[] = 'Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9';
$headers[] = 'Sec-Fetch-Site: same-origin';
$headers[] = 'Sec-Fetch-Mode: navigate';
$headers[] = 'Sec-Fetch-User: ?1';
$headers[] = 'Sec-Fetch-Dest: document';
$headers[] = 'Referer: https://home.abxexpress.com.my/track_multiple.html';
$headers[] = 'Accept-Language: it,it-IT;q=0.9,en-US;q=0.8,en;q=0.7,ar;q=0.6';
$headers[] = 'Cookie: ASPSESSIONIDAWDDSADQ=LLOJGMICLOOKHGEHJHPBMBKE';
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$result = curl_exec($ch);
if (curl_errno($ch)) {
echo 'Error:' . curl_error($ch);
}
curl_close($ch);
I'm trying to run a PHP curl GET in domain https://www.submarino.com.br. I'm getting 403 (Forbidden). I then tried by the chrome developper to copy the curl request provided in the url and run in bash and get the same answer
My PHP code
public function GetStatusHost(){
$headers = Array();
$headers[] = 'Host:www.submarino.com.br';
$headers[] = 'Connection:keep-alive';
$headers[] = 'Cache-Control:max-age=0';
$headers[] = 'Upgrade-Insecure-Requests: 1';
$headers[] = 'Accept:text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8';
$headers[] = 'Accept-Language:pt-BR,pt;q=0.9,en-US;q=0.8,en;q=0.7,es;q=0.6,pt-PT;q=0.5';
$headers[] = 'Accept-Encoding:gzip, deflate, br';
$headers[] = "If-None-Match:W/'5beff-TgqN41ZtNiOTyAH2bpA4lvYiKTE'";
$ch = curl_init($this->GetKeywordUrl());
$tmp = "/home/leilao/public_html/tmp/curl_cookie/cookie.txt";
curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/62.0.3202.94 Safari/537.36");
curl_setopt($ch, CURLOPT_HTTPHEADER,$headers);
//curl_setopt($ch, CURLOPT_COOKIEJAR, $tmp);
//curl_setopt($ch, CURLOPT_COOKIEFILE,$tmp);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
$result = curl_exec($ch);
$info = curl_getinfo($ch);
curl_close($ch); echo $info['http_code']; exit;
return $info['http_code'];
}
Bash
Any ideas how to fixed this problem?
Response using header SomeHugeOAuthaccess_tokenThatIReceivedAsAString:
Maybe your site (Host:www.submarino.com.br) use an auth credentials to provide the permission for method access.
So try to add this lines with a specific credentials:
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($ch, CURLOPT_USERPWD, "username:password"); //Your credentials goes here
or
if there a base64string Authorization is used then you can use this also
$header[] = "Authorization: Bearer $access_token";
or
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Authorization', 'OAuth ' . $atoken));
include_once('simple_html_dom.php');
$usuario = "username";
$password = "password";
$url = 'https://www.instagram.com/';
$url_login = 'https://www.instagram.com/accounts/login/ajax/';
$user_agent = array("Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 ",
"(KHTML, like Gecko) Chrome/48.0.2564.103 Safari/537.36");
$ch = curl_init();
$headers = [
'Accept-Encoding: gzip, deflate',
'Accept-Language: en-US;q=0.6,en;q=0.4',
'Connection: keep-alive',
'Content-Length: 0',
'Host: www.instagram.com',
'Origin: https://www.instagram.com',
'Referer: https://www.instagram.com/',
'User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/48.0.2564.103 Safari/537.36',
'X-Instagram-AJAX: 1',
'X-Requested-With: XMLHttpRequest'
];
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
curl_setopt($ch, CURLOPT_COOKIEFILE, "/tmp/cookie/pruebalogininsta2.txt");
curl_setopt($ch, CURLOPT_REFERER, $sTarget);
curl_setopt($ch, CURLOPT_HEADER, TRUE);
$html = curl_exec($ch);
preg_match_all('/^Set-Cookie:\s*([^;]*)/mi', $html, $matches);
$cookies = array();
foreach($matches[1] as $item) {
parse_str($item, $cookie);
$cookies = array_merge($cookies, $cookie);
}
$headers = [
'Accept-Encoding: gzip, deflate',
//'Accept-Language: ru-RU,ru;q=0.8,en-US;q=0.6,en;q=0.4',
'Accept-Language: en-US;q=0.6,en;q=0.4',
'Connection: keep-alive',
'Content-Length: 0',
'Host: www.instagram.com',
'Origin: https://www.instagram.com',
'Referer: https://www.instagram.com/',
'User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/48.0.2564.103 Safari/537.36',
'X-Instagram-AJAX: 1',
'X-Requested-With: XMLHttpRequest'
];
$cadena_agregar_vector = 'X-CSRFToken:'. $cookies["csrftoken"];
$headers[] = $cadena_agregar_vector ;
$sPost = "username=".$usuario . "&password=". $password ;
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_POSTFIELDS, $sPost);
curl_setopt($ch, CURLOPT_URL, $url_login);
$html2 = curl_exec($ch);
curl_setopt($ch, CURLOPT_URL, "http://www.instagram.com/");
$html4 = curl_exec($ch);
echo $html4;
this is what I get
the problem is the way you hardcode Accept-Encoding: gzip, deflate, this makes curl send the encoding header indeed, but it does not turn on the decoding feature of curl, thus you get the raw data, without curl decoding it for you.
remove 'Accept-Encoding: gzip, deflate', and add curl_setopt($ch, CURLOPT_ENCODING, 'gzip, deflate'); , and curl will decode it for you (provided that curl is compiled with gzip & deflate support) - or better yet, just do curl_setopt($ch, CURLOPT_ENCODING, ''); , and curl will automatically list all supported encodings, so you dont run into the encoding problem where curl isn't compiled with gzip support.
on an unrelated note, you probably want to use CURLOPT_USERAGENT, not set the user-agent header manually. else, the UA-string will just be sent with this 1 request, and be reset on the next request, while CURLOPT_USERAGENT is kept until curl_close($ch)
edit: on my first revision of this post, i wrote CURLOPT_POSTFIELDS instead of CURLOPT_ENCODING, sorry, fixed that
edit 2: on another unrelated note, you're encoding the username/password wrong. instead of $sPost = "username=".$usuario . "&password=". $password ;, do
$sPost=http_build_query(array('username'=>$usuario,'password'=>$password));, else accounts with & or = or NULLs in the password or username wont work properly
The answer posted by #hanshenrik should really be accepted. But if you just want an easy solution that works and is not incorrect, remove the 'Accept-Encoding: gzip, deflate' from your headers array.