PHP Curl POST Request to iFrame - php

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);

Related

GraphQL-call with cUrl returns "POST body missing. Did you forget use body-parser middleware?" error message

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?

PHP Curl connection to an IP without a domain

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!

why does curl not work when it is made into a function in php

I'm making code to run curl, but when I try to convert the code into a function on php, the code doesn't work.
I run the code in php xampp / cmd shell, PHP version 7, and I have also tried hosting, but the results are the same. I have tried adding data types like those found at https://www.w3schools.com/php/php_functions.asp, but it has no effect.
This is code without being included in the function:
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://i.instagram.com/api/v1/friendships/create/2878405206/');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, "signed_body=cdb3985eb8de0d7ab8bf9a2282615e27452b3f71c30cc95d1b1a325d9f2e36d2.%7B%22_csrftoken%22%3A%221W9jdoDp4GkJRNvWwnZ9Mm1G5Tfh2ub1%22%2C%22user_id%22%3A%222878405206%22%2C%22radio_type%22%3A%22wifi-none%22%2C%22_uid%22%3A%2221423938509%22%2C%22device_id%22%3A%22android-0aaa681cf6d47fdb%22%2C%22_uuid%22%3A%22825ebac9-3235-4cad-b187-7ed685a06b37%22%7D&ig_sig_key_version=4");
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_ENCODING, 'gzip, deflate');
$headers = array();
$headers[] = 'X-Ig-Connection-Type: WIFI';
$headers[] = 'User-Agent: Instagram 24.0.0.12.201 Android (4.4.4/19; 240dpi; 800x480; GT-I9060I; samsung; samsung; grandneove3g; in_ID)';
$headers[] = 'Accept-Language: id-ID, en-US';
$headers[] = 'Host: i.instagram.com';
$headers[] = 'X-Fb-Http-Engine: Liger';
$headers[] = 'Content-Type: application/x-www-form-urlencoded';
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$resultLast = curl_exec($ch);
if (curl_errno($ch)) {
echo 'Error:' . curl_error($ch);
}
curl_close($ch);
print_r($resultLast);
and this is the code that has been used as a function:
function sendData($path,$dataHook){
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $path);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $dataHook);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_ENCODING, 'gzip, deflate');
$headers = array();
$headers[] = 'X-Ig-Connection-Type: WIFI';
$headers[] = 'User-Agent: Instagram 24.0.0.12.201 Android (4.4.4/19; 240dpi; 800x480; GT-I9060I; samsung; samsung; grandneove3g; in_ID)';
$headers[] = 'Accept-Language: id-ID, en-US';
$headers[] = 'Host: i.instagram.com';
$headers[] = 'X-Fb-Http-Engine: Liger';
$headers[] = 'Content-Type: application/x-www-form-urlencoded';
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$resultLast = curl_exec($ch);
if (curl_errno($ch)) {
echo 'Error:' . curl_error($ch);
}
curl_close($ch);
return $resultLast;
}
print_r(sendData('https://i.instagram.com/api/v1/friendships/create/2878405206','signed_body=cdb3985eb8de0d7ab8bf9a2282615e27452b3f71c30cc95d1b1a325d9f2e36d2.%7B%22_csrftoken%22%3A%221W9jdoDp4GkJRNvWwnZ9Mm1G5Tfh2ub1%22%2C%22user_id%22%3A%222878405206%22%2C%22radio_type%22%3A%22wifi-none%22%2C%22_uid%22%3A%2221423938509%22%2C%22device_id%22%3A%22android-0aaa681cf6d47fdb%22%2C%22_uuid%22%3A%22825ebac9-3235-4cad-b187-7ed685a06b37%22%7D&ig_sig_key_version=4'));
it should produce {"message": "login_required", "error_title": "You have logged out", "error_body": "Please log in again.", "logout_reason": 2, "status": "fail"}, but the function does not issue anything.
You forgot to add "/" at the end of the url
function sendData($path,$dataHook){
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $path);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $dataHook);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_ENCODING, 'gzip, deflate');
$headers = array();
$headers[] = 'X-Ig-Connection-Type: WIFI';
$headers[] = 'User-Agent: Instagram 24.0.0.12.201 Android (4.4.4/19; 240dpi; 800x480; GT-I9060I; samsung; samsung; grandneove3g; in_ID)';
$headers[] = 'Accept-Language: id-ID, en-US';
$headers[] = 'Host: i.instagram.com';
$headers[] = 'X-Fb-Http-Engine: Liger';
$headers[] = 'Content-Type: application/x-www-form-urlencoded';
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$resultLast = curl_exec($ch);
if (curl_errno($ch)) {
echo 'Error:' . curl_error($ch);
}
curl_close($ch);
return $resultLast;
}
print_r(sendData('https://i.instagram.com/api/v1/friendships/create/2878405206/','signed_body=cdb3985eb8de0d7ab8bf9a2282615e27452b3f71c30cc95d1b1a325d9f2e36d2.%7B%22_csrftoken%22%3A%221W9jdoDp4GkJRNvWwnZ9Mm1G5Tfh2ub1%22%2C%22user_id%22%3A%222878405206%22%2C%22radio_type%22%3A%22wifi-none%22%2C%22_uid%22%3A%2221423938509%22%2C%22device_id%22%3A%22android-0aaa681cf6d47fdb%22%2C%22_uuid%22%3A%22825ebac9-3235-4cad-b187-7ed685a06b37%22%7D&ig_sig_key_version=4'));

How to login to an ASP.NET webpage using PHP cURL?

Our college system is using TCS (Total Campus Solution), Which is an online platform to track attendance and score of students by logging in with their username and passwords.
I want to create a PHP script that logging to their account if username+password is known and extract contents from HTML out.
This is because The vendor of the TCS is not providing any API's to access their database.
Here is the website: http://117.232.108.164/OnlineTCS/Login.aspx
Username: b14080
Password: b14080
(*I'm giving it here because the website hosts nothing sensitive and is read-only)
Here is my sample PHP cURL script:
<?php
$login_url = 'http://117.232.108.164/OnlineTCS/Login.aspx';
//These are the post data username and password
$post_data = 'ddlInstitution_ddl=KMEA Engineering College&ddlAcademicYear_ddl=2017-2018&txtUserName_txt=b14080&txtPassword_txt=b14080';
//Create a curl object
$ch = curl_init();
//Set the useragent
$agent = 'Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/537.31 (KHTML, like Gecko) Chrome/26.0.1410.64 Safari/537.31';
curl_setopt($ch, CURLOPT_USERAGENT, $agent);
//Set the URL
curl_setopt($ch, CURLOPT_URL, $login_url );
//This is a POST query
curl_setopt($ch, CURLOPT_POST, 1 );
//Set the post data
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
//We want the content after the query
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
//Follow Location redirects
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
/*
Set the cookie storing files
Cookie files are necessary since we are logging and session data needs to be saved
*/
curl_setopt($ch, CURLOPT_COOKIEJAR, 'cookie.txt');
curl_setopt($ch, CURLOPT_COOKIEFILE, 'cookie.txt');
//Execute the action to login
$postResult = curl_exec($ch);
echo $postResult;
?>
And $postResult is displaying only the HTML of the login page. Why and How to make this up
You have the keys to the post data incorrect.
You are missing some post fields
I broke the request into two separate request rather than use CURLOPT_FOLLOWLOCATION,true
I get the cookies from the first request (with 302 redirect)
Then pass the cookies to the redirect URL.
I return the HTML as plain text because the page is generated with JavaScript. For HTML change the header from text/plain to header('Content-Type: text/html; charset=utf-8');
This code is tested and works (debug code is commented out):
<?php
header('Content-Type: text/plain; charset=utf-8');
$post = array('txtUserName$txt'=>'b14080',
'txtPassword$txt'=>'b14080',
'btnLogin.x'=>"47",
'btnLogin.y'=>"16",
'ddlInstitution$ddl'=>"1",
'__VIEWSTATE'=>"/wEPDwUKMTM4NDc5NjkwOA9kFgQCAQ9kFggCAw8WAh4JaW5uZXJodG1sBTE8dWwgc3R5bGU9J2xpc3Qtc3R5bGUtdHlwZTogbm9uZTsnPjxsaT48L2xpPjwvdWw+ZAIHD2QWCgIBD2QWBAIBDxAPFgIeC18hRGF0YUJvdW5kZ2QQFQEYS01FQSBFbmdpbmVlcmluZyBDb2xsZWdlFQEBMRQrAwFnFgFmZAIDDw8WDh4RQ29udHJvbFRvVmFsaWRhdGUFA2RkbB4MRXJyb3JNZXNzYWdlBRdJbnN0aXR1dGlvbiBpcyBSZXF1aXJlZB4SRW5hYmxlQ2xpZW50U2NyaXB0Zx4PU2V0Rm9jdXNPbkVycm9yZx4MSW5pdGlhbFZhbHVlBQItMR4PVmFsaWRhdGlvbkdyb3VwBQRTYXZlHgdWaXNpYmxlZ2RkAgMPZBYEAgEPEA8WAh8BZ2QQFQgJMjAxNy0yMDE4CTIwMTYtMjAxNwkyMDE1LTIwMTYJMjAxNC0yMDE1CTIwMTMtMjAxNAkyMDEyLTIwMTMJMjAxMS0yMDEyCTIwMTAtMjAxMRUIAjExAjEwATkBOAE0ATMBNQE2FCsDCGdnZ2dnZ2dnFgFmZAIDDw8WDh8CBQNkZGwfAwUQWWVhciBpcyBSZXF1aXJlZB8EZx8FZx8GBQItMR8HBQRTYXZlHwhnZGQCBQ9kFgZmDw9kFgIeBWNsYXNzBQ1SZXF1aXJlZEZpZWxkZAICDw8WDh8CBQN0eHQfAwUVVXNlciBOYW1lIGlzIFJlcXVpcmVkHwRnHwVnHgRUZXh0ZR8HBQRTYXZlHwhnZGQCBA8PFgIfCmVkZAIHD2QWBmYPD2QWAh8JBQ1SZXF1aXJlZEZpZWxkZAICDw8WDh8CBQN0eHQfAwUUUGFzc3dvcmQgaXMgUmVxdWlyZWQfBGcfBWcfCmUfBwUEU2F2ZR8IZ2RkAgQPDxYCHwplZGQCCw8PFgIfCGgWAh4HT25DbGljawVEamF2YXNjcmlwdDpsb2NhdGlvbi5ocmVmPSdUZWFjaGVyRXZhbHVhdGlvbkxvZ2luLmFzcHgnO3JldHVybiBmYWxzZTtkAgkPFCsAAhQrAAMPFgIeF0VuYWJsZUFqYXhTa2luUmVuZGVyaW5naGRkZGRkAgsPFgIfCGhkAgIPFgIfCGhkGAEFHl9fQ29udHJvbHNSZXF1aXJlUG9zdEJhY2tLZXlfXxYCBQhidG5Mb2dpbgURUmFkV2luZG93TWFuYWdlcjGdhT6X6If1jy9RfnfHobAg2K4rSIeelkBCJSy3rq2wpw==",
'__EVENTVALIDATION'=>"/wEdABQgdITQYl0qSK+FUmoppd6yyBxeNODIYGb6hAb21KeJ7kXTFXzD8TPDckb0BpfOFEp8Q9GPFDMhnrO3EjMK7WDTfhXHcTNPmygrTq8U9nAZtWQdVtgFq+rE5JnIhzW6znHLgPIn5luRNqETic6Gh3lCzr/wuij3wzWkzglRmmW3l5eBKc1N3yXvWoV9t2A0ySUKrC+DRHeW4g3m+f2DrbA3AvnW9Ev6X0f6wNxHrCudiSSDXC6OOeSFHGK58wOzO+HMoHg3/YyHTBQ6cquFZ2oJblFY4hVirLH9o7SsXfoOK5utqpARm6HgYh9wA9bYTxNCuiJCR6m/KNe29Xd5W0RD9Pi9q20fDF3kt6+0eIryN4mI/M6bhYv0cQCm2IXbuc7UW0vN9k39fUY9+DzXmYv3op4oRunf14dz2Zt2+QKDELhHwE4yeHGuxfNtQ50+95KNfuiPvtdexWTG2c3kPRh1");
$request = array();
$request[] = 'Host: 117.232.108.164';
$request[] = 'Connection: keep-alive';
$request[] = 'Pragma: no-cache';
$request[] = 'Cache-Control: no-cache';
$request[] = 'Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8';
$request[] = 'User-Agent: Mozilla/5.0 (Windows NT 5.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2272.101 Safari/537.36';
$request[] = 'DNT: 1';
$request[] = 'Referer: http://117.232.108.164/OnlineTCS/Login.aspx';
$request[] = 'Accept-Encoding: gzip, deflate';
$request[] = 'Accept-Language: en-US,en;q=0.8';
$url = 'http://117.232.108.164/OnlineTCS/Login.aspx';
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, false);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
curl_setopt($ch, CURLOPT_HTTPHEADER, $request);
curl_setopt($ch, CURLOPT_ENCODING,"");
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);
curl_setopt($ch, CURLOPT_TIMEOUT,10);
curl_setopt($ch, CURLOPT_FAILONERROR,true);
curl_setopt($ch, CURLOPT_ENCODING,"");
curl_setopt($ch, CURLOPT_VERBOSE, true);
curl_setopt($ch, CURLINFO_HEADER_OUT, true);
curl_setopt($ch, CURLOPT_HEADER, true);
$data = curl_exec($ch);
if (curl_errno($ch)){
$data .= 'Retreive Base Page Error: ' . curl_error($ch);
}
else {
$info = rawurldecode(var_export(curl_getinfo($ch),true));
$skip = intval(curl_getinfo($ch, CURLINFO_HEADER_SIZE));
$responseHeader= substr($data,0,$skip);
// The folowing line is for debug
// echo "$info\n$responseHeader\n$data\n\n";
// Get the cookies:
$e = 0;
while(true){
$s = strpos($responseHeader,'Set-Cookie: ',$e);
if (!$s){break;}
$s += 12;
$e = strpos($responseHeader,';',$s);
$cookie = substr($responseHeader,$s,$e-$s) ;
$s = strpos($cookie,'=');
$key = substr($cookie,0,$s);
$value = substr($cookie,$s);
$cookies[$key] = $value;
}
// Create cookie for subsequent Requests:
$cookie = '';
$show = '';
$head = '';
$delim = '';
foreach ($cookies as $k => $v){
$cookie .= "$delim$k$v";
$delim = '; ';
}
}
$request = array();
$request[] = 'Host: 117.232.108.164';
$request[] = 'Connection: keep-alive';
$request[] = 'Pragma: no-cache';
$request[] = 'Cache-Control: no-cache';
$request[] = 'Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8';
$request[] = 'User-Agent: Mozilla/5.0 (Windows NT 5.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2272.101 Safari/537.36';
$request[] = 'DNT: 1';
$request[] = 'Referer: http://117.232.108.164/OnlineTCS/Login.aspx';
$request[] = 'Accept-Encoding: gzip, deflate';
$request[] = 'Accept-Language: en-US,en;q=0.8';
$url = 'http://117.232.108.164/OnlineTCS/StudentLogin/StudentLoginForm.aspx';
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, false);
curl_setopt($ch, CURLOPT_POST, false);
curl_setopt($ch, CURLOPT_HTTPHEADER, $request);
curl_setopt($ch, CURLOPT_ENCODING,"");
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);
curl_setopt($ch, CURLOPT_TIMEOUT,10);
curl_setopt($ch, CURLOPT_FAILONERROR,true);
curl_setopt($ch, CURLOPT_ENCODING,"");
curl_setopt($ch, CURLOPT_VERBOSE, true);
curl_setopt($ch, CURLINFO_HEADER_OUT, false);
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_COOKIESESSION , true );
curl_setopt($ch, CURLOPT_COOKIE, $cookie );
$data = curl_exec($ch);
$skip = intval(curl_getinfo($ch, CURLINFO_HEADER_SIZE));
echo substr($data,$skip); //do not transmit header
/* Debug Code
if (curl_errno($ch)){
$data .= 'Retreive Base Page Error: ' . curl_error($ch);
}
else {
$info = rawurldecode(var_export(curl_getinfo($ch),true));
// Get the cookies:
$skip = intval(curl_getinfo($ch, CURLINFO_HEADER_SIZE));
$responseHeader= substr($data,0,$skip);
echo "$info\n$responseHeader\n$data\n\n";
$e = 0;
while(true){
$s = strpos($responseHeader,'Set-Cookie: ',$e);
if (!$s){break;}
$s += 12;
$e = strpos($responseHeader,';',$s);
$cookie = substr($responseHeader,$s,$e-$s) ;
$s = strpos($cookie,'=');
$key = substr($cookie,0,$s);
$value = substr($cookie,$s);
$cookies[$key] = $value;
}
*/
}
?>

Sending auth in headers php curl

trying to do the equivalent of this in PHP - and failing :):
curl -H "X-abc-AUTH: 123456789" http://APIserviceProvider=http://www.cnn.com;
"123456789" is the API key. The command line statement works fine.
PHP code (does not work):
$urlToGet = "http://www.cnn.com";
$service_url = "http://APIserviceProvider=$urlToGet";
//header
$contentType = 'text/xml'; //probably not needed
$method = 'POST'; //probably not needed
$auth = 'X-abc-AUTH: 123456789'; //API Key
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $service_url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLINFO_HEADER_OUT, true);
//does not work
// curl_setopt($ch, CURLOPT_HTTPHEADER, Array('Content-type: ' .
// $contentType . '; auth=' . $auth));
//works! (THANKS #Fratyr for the clue):
curl_setopt($ch, CURLOPT_HTTPHEADER, Array($auth));
//this works too (THANKS #sergiocruz):
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Some_custom_header: 0',
'Another_custom_header: 143444,12'
));
//exec
$data = curl_exec($ch);
echo $data;
curl_close($ch);
Any ideas?
In order to get custom headers into your curl you should do something like the following:
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Some_custom_header: 0',
'Another_custom_header: 143444,12'
));
Therefore the following should work in your case (given X-abc-AUTH is the only header you need to send over):
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'X-abc-AUTH: 123456789' // you can replace this with your $auth variable
));
If you need additional custom headers, all you have to do is add on to the array within the curl_setopt.
I hope this helps :)
Use the following Syntax
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,"http://www.example.com/process.php");
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS,$vars); //Post Fields
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$headers = array();
$headers[] = 'X-abc-AUTH: 123456789';
$headers[] = 'Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8';
$headers[] = 'Accept-Encoding: gzip, deflate';
$headers[] = 'Accept-Language: en-US,en;q=0.5';
$headers[] = 'Cache-Control: no-cache';
$headers[] = 'Content-Type: application/x-www-form-urlencoded; charset=utf-8';
$headers[] = 'Host: 202.71.152.126';
$headers[] = 'Referer: http://www.example.com/index.php'; //Your referrer address
$headers[] = 'User-Agent: Mozilla/5.0 (X11; Ubuntu; Linux i686; rv:28.0) Gecko/20100101 Firefox/28.0';
$headers[] = 'X-MicrosoftAjax: Delta=true';
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$server_output = curl_exec ($ch);
curl_close ($ch);
print $server_output ;
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'auth=' . $auth
));
You set only one request header, not the two you wanted. You could do it for example like this:
// input
$urlToGet = "http://www.cnn.com";
// url
$service_url = sprintf("http://APIserviceProvider=%s", urlencode($urlToGet));
//header
$contentType = 'Content-type: text/xml'; //probably not needed
$auth = 'X-abc-AUTH: 123456789'; //API Key
$method = 'POST'; //probably not needed
// curl init
$ch = curl_init($service_url);
curl_setopt_array($ch, [
CURLOPT_RETURNTRANSFER => true,
CURLINFO_HEADER_OUT => true,
CURLOPT_HTTPHEADER => [
$contentType,
$auth,
],
]);
// curl exec
$data = curl_exec($ch);
curl_close($ch);
// output
echo $data;
(change the service url to the right one to get this to work)

Categories