How to write curl into php? - php

I have this below php script. I wants to add call function using curl.
How to i write curl on my below script?
I am new in programming. I am still learning it goodly. Please check my code and try to help me adding curl.
My script has not error. Just i wants to add php function replace to curl function.
I hope so, you understand. Sorry for my not good English. thanks
My code is here:
$url = 'https://web.facebook.com/'.$pageid.'/videos/'.$id.'/';
$context = [
'http' => [
'method' => 'GET',
'header' => "User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.47 Safari/537.36",
],
];
$context = stream_context_create($context);
$data = file_get_contents($url, false, $context);
function cleanStr($str)
{
return html_entity_decode(strip_tags($str), ENT_QUOTES, 'UTF-8');
}
function hd_finallink($curl_content)
{
$regex = '/hd_src_no_ratelimit:"([^"]+)"/';
if (preg_match($regex, $curl_content, $match)) {
return $match[1];
} else {return;}
}
function sd_finallink($curl_content)
{
$regex = '/sd_src_no_ratelimit:"([^"]+)"/';
if (preg_match($regex, $curl_content, $match1)) {
return $match1[1];
} else {return;}
}
$hdlink = hd_finallink($data);
$sdlink = sd_finallink($data);
if ($sdlink || $hdlink) {
echo 'HD Download ';
echo 'SD Download ';
} else {
echo 'Download not showing - Please Reload This Page';
}

Before we can do anything with a cURL request, we need to first instantiate an instance of cURL - we can do this by calling the function curl_init();
Sample code for GET request:
// Get cURL resource
$curl = curl_init();
// Set some options - we are passing in a useragent too here
curl_setopt_array($curl, array(
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_URL => 'http://testcURL.com/?item1=value&item2=value2',
CURLOPT_USERAGENT => 'Codular Sample cURL Request'
));
// Send the request & save response to $resp
$resp = curl_exec($curl);
// Close request to clear up some resources
curl_close($curl);
Sample code for POST request:
// Get cURL resource
$curl = curl_init();
// Set some options - we are passing in a useragent too here
curl_setopt_array($curl, array(
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_URL => 'http://testcURL.com',
CURLOPT_USERAGENT => 'Codular Sample cURL Request',
CURLOPT_POST => 1,
CURLOPT_POSTFIELDS => array(
item1 => 'value',
item2 => 'value2'
)
));
// Send the request & save response to $resp
$resp = curl_exec($curl);
// Close request to clear up some resources
curl_close($curl);
See this link

Related

PHP - How do i put the X-CSRF-TOKEN in the headers?

How do I put the $token or x-csrf-token to headers?
The curl post works when I put x-csrf-token in the headers manually.
What should I do to put the $token or x-csrf-token automaticly to headers?
When I make a new curl post that gets token and when I put the token automatic on the headers the response of the curl is 404 bad request
This is my script:
$Thing = $_GET['Thing'];
$username = $_GET['username'];
$password = $_GET['password'];
$login = array(
"cvalue" => $username,
"ctype" => "Username",
"password" => $password,
"captchaToken" => $Thing,
"captchaProvider" => "PROVIDER_ARKOSE_LABS"
);
$curl = curl_init('https://auth.roblox.com/v2/login'); // There are many links you can login from
curl_setopt_array($curl,array(
CURLOPT_RETURNTRANSFER => true,
CURLOPT_POST => true,
CURLOPT_SSL_VERIFYPEER => true,
CURLOPT_POSTFIELDS => json_encode($login),
CURLOPT_HEADER => true,
CURLOPT_CONNECTTIMEOUT => 5,
CURLOPT_TIMEOUT => 5,
CURLOPT_HTTPHEADER => array(
'Content-Type: application/json; charset=utf-8',
'Accept: application/json, text/plain, */*',
'X-CSRF-TOKEN: '
),
CURLOPT_USERAGENT,'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.130 Safari/537.36'
));
$response = curl_exec($curl);
$size = curl_getinfo($curl,CURLINFO_HEADER_SIZE);
curl_close($curl);
#$response1 = explode(" ", $response);
#$cookie = $response1[10];
#$search = ".ROBLOSECURITY=";
#$cookie1 = str_replace($search, '', $cookie);
$headers = substr($response,0,$size);
$array = explode("\n",$headers);
foreach($array as $data){
if(strpos($data,'csrf')){
$info = explode(' ',$data);
break;
}
}
$csrf = $info[1];
$token = $csrf;
print_r($token);
print_r($response);
There isn't a way to automatically attach custom headers to all CURL requests. The best way is to wrap your CURL code inside a function and pass the desired CSRF token as a parameter. Here's a demo:
function curl_call(array $params, string $csrf_token)
{
... curl code here...
'X-CSRF-TOKEN: '. $csrf_token,
... remaining curl code here...
}
Then when you want to make a CURL request, you may call the function like this:
curl_call(['url' => '...', etc], 'csrf token string here');

Not receiving transfer on different host

I'm trying to fetch html from a page with curl and php. I have the following code, which works perfectly on my host:
<?php
$curl = curl_init();
$URLs[] = "http://www.stackoverflow.com";
echo request( $curl, $URLs[0] );
function request($curl, $url, $post = null)
{
echo (isset($post) ? 'POST ' . $url : 'GET ' . $url) . PHP_EOL;
try
{
curl_setopt_array($curl, array
(
CURLOPT_RETURNTRANSFER => true,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_HEADER => true,
CURLOPT_AUTOREFERER => true,
CURLOPT_SSL_VERIFYPEER => false,
CURLOPT_URL => $url,
CURLOPT_USERAGENT => 'Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:30.0) Gecko/20100101 Firefox/30.0',
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,image/webp,*/*;q=0.8'
)
));
return curl_exec($curl);
}
catch(Exception $exception)
{
echo $exception;
}
}
curl_close($curl);
?>
When I put it on my webhost, I get no response. I checked for curl error and it says 'No Url set!'. Any hints please? Thanks!
try this other format of curl handling, initializing host in curl_init function:
$url = ....
$arrayConfig = ....
$curl = curl_init($url);
curl_setopt_array($curl,$arrayConfig);
$content = curl_exec($curl);
$err = curl_errno($curl);
$errmsg = curl_error($curl) ;
$header = curl_getinfo($curl);
curl_close($curl);

Server not responding using Curl POST Request

I am using this code to get the contents of a post request url using php curl
Code looks as below:
// Get cURL resource
$curl = curl_init();
// Set some options - we are passing in a useragent too here
curl_setopt_array($curl, array(
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_URL => 'http://www1.ptt.gov.tr/tr/interaktif/sonuc-yd.php',
CURLOPT_USERAGENT => 'Codular Sample cURL Request',
CURLOPT_POST => 1,
CURLOPT_POSTFIELDS => array(
'barcode' => 'CP021325078TR',
'security_code' => $capcha2
)
));
// Send the request & save response to $resp
$resp = curl_exec($curl);
// Close request to clear up some resources
curl_close($curl);
echo "<pre>";
var_dump($resp);
echo "</pre>";
The result doesn’t seem to return anything at all.
What is wrong with this code?
Try this:
$url = 'http://www1.ptt.gov.tr/tr/interaktif/sonuc-yd.php';
$postvals = array(
'barcode' => 'CP021325078TR',
'security_code' => $capcha2
);
$resp = Request($url,$postvals);
echo "<pre>"; var_dump($resp); exit;
...
function Request($url,$params=array()){
$ch = curl_init();
$curlOpts = array(
CURLOPT_URL => $url,
CURLOPT_USERAGENT => 'Mozilla/5.0 (Windows NT 6.3; WOW64; rv:29.0) Gecko/20100101 Firefox/29.0',
CURLOPT_SSL_VERIFYPEER => false,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_FOLLOWLOCATION => true
);
if(!empty($params)){
$curlOpts[CURLOPT_POST] = true;
$curlOpts[CURLOPT_POSTFIELDS] = $params;
}
curl_setopt_array($ch,$curlOpts);
$answer = curl_exec($ch);
if (curl_error($ch)) {
echo curl_error($ch); exit;
}
curl_close($ch);
return $answer;
}
EDIT:
I tested this and got:
Could not resolve host: www1.ptt.gov.tr
So make sure you're calling the right endpoint.
Actually you need to set this variable
$captcha2
To use it here -
'security_code' => $capcha2

Error to call Tinypass response in Restapi

I am new in Tinypass api integration.
I try to integrate Tinypass API using PHP. Code below:
$curl = curl_init();
// Set some options - we are passing in a useragent too here
curl_setopt_array($curl, array(
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_URL => 'http://sandbox.tinypass.com/r2/access?rid=portfolio_id&user_ref=badashah26',
// CURLOPT_USERAGENT => 'Codular Sample cURL Request',
CURLOPT_HTTPHEADER => array(
'AID: xxxxxxxx', // PUT your AID
'signature: xxxxxxxxxxxxxxxxxx', // PUT your signature
'sandbox: true'
)
));
// Send the request & save response to $resp
$resp = curl_exec($curl);
// Close request to clear up some resources
curl_close($curl);
print_r($resp); exit();
Response get but error display.
"error":{"message":"Access denied: invalid AID or signature","code":401}}
Any one can find solutions.
Thanks
Our REST API documentation has been updated to provide a few steps on how to generate your own API header. Check out the documentation at http://developer.tinypass.com/main/restapi.
Best,
Tinypass Support
Something like this..? (untested)
$aid = 'YOUR AID';
$action = '/r2/access?rid='.$rid.'&user_ref='.$userref;
$request = 'GET '.$action;
$url = 'http://sandbox.tinypass.com'.$action;
$signature = hash_hmac('sha256',$request,$aid);
$auth = $aid.':'.$signature;
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_URL => $url,
CURLOPT_HTTPHEADER => array('Authorization: '.$auth);
));
$resp = curl_exec($curl);
curl_close($curl);
print_r($resp); exit();

cURL with PHP and JSON

I wish to mimic, using CURL with PHP, the operation of a website that retrieves data using an AJAX POST.
Normally when I'm viewing POST requests using Firebug you will see variable/value pairs, but in this case all you see is a single JSON string. E.g.
{"refId":"14536"}
Is there a way to mimic this request using CURL? I've looked at CURL but as far as I can see the CURLOPT_POSTFIELDS parameter has to be a query string made up of one or more name/value.
Here is my test code with a normal POST request using a single name/value pair. I'd like to modify it to do the above.
$curlOptions = array(
CURLOPT_RETURNTRANSFER => true, // return web page
CURLOPT_HEADER => false, // don't return headers
CURLOPT_ENCODING => "", // handle all encodings
CURLOPT_USERAGENT => "Mozilla/5.0 (Windows; U; Windows NT 6.0; en-GB; rv:1.9.0.3) Gecko/2008092417 Firefox/3.0.3",
CURLOPT_CONNECTTIMEOUT => 600, // timeout on connect
CURLOPT_TIMEOUT => 600, // timeout on response
CURLOPT_POST => true,
CURLOPT_POSTFIELDS => 'var1=113',
CURLOPT_URL => "http://localhost/t4.php"
);
$curlCh = curl_init();
curl_setopt_array( $curlCh, $curlOptions );
$fileContents = curl_exec( $curlCh );
$curlErr = curl_errno( $curlCh );
$curlErrmsg = curl_error( $curlCh );
if( $curlErr ) echo "CURL ERROR:</b> $curlErr $curlErrmsg";
echo $fileContents; //check worked
curl_close( $curlCh );
How about something like:
$postData = json_encode(array('refId' => '14536'));
curl_setopt($ch, CURLOPT_POSTFIELDS, $postData);

Categories