I have a code which returns server response, but it's using fscokopen function which is not available on the server I'm using on this project.
How to rewrite this to CURL or similar alternative?
$server = 'whois.afilias.net';
$fp = #fsockopen($server, 43,$errno, $errstr, $this->m_connectiontimeout);
if( $fp ){
#fputs($fp, $domain."\r\n");
#socket_set_timeout($fp, $this->m_sockettimeout);
while( !#feof($fp) ){
$data .= #fread($fp, 4096);
}
#fclose($fp);
return $data;
}else{
return "\nError - could not open a connection to $server\n\n";
}
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
$result = curl_exec($ch);
curl_close($ch);
See for more options of curl.
Related
I have created two file php. The first one for send a file through curl utilizing the chunking method. And a second one for receiving the file.
What I need is to send a chunked file with curl and receive it. However, I am not really sure that the code in this two pages does what I need.
This is the code in the first page (send.php).
<?php
function curlPutThrottle($ch, $fh, $length = false)
{
if (!$length)
{
$length = 4096;
}
return fread($fh, $length);
}
$target_url = "http://localhost/save.php?filename=test2.zip";
$cFile = fopen ('./test.zip', "r");
$size = filesize('./test.zip');
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $target_url);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_PUT, true);
// Let curl know that we are sending an entity body
curl_setopt($ch, CURLOPT_UPLOAD, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_BINARYTRANSFER, 1);
// Let curl know that we are using a chunked transfer encoding
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Transfer-Encoding: chunked'));
curl_setopt($ch, CURLOPT_READFUNCTION, 'curlPutThrottle');
curl_setopt($ch, CURLOPT_INFILE, $cFile);
curl_setopt($ch, CURLOPT_INFILESIZE, $size);
$result=curl_exec ($ch);
curl_close ($ch);
echo $result;
?>
And this is the code in save.php.
<?php
function file_get_contents_chunked($filename, $chunk_size, $callback) {
$handle = fopen($filename, 'r');
if (feof($handle)) {
return 0;
} else {
while (!feof($handle)) {
call_user_func_array($callback, array(fread($handle, $chunk_size)));
}
fclose($handle);
return 1;
}
}
if ($_SERVER['REQUEST_METHOD'] == 'PUT') {
$count = 0;
$file = fopen('./'.$_GET['filename'], 'wb+');
$res = file_get_contents_chunked('php://input', 8192, function ($chunk) use (&$file) {
global $count;
fwrite($file, $chunk);
$count++;
});
if (!$res)
echo "file not received";
else
echo "file received ".$count;
} else {
echo 'Not a PUT request!!!';
}
?>
Is this code in these two pages correct for sending a chunked file through curl? Or is it incomplete in some way?
And, if is it incomplete what I need to send a chunked file correctly using curl?
Thanks at all for your help in this.
I use this piece of code below to send data to another server via a url and it work successfully. I want to capture the response from the server and process it but I can't seem to capture it.
CODE
$url="http://www.example.com/com_spc/api.php?username=".urlencode($uname)."&password=".urlencode($pwd);
$ch = curl_init(); // create cURL handle (ch)
if (!$ch) {
die("Couldn't initialize a cURL handle");
}
// set some cURL options
$ret = curl_setopt($ch, CURLOPT_URL, $url);
$ret = curl_setopt($ch, CURLOPT_HEADER, 0);
$ret = curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 0);
$ret = curl_setopt($ch, CURLOPT_RETURNTRANSFER, 0);
$ret = curl_setopt($ch, CURLOPT_TIMEOUT, 30);
// execute
$ret = curl_exec($ch);
if (empty($ret)) {
// some kind of an error happened
die(curl_error($ch));
curl_close($ch); // close cURL handler
} else {
$info = curl_getinfo($ch);
curl_close($ch); // close cURL handler
if (empty($info['http_code'])) {
die("No HTTP code was returned");
} else {
}
}
Make sure to set CURLOPT_RETURNTRANSFER to 1. Otherwise curl_exec will not return anything
Is it possible to send post/get request to tor hidden service using php? Tor installed on my VPS and it opens 127.0.0.1:9050 scoks5 to access for tor network. I am trying to use POST via proxy like in example at the end, but nothing happens. Please tell me what is wrong in that way.
<?php
$proxy = "127.0.0.1";
$port = "9050";
$url = "https://mydomain.onion/input.php";
$fp = fsockopen($proxy, $port, $errno, $errstr, 30);
$url = #parse_url($url);
if($fp)
{
//reading data
if(($data = #file_get_contents('php://input')) === false)$data = '';
$request = "POST {$url['path']}." HTTP/1.1\r\n";
$request .= "Host: {$url['host']}\r\n";
if(!empty($_SERVER['HTTP_USER_AGENT']))$request .= "User-Agent: {$_SERVER['HTTP_USER_AGENT']}\r\n";
$request .= "Content-Type: application/x-www-form-urlencoded\r\n";
$request .= "Content-Length: ".strlen($data)."\r\n";
$request .= "Connection: Close\r\n";
//trying to send request
fwrite($fp, $headers.$data);
//trying to get answer
while(!feof($fp)) echo fread($fp, 1024);
fclose($fp);
}
else die;
?>
I used tor this way:
$proxy = "127.0.0.1";
$port = "9050";
$url = "https://mydomain.onion/input.php";
$ch = curl_init();
curl_setopt ($ch, CURLOPT_URL, $url);
curl_setopt ($ch, CURLOPT_HEADER, 0);
curl_setopt ($ch, CURLOPT_PROXYTYPE, 7 );
curl_setopt ($ch, CURLOPT_PROXY, $proxy.':'.$port );
ob_start();
curl_exec ($ch);
curl_close ($ch);
$result = ob_get_contents();
ob_end_clean();
var_dump($result);
I have got an image url from facebook:
https://fbcdn-sphotos-e-a.akamaihd.net/hphotos-ak-prn1/s720x720/156510_443901075651849_1975839315_n.jpg
I need to save this in my local. when i used file_get_contents it gave error failed to open stream. when i open image in the browser it is showing fine. I just understand how to do it.
infact i used curl in the following way and got no response at all
$url = https://fbcdn-sphotos-e-a.akamaihd.net/hphotos-ak-prn1/s720x720/156510_443901085651849_1975839315_n.jpg;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, 0);
$response = curl_exec($ch);
curl_close($ch);
$filename = 'ex'.$src['photo_id'].'.jpg';
$imgRes = imagecreatefromstring($response);
imagejpeg($imgRes, $filename, 70);
header("Content-Type: image/jpg");
imagejpeg($imgRes, NULL, 70);
It's because you are requesting an secure URL and your server probably doesn't support it without configuration. You can either use CURL to request the URL with a valid certificate or just try and request it without SSL:
<?php
$file = 'http://url/to_image.jpg';
$data = file_get_contents($file);
header('Content-type: image/jpg');
echo $data;
You need to tell cURL that you don't wan't to verify the SSL connection.
The following is tested and works.
$url = "https://******";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); // ignore SSL verifying
curl_setopt($ch, CURLOPT_HEADER, 0);
$response = curl_exec($ch);
curl_close($ch);
header("Content-Type: image/jpg");
echo $response;
It's most likely that Facebook requires a valid User-Agent string and is denying your request because file_get_contents doesn't send one when accessing remote files.
You could use this:
if( $f = fsockopen($host="fbcdn-sphotos-e-a-.akamaihd.net",80)) {
fputs($f,"GET /hphotos-ak-prn1/........ HTTP/1.0\r\n"
."Host: ".$host."\r\n"
."User-Agent: My Image Downloader\r\n\r\n");
$ret = "";
$headers = true;
while(!feof($f)) {
$line = fgets($f);
if( $headers) {
if( trim($line) == "") $headers = false;
}
else $ret .= $line;
}
fclose($f);
file_put_contents("mylocalfile.png",$ret);
}
I want to create site map for my site. So before creating sitemap, i want to know the status code of each url. I have used curl option to deduct status code. I have more than 400 urls in my site. if i use curl, its taking long time.
Only i want to allow the url which is contain status code 200.
Could you please any one tell me any other option to deduct each url's status code.
I have used below curl code.
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $urlparam);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 240);
curl_exec($ch);
$curlcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
echo $curlcode;
reference link
I ran into this same issue a few months ago. I found that using this code example to access my own page status codes was much faster:
<?php
//
// Checking the status of a web page - funmin.com
//
$server="www.YOUR_WEBSITE.com";
function sockAccess($page)
{
$errno = "";
$errstr = "";
$fp = 0;
global $server;
$fp = fsockopen($server, 80, $errno, $errstr, 30);
if ($fp===0)
{
die("Error $errstr ($errno)");
}
$out = "GET /$page HTTP/1.1\r\n";
$out .= "Host: $server\r\n";
$out .= "Connection: Close\r\n\r\n";
fwrite($fp,$out);
$content = fgets($fp);
$code = trim(substr($content,9,4));
fclose($fp);
return intval($code);
}
?>
Further documentation may be found here: http://www.forums.hscripts.com/viewtopic.php?f=11&t=4217