I have a long list of URLs saved in excel and have a shell script to use with cURL to retrieve all URLs with errors, redirects, and/or connection timeout. I have never used cURL or shell. I need to use it on windows. So far I only know how to get to " C:\MyCurl>curl ". I have my shell script saved in a notepad. Can someone please tell me in specific detail what to do, including what the script and URLs should be saved as? It would be very helpful as I do not want to have to do each one manually and could be useful for many times in the future. Thank very much for your time.
I don't have enough details to answer your question about using a curl with a shell script on your local machine. But you may want to consider avoiding a shell altogether. PHP has a robust curl library. If you add the function below to your PHP script and call it with the URL as the parameter like:
$result = file_get_contents_curl( $url );
you will have code that's far more portable and scalable.
function file_get_contents_curl( $url ) {
$ch = curl_init();
curl_setopt( $ch, CURLOPT_AUTOREFERER, TRUE );
curl_setopt( $ch, CURLOPT_HEADER, 0 );
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, 1 );
curl_setopt( $ch, CURLOPT_URL, $url );
curl_setopt( $ch, CURLOPT_FOLLOWLOCATION, TRUE );
$data = curl_exec( $ch );
if ( curl_errno( $ch ) <> FALSE ) {
return FALSE;
}
curl_close( $ch );
return $data;
}
Related
I want that my bot when I write /orario he answer me with a inline keyboard.
So.. I created an array for my keyboard in this way:
$tastierino_giorno = '[{"text":"Testo","callback_data":"StampaMessaggio"}]';
and in another function I write this:
function tastieraInline($chatid, $tastierino)
{
global $token;
$messaggio = "Scegli per che giorno inviare il messaggio:";
$tastiera = '&reply_markup={"inline_keyboard":
['.urlencode($tastierino).'],"resize_keyboard":true}';
$url = "https://api.telegram.org/$token/sendMessage?chat_id=$chatId&parse_mode=HTML&text=".urlencode($messaggio).$tastiera;
file_get_contents($url);
}
After this, with an if, I check if the users has write "/orario".
} elseif($message == "/orario"){
tastieraInline($chatid, $tastierino_giorno);
}
Now the problem is that it doesn't works... what's the problem?
Change
$tastiera = '&reply_markup={"inline_keyboard":
['.urlencode($tastierino).'],"resize_keyboard":true}';
to
$tastiera = '&reply_markup='.urlencode('{"inline_keyboard":
['.$tastierino.'],"resize_keyboard":true}');
you should URL encode the whole JSON data
You should pay attention to the result returned from your telegram calls. file_get_contents is great for getting something working quickly but doesn't return any error information.
You should use curl or a library like guzzle. An example for curl:
$ch = curl_init( $url );
if ( $ch == FALSE )
{
error_log( "Error initialising curl" );
return FALSE;
}
curl_setopt( $ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1 );
curl_setopt( $ch, CURLOPT_POST, 0 );
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, 1 );
curl_setopt( $ch, CURLOPT_SSL_VERIFYPEER, 1 );
curl_setopt( $ch, CURLOPT_SSL_VERIFYHOST, 2 );
curl_setopt( $ch, CURLOPT_FORBID_REUSE, 1 );
// Set TCP timeout to 30 seconds
curl_setopt( $ch, CURLOPT_CONNECTTIMEOUT, 30 );
curl_setopt( $ch, CURLOPT_HTTPHEADER, array( 'Connection: Close' ) );
$result = curl_exec( $ch );
$error = curl_errno( $ch );
$errorStr = curl_error( $ch );
curl_close( $ch );
if ( $error != 0 )
{
return array( $errorStr, $result );
}
return $result;
Guzzle is a lot simpler if you don't mind installing an additional library.
Hopefully this will get you the error string from your failed telegram calls.
Onto your actual issue. You should create json for the keyboard markup using json_encode rather than appending strings. http_build_query makes building URLs much easier.
I have a php script that has a for loop that downloads some pdf/jpg files from AWS.
I'm currently testing it and it is downloading 180 files which total about 64MB.
When I call the script through the browser using http://localhost/script.php, the script takes about 10-12 minutes to completely finish.
If I call the same script from the command prompt by running c:\Program Files (x86)\PHP\v5.6\php-cgi.exe -f "c:\WebApps\scripts.php" the complete script runs in about 5 seconds and downloads all the files correctly.
Does anybody have an idea what could be causing this huge discrepancy?
I am using PHP 5.6 on IIS 7.5 on Windows 2008 R2.
I ended up solving the issue by changing the method I was using for downloading the files. I was initially using file_put_contents and I switched over to curl.
I am using the following function:
function retrieveandsave($url, $pathtosave, $filename){
if (!file_exists($pathtosave)) {
mkdir($pathtosave, 0777, true);
}
$fp = fopen ($pathtosave . $filename, 'w+');
$ch = curl_init();
curl_setopt( $ch, CURLOPT_URL, $url );
curl_setopt( $ch, CURLOPT_BINARYTRANSFER, true );
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, false );
curl_setopt( $ch, CURLOPT_SSL_VERIFYPEER, false );
curl_setopt( $ch, CURLOPT_CONNECTTIMEOUT, 10 );
curl_setopt( $ch, CURLOPT_FILE, $fp );
curl_exec( $ch );
curl_close( $ch );
fclose( $fp );
}
I have and xml url from a supplier which generates xml content dynamically with php like;
http://www.example.com/outputxml/index.php?xml_service_id=161
This url is valid for a static ip so I gave him my websites hosting ip. Is there a way to open that url in browser with data scraping? Because My internet connection has no static ip.
Thank you.
I have tried below code;
$url = 'http://www.example.com/outputxml/index.php?xml_service_id=161?';
$ch = curl_init();
curl_setopt( $ch, CURLOPT_URL, $url );
curl_setopt( $ch, CURLOPT_POST, true );
curl_setopt( $ch, CURLOPT_HTTPHEADER, array('Content-Type: text/xml'));
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );
curl_setopt( $ch, CURLOPT_POSTFIELDS, $xml );
$result = curl_exec($ch);
curl_close($ch);
echo $result;
But it gave html format.
Save the content on your server with something like a wget and then serve it. Please notice that you are probably going to infringe the policy of the xml's author (I don't know the consequences or the policy itself, but you should be careful), so you might consider to at least add a .htacces authentication on your server's page, just to not make the xml public.
I am trying to import lot's of images into my database which is in my .csv file, but before importing i am trying to check if url of an image is valid or not by using 'getimagesize()' function in php.
When i use getimagesize() function to check url of an image is valid or not at that time my importing is working too slowly.
So is there any other way to check image URL and import speedly as well please suggest me, Thanks in advance
The fastest way to test a remote url is using CURL with the NOBODY flag enabled, if your server supports it.
function remoteFileExists( $url )
{
$ch = curl_init();
curl_setopt( $ch, CURLOPT_URL,$url );
curl_setopt( $ch, CURLOPT_NOBODY, 1 );
curl_setopt( $ch, CURLOPT_FAILONERROR, 1 );
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, 1 );
$output = ( curl_exec( $ch ) !== false );
curl_close( $ch );
return $output;
}
I have a server, called install64-7 in this example which I will access to check for the existance of a zip file, which is not on server. The following PHP code returns the HTTP returncode 200 even if the zip file does not exist on the server install64-7.
$srcPath = "http://install64-7/TestApp.zip";
$ch = curl_init( $srcPath );
curl_setopt( $ch, CURLOPT_NOBODY, true );
curl_exec( $ch );
$retcode = curl_getinfo( $ch, CURLINFO_HTTP_CODE );
curl_close( $ch );
unset( $ch );
var_dump($retcode);
exit;
In case I remove the option CURLOPT_NOBODY, the request gives a 404! see screenshot for second request
$srcPath = "http://install64-7/TestApp.zip";
$ch = curl_init( $srcPath );
//curl_setopt( $ch, CURLOPT_NOBODY, true );
curl_exec( $ch );
$retcode = curl_getinfo( $ch, CURLINFO_HTTP_CODE );
curl_close( $ch );
unset( $ch );
var_dump($retcode);
exit;
How is this possible, what am I missing? What is this sorcery about the option CURLOPT_NOBODY?
Thank you for any help
CURLOPT_NOBODY set to TRUE makes a HTTP HEAD request, as compared to the "normal" HTTP GET.
If you get a different repsonse code because of that it is simply because the server decides to respond differently - although it shouldn't according to the HTTP spec.