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
Related
I am currently working with an API for a little project of mine that will only accept POST requests, however no matter what I seem to do I cannot get a response out of it, I have tried echoing the response, I have tried returning it, I have tried using print_r and nothing works. Can someone tell me what I am doing wrong?
<?php
$applicationPass = $_GET['appPass'];
$email = $_GET['email'];
$password = $_GET['password'];
if ($applicationPass == "abc123"){
//
$curl = curl_init();
// Set some options - we are passing in a useragent too here
curl_setopt_array($curl, array(
CURLOPT_RETURNTRANSFER => true,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_AUTOREFERER => true,
CURLOPT_URL => 'http://api.minergate.com/1.0/auth/login/',
CURLOPT_USERAGENT => 'Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:10.0) Gecko/20100101 Firefox/10.0',
CURLOPT_POST => 1,
CURLOPT_POSTFIELDS => array(
'email' => urlencode($_GET['email']),
'password' => urlencode($_GET['password'])
)
));
// Send the request & save response to $resp
$resp = curl_exec($curl);
// Close request to clear up some resources
curl_close($curl);
echo $resp;
//
}else{
die();
}
?>
Here is my updated code, I get a little bit of a different response with their new API that took forever to find.
<?php
$applicationPass = $_GET['appPass'];
$email = $_GET['email'];
$password = $_GET['password'];
if ($applicationPass == "abc123"){
//
$curl = curl_init();
// Set some options - we are passing in a useragent too here
curl_setopt_array($curl, array(
CURLOPT_RETURNTRANSFER => true,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_AUTOREFERER => true,
CURLOPT_URL => 'https://minergate.com/api/2.2/auth/login',
CURLOPT_USERAGENT => 'Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:10.0) Gecko/20100101 Firefox/10.0',
CURLOPT_CUSTOMREQUEST => 'POST',
curl_setopt($curl, CURLOPT_HTTPHEADER, array(
'email: ' . $email,
'password: ' . $password
)),
CURLOPT_POSTFIELDS => array(
'email' => urlencode($email),
'password' => urlencode($password)
)
));
// Send the request & save response to $resp
$resp = curl_exec($curl);
// Close request to clear up some resources
curl_close($curl);
echo $resp;
//
}else{
die();
}
?>
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
I have this code:
<?php
$data = array('name' => 'Ross', 'php_master' => true);
$url = 'http://dsaasd.adsds.nl:8081/cops.nsf/orders?openagent';
$html_brand = "www.google.com";
$ch = curl_init();
$options = array(
CURLOPT_URL => $url,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_HEADER => true,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_ENCODING => "",
CURLOPT_AUTOREFERER => true,
CURLOPT_CONNECTTIMEOUT => 120,
CURLOPT_TIMEOUT => 120,
CURLOPT_MAXREDIRS => 10,
CURLOPT_VERBOSE => true,
);
curl_setopt_array( $ch, $options );
$response = curl_exec($ch);
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
if ( $httpCode != 200 ){
echo "Return code is {$httpCode} \n"
.curl_error($ch);
} else {
echo "<pre>".htmlspecialchars($response)."</pre>";
}
curl_close($ch);
?>
The problem is, if i send curl post to:
$url = 'http://dsaasd.adsds.nl:8081/cops.nsf/orders?openagent';
Then it does NOT work.
But if i send curl post to the same service but on a different server then it works, this is the other server
$url = 'http://dsaasd.adsds.nl:80/cops.nsf/orders?openagent';
I also post data by normal form post to:
$url = 'http://dsaasd.adsds.nl:8081/cops.nsf/orders?openagent';
And then it works and i receive data on the server.
But with this curl post i keep getting:
Return code is 0 Failed to connect to 11.43.45.123: Network is unreachable
Anyone have any idea?
I think you need to add CURLOPT_POST to the $options array. Docs here.
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);
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);