Why is it not working code, why - I do not understand. Code gets a response from curl and looking (must look) in this response word yes, if it is found - that displays the text - if not, then the other. The code:
<?PHP
// CURL
$ch = curl_init('http://dev.local/phpwhois-4.2.2/example.php?query=domain.ru&output=object');
curl_setopt ($ch, CURLOPT_USERAGENT, "Mozilla/5.0");
curl_setopt ($ch, CURLOPT_HEADER, false);
$curl = curl_exec($ch);
echo $curl;
curl_close($ch);
if(preg_match('~\s*yes\s*~u', $curl))
echo 'Ok';
else
echo 'Else text';
?>
Error strange, more precisely, its not quite there, but - if curl sends text yes, that does not work, then writes that else, and if it does not give a text - too else. If all the text that simply gives curl himself put in the variable it works.
That's what gives the script to curl `e (this answer in writing what else):
regrinfo->Array disclaimer->Array 0->By submitting a query to RIPN's
Whois Service 1->you agree to abide by the following terms of use:
2->#3.2 (in Russian)
3-#3.2 (in English).
domain->Array name->hashcode.ru nserver->Array
ns1.nameself.com->81.176.95.18 ns2.nameself.com->88.212.207.45
status->REGISTERED, DELEGATED, VERIFIED created->2010-11-05
expires->2014-11-05 source->TCI registered->yes regyinfo->Array
referrer-> registrar->RUCENTER-REG-RIPN
servers->Array 0->Array server->ru.whois-servers.net
args->hashcode.ru port->43 type->domain rawdata->Array 0->% By
submitting a query to RIPN's Whois Service 1->% you agree to abide by
the following terms of use: 2->%
(in Russian) 3->%
(in English). 4->
5->domain: 6->nserver: . 7->nserver:
. 8->state: REGISTERED, DELEGATED, VERIFIED
9->person: Private Person 10->registrar: REGTIME-REG-RIPN
11->admin-contact: 12->created: 2010.11.05
13->paid-till: 2014.11.05 14->free-date: 2014.12.06 15->source: TCI
16-> 17->Last updated on 2014.07.27 12:31:31 MSK 18->
You have forgotten to set flag return transfer
<?PHP
// CURL
$ch = curl_init('http://dev.local/phpwhois-4.2.2/example.php?query=domain.ru&output=object');
curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0");
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$curl = curl_exec($ch);
echo $curl;
curl_close($ch);
if(preg_match('~\s*yes\s*~u', $curl))
echo 'Ok';
else
echo 'Else text';
?>
Take care also about timeouts in the future. Good luck.
Related
I have a little PHP script for scraping google. I wnat to google the exact search "batman kill a human" I pass the param with simple quoation marks "batman kill a human"
and I can see a correct URL generated.
I call the script with the param '"batman kill a human"', and I get >Error 400 (Bad Request)
example
root#ubuntu:/var/www/html# php ejemplo.php '"batman kill a human"'
------------- [https://www.google.es/search?q="batman kill a human"]
------------- [
Error 400 (Bad Request)!!1 *{margin:0;padding:0}html,code{font:15px/22px arial,sans-serif}html{background:#fff;color:#222;padding
....
...
-------------------------------------------
if i try to use this url in firefox, https://www.google.es/search?q="batman kill a human" y get a correct answer from google.
Why i don't get a correct answer when I'm trying it from the php scritp
this is the source code.
<?php
include('simple_html_dom.php');
function file_get_contents_curl($url) {
/*
This is a file_get_contents replacement function using cURL
One slight difference is that it uses your browser's idenity
as it's own when contacting google.
*/
$ch = curl_init();
// curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION , 1);
curl_setopt($ch, CURLOPT_URL, $url);
$data = curl_exec($ch);
curl_close($ch);
return $data;
}
$busca=$argv[1];
$cadena="https://www.google.es/search?q=$busca";
print "\n------------- [$cadena]";
$data=file_get_contents_curl($cadena);
$html = str_get_html($data);
print "\n------------- [$html]";
$html->clear(); exit();
?>
Check out the PHP manual on urlencode()
$busca=$argv[1];
becomes
$busca=urlencode($argv[1]);
Which renders as:
https://www.google.es/search?q=batman+kills+a+human
GREAT comment thread at: https://stackoverflow.com/a/6398220/1679026 gave me hope but I failed to solve my issue. When I use cURL to fetch the www.php.net page, it works so cURL works
I have a network-appliance that lives on a local LAN (http://192.168.1.117) here, or at my client site the devices (n) are on their LAN (173.18.13.xxx). From a Local_Browser_Address I can type "http://192.168.1.117/#diag/term&cmd=signage+sign+ALARM" and the network-appliance responds with a change to the "ALARM" state. <-- That is what I want to automate with one button!
On my web form I have $_POST PHP code to execute when the Alarm_Button is clicked. I want the user on my clients physical site to be able to click the_button on my web_form (www.mySite.com/index.php?option=com_rsform&formId=27) in -their browser- and have cURL code send/Post/execute that (http-ALARM) address/instruction string. Can I do this thing? No UI Required at or after address/instruction is sent.
I tried:
$c = curl_init('http://192.168.1.117/u/r/l');
curl_setopt($c, CURLOPT_HTTPHEADER, array('#diag/term&cmd=signage+sign+ALARM'));
curl_exec($c);
AND
$curl = curl_init();
echo"<br> the curl resource is ch --> $curl";
curl_setopt ($curl, CURLOPT_URL, "http://192.168.1.117");
curl_setopt ($curl, CURLOPT_POST, "/#diag/term&cmd=signage+sign+CELL_IN_ALARM");
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
$result = curl_exec ($curl);
curl_close ($curl);
echo"<br> The RESULT (1) is --> $result";
No error message but no useful result.
First: I am afraid I am being stopped by the security restrictions like "no_outside_web_page_doing_junk_locally" kind of rules.
IF I have to create a local page on an internal web server, I can do this on another device on the same/adjacent network. But the problem remains for me, how to express the ip/command instruction in the cURL tools.
I have scoured the StackOverFlow information and it seems like I am close but missing the "glaring truth" somewhere!
Hope my challenge is worthy of your interest.
Thank you.
---UPDATE---
the "Sent" values (461) above and below the one (484) highlighted have respective POST Data of:
{"version":"1.1","method":"unit.baseInfo","id":28,"params":[]}
{"version":"1.1","method":"term.cmd","id":29,"params":["signage sign CELL_IN_ALARM"]}
{"version":"1.1","method":"unit.baseInfo","id":30,"params":[]}
This shows Types application/jason and URLs of -http://192.168.1.117/jasonrpc-(jasonRemoteProcedureCall?)
Does this simplify or complicate my struggle?
-- New Update --
When I run the following code I get the php.net home page and a blank return "...is -->" for the second return...
// From http://www.tuxradar.com/practicalphp/15/10/2
$curl1 = curl_init();
curl_setopt ($curl1, CURLOPT_URL, "http://www.php.net");
curl_setopt($curl1, CURLOPT_RETURNTRANSFER, 1);
$result = curl_exec ($curl1);
curl_close ($curl1);
print $result;
//====================
$curl = curl_init();
curl_setopt ($curl, CURLOPT_URL, "http://192.168.1.117");
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
$result = curl_exec ($curl);
curl_close ($curl);
//print $result;
echo"<br> The RESULT (1) is --> $result";
The first one is a www and the second one is the device on my network. Is this a primitive Network/Web conflict? Really appreciate the brain cycles here!
You are thinking of CURLOPT_POSTFIELDS.
This can be used like
$ch = curl_init($site);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, "field1=value1&field2=value2&field3=value3");
$chx = curl_exec($ch);
It should also be noted that CURLOPT_POST only takes TRUE or FALSE per the documentation found here
Please excuse my terminology if I get anything wrong, I'm new to Google API
I'm trying to add events via the Places API, for a single venue (listed under the bar category). I've followed the instructions here:
https://developers.google.com/places/documentation/actions?utm_source=welovemapsdevelopers&utm_campaign=places-events-screencast#event_intro
and this is the URL I am posting to (via PHP)
https://maps.googleapis.com/maps/api/place/event/add/format?sensor=false&key=AIzaSyAFd-ivmfNRDanJ40pWsgP1 (key altered)
which returns a 404 error. If I have understood correctly, I have set the sensor to false as I am not mobile, and created an API key in Google apis, with the PLACES service turned on.
Have I missed a vital step here, or would a subsequent error in the POST submission cause a 404 error? I can paste my code in but I thought I'd start with the basics.
Many thanks for your help, advice and time. It's very much apprecciated.
I've added this line to my CurlCall function which I believe should specify a POST, and the result is still the same.
curl_setopt($ch, CURLOPT_POST, 1);
so the whole functions reads
function CurlCall($url,$topost)
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_HTTPHEADERS, array('Content-Type: application/json'));
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
curl_setopt($ch, CURLOPT_VERBOSE, TRUE);
curl_setopt($ch, CURLOPT_POSTFIELDS, $topost);
$body = curl_exec($ch);
curl_close($ch);
echo $body;
echo '<br>';
echo 'URL ' . $url;
echo '<br>';
return $body;
}
Are you sure that you are using the POST method and not GET?
You need to specify the format of your request in the URL by changing the word format to either json or xml depending on how you are going to structure and POST your request.
XML:
https://maps.googleapis.com/maps/api/place/event/add/xml?sensor=false&key=your_api_key
JSON:
https://maps.googleapis.com/maps/api/place/event/add/json?sensor=false&key=your_api_key
I have been struggling for almost three days now on this task, and I guess I am missing on some basic cURL skills.
I start with:
In the F12 of IE I see 2 POSTs on the first page: (I notice the first one is getting a 302 which is supposed to be a redirect, and with cURL I only get 200)
Filling up the captcha:
on the second page (after captcha):
traffic:
This is my code (and I cannot move on with it because it doesn't work for the early stages):
I Built a special form that submits to my own page with GET (with the cURL) which in turn is accessing the website:
$id=$_GET['id']; // getting the biznumber
$humanCode=$_GET['nobot'];
$curl = curl_init();
curl_setopt ($curl, CURLOPT_URL, "https://www.*******.******.***");
// setting some https to be able to access the website from my local computer.
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, true);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 2);
curl_setopt($curl, CURLOPT_CAINFO, "c:/xampp/htdocs/CAcerts/curl-ca-bundle.crt");
// I know the values for the ASPX vars like __EVENTTARGET, __EVENTARGUMENT, __VIEWSTATE are arbitrary now. I need to take care of that but I don't yet know how.
$postarr= array (
"__EVENTTARGET"=>"",
"__VIEWSTATE=" =>"%2FwEPDwULLTEzMzI2OTg4NDYPZBYCZg9kFgQCBA8PZBYCHgdvbmNsaWNrBQxnb1RvTWl2emFrKClkAgYPD2QWAh8ABQxnb1RvTWl2emFrKClkZM6iZZ0Qaf2CpfXoJJdZ0IqaWsDO",
"__EVENTARGUMENT=" =>"",
"__EVENTVALIDATION" =>"%2FwEWBQKgysLGCwL2r7SGDQLh4ri%2BAwLWws7NDwLWwpLPD%2F1HuCAFYzs2seaziWbYEXjDfigP",
"hidUrlFileIshurim"=>"https%3A%2F,
"cod"=>"3322"
);
$fields_string='';
foreach($postarr as $key=>$value) { $fields_string .= $key.'='.$value.'&'; }
rtrim($fields_string,'&');
curl_setopt($curl, CURLOPT_POST ,1);
curl_setopt($curl, CURLOPT_POSTFIELDS, $fields_string);
curl_setopt($curl, CURLOPT_TIMEOUT, 10);
curl_setopt ($curl, CURLOPT_USERAGENT, "User-Agent Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; WOW64; Trident/5.0; MAAU)");
// I made a cookie file and it seems to work
$cookiefile = "d:/cookie.txt";
curl_setopt($curl, CURLOPT_COOKIEJAR, $cookiefile);
curl_setopt($curl, CURLOPT_COOKIEFILE, $cookiefile);
curl_setopt($curl, CURLOPT_FRESH_CONNECT , 1);
curl_setopt($curl, CURLOPT_FOLLOWLOCATION ,1);
curl_setopt($curl, CURLOPT_HEADER ,1); // DO NOT RETURN HTTP HEADERS
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
$temp=curl_exec($curl);
$info = curl_getinfo($curl);
$html = mb_convert_encoding($temp, 'HTML-ENTITIES', 'utf-8');
echo "ERRCODE: ".curl_error($curl);
echo '<br /><br />';
echo "INFO : ";
print_r($info);
echo '<br /><br />';
$httpcode = curl_getinfo($curl, CURLINFO_HTTP_CODE);
echo "CODE: ".$httpcode;
echo '<br /><br />';
echo "CODE: ".$httpcode;
echo '<br /><br />';
echo "VARS: ".$vars;
echo '<br /><br />';
//echo $html;
curl_setopt ($curl, CURLOPT_URL, "https://www.*******.******.***");
curl_setopt($curl, CURLOPT_FRESH_CONNECT , 0);
echo "<br /><br /><b>2nd</b><br /><br />";
$temp=curl_exec($curl);
$info = curl_getinfo($curl);
$html = mb_convert_encoding($temp, 'HTML-ENTITIES', 'utf-8');
echo "ERRCODE: ".curl_error($curl);
echo '<br /><br />';
echo "INFO : ";
print_r($info);
echo '<br /><br />';
echo $html;
Can't get that to even start to work. It starts with returning me a 200 OK, instead of 302, and sometimes I also get a 500.
I know the ASPX vars might actually be crucial, but if my browser can make these vars and send them to the server, can't cURL do the same ?
Thanks for any help !!
Problem solved.
It was a matter of using the correct headers.
Following the reports from the browser, I went through all steps and the result showed up.
I went through each step by using:
curl_init
curl_setopt()
..
curl_setopt()
curl_exec()
curl_close()
This way I had to manually set each request and go through the settings. It made the code longer, but much easier to understand.
I had thoughts about the site using some javascript special code to make the site work, so I was troubled a lot by all the extra, javascript code, which turned out unnecessary.
It was all about being alot more organized and following the correct header settings.
Moreover, since this was an ASPX site, I had to read and memorize the VIEWSTATE and VALIDATION of the last page in each iteration. That is the first and very reason for the 500 internal error server message I used to get all the time.
I used Firebug and LiveHttpHeaders to concolude each step.
"Can't get that to even start to work. It starts with returning me a 200 OK, instead of 302, and sometimes I also get a 500."
curl_setopt($curl, CURLOPT_FOLLOWLOCATION ,1);
You have Curl set to follow any 302 redirects. These will be followed internally inside of Curl and won't be seen by PHP.
Also:
curl_setopt($curl, CURLOPT_HEADER ,1); // DO NOT RETURN HTTP HEADERS
The comment does exactly the opposite of what the code does....which seems wrong.
before u made cURL, u need to review the requeste field used. usually HTTP 500 from aspx is not found the field send..
foreach($postarr as $key=>$value) {
$fields_string .= $key.'='.$value.'&';
echo" $fields_string <br> ";
}
make sure, that field are not dinamic when u r sending request..
Hope this helpfull..
I used this:
curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER["HTTP_USER_AGENT"]);
It kind of simulates the curl like it has a browser name and version.
my service provider has given me following piece of PHP code for accessing his service. I need help in converting to C lang code for use in my application. The code is using curl module to post on to a site.
pls advise.
<?php
$ch = curl_init();
curl_setopt($ch,CURLOPT_URL, "http://api.mVaayoo.com/mvaayooapi/MessageCompose?user="myusername":"mypassword"&senderID=TEST SMS&receipientno="phonenum"&msgtxt=This is a test from mVaayoo API&state=4");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, "user=$user&senderID=$senderID&receipientno=$receipientno&cid=$cid&msgtxt=$msgtxt");
$buffer = curl_exec($ch);
if(empty ($buffer))
{ echo " buffer is empty "; }
else
{ echo $buffer; }
curl_close($ch);
?>
Use libcurl with it's C-interface. The remainer is good old C-style-string-handling.
Your example libcurl program in the comment looks good, except that for a POST you need to install a CURLOPT_READFUNCTION, not a CURLOPT_WRITEFUNCTION. But if you just want to post a static buffer, use CURLOPT_POSTFIELDS instead of a callback function.