<?php
// Set username and password
$username = 'XXX';
$password = 'XXX';
// The message you want to send
$message = 'is twittering from php using curl';
// The twitter API address
$url = 'https://twitter.com/statuses/update.xml';
// Alternative JSON version
// $url = 'https://twitter.com/statuses/update.json';
// Set up and execute the curl process
$curl_handle = curl_init();
curl_setopt($curl_handle, CURLOPT_URL, "$url");
curl_setopt($curl_handle, CURLOPT_CONNECTTIMEOUT, 2);
curl_setopt($curl_handle, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl_handle, CURLOPT_POST, 1);
curl_setopt($curl_handle, CURLOPT_POSTFIELDS, "status=$message");
curl_setopt($curl_handle, CURLOPT_USERPWD, "$username:$password");
$buffer = curl_exec($curl_handle);
curl_close($curl_handle);
// check for success or failure
if (empty($buffer)) {
echo 'failure';
} else {
echo 'success';
}
echo $buffer;
?>
Hallo, I have an Problem with the Code above. I have it from an Website. It returns at $buffer, that the Server understood the request but is an forbidden one.
The Twitter API does not support Tweeting with a username and password, you need to use OAuth. Also, the Twitter API URL that you are using in this code is very old (like, about 14 years old). You need to use the official Twitter API. There are some current PHP libraries available.
It's probably disabled within your server's configuration file. You should look into your specific server settings and fiddle around, curl_exec might be disabled by default.
You can check if the setting is enabled using the following command;
<?php phpinfo(); ?>
If you need help with configuring the php.ini file, please check this topic; enable curl_exec on php.ini. You would have to look for a line with disabled_functions, curl_exec might be added there.
Related
I'm using Geoplugin to retrieve users' current location. Everything works fine except the data is wrong. My Country is Oman but the output is USA.
Note that, I'm not using VPN or Proxy.
Code:
function file_get_contents_curl($url) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); //Set curl to return the data instead of printing it to the browser.
curl_setopt($ch, CURLOPT_URL, $url);
$data = curl_exec($ch);
curl_close($ch);
return $data;
}
$url = unserialize(file_get_contents_curl('http://www.geoplugin.net/php.gp?'.$_SERVER['REMOTE_ADDR']));
$orig_lat = $url["geoplugin_latitude"];
$orig_lon = $url["geoplugin_longitude"];
echo $orig_lat;
echo $orig_lon;
I'm using also cUrl instead of file_get_contents because allow_url_fopen in off in the server.
Output:
41.877602
-87.627197
EDIT:
I figure out that I forget to write ip in the url. http://www.geoplugin.net/php.gp?ip=
Problem SOLVED :)
Using your code returns real coordenates to my location.
Geoplugin uses MaxMind DB to get data, you can check this source.
I am new here and not a pro in php but need a small help. Actually I was given a code by my service prodvider to upload on my hosting. But when i uploaded that code on my hosting account it didnt work well and after contact the support I was told to use php curl code in some part to make it run. can anyone please help me with the issue.
Original Code:
<?php
$MobileNumber=substr($_REQUEST['VerifiedNumber'],-10);
$APIKey="<Your Dial2verify API Key Here>";
$SenderID="<6 ALPHABETIC CHARACTERS ONLY>";
$Message="<Your SMS Text Here>";
echo `curl -XPOST "http://host/SMS/SEND/$APIKey/$SenderID/$MobileNumber" -d "Msg=$Message"`;
?>
I don't know how to make the above code into curl code to make it work.
Thanks :)
The reason it did not work was that you where trying to call the command line version of cURL (and also doing the call wrong).
The best way is to use the php cURL module to make this call. Verify first that it is installed by creating an info.php file with the contents
<?php phpinfo();
If cURL is present on that page you are good to go.
Obviously I have not had the ability to test this code but something like this should work
$msisdn = substr($_REQUEST['VerifiedNumber'],-10);
$apiKey = "<Your Dial2verify API Key Here>";
$senderId = "<6 ALPHABETIC CHARACTERS ONLY>";
$message = "<Your SMS Text Here>";
$url = "http://host/SMS/SEND/$apiKey/$senderId/$msisdn";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, array('Msg' => $message));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($ch);
curl_close($ch);
echo 'Response from server:';
print_r($result);
By using `, you are using CURL via command line.
1st, check if the CURL extension for php is installed properly on you environment. Check if something about CURL is available when you print <?php phpinfo();?>
2nd, if CURL is installed, check on php.net/curl to know how to use CURL.
Here a simple example to call your webservice :
<?php
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,"http://www.host.com/SMS/SEND/$APIKey/$SenderID/$MobileNumber");
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS,"Msg=This+is+my+text+message");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$reply = curl_exec ($ch);
curl_close ($ch);
echo $reply;
?>
There's tons of info on logging in to Gmail and displaying the inbox and getting contacts etc, but I cannot figure out how to get the email itself into a variable so I can do stuff with it in PHP.
Here's what I have:
function inbox($username, $password){
$url = "https://mail.google.com/mail/feed/atom";
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_USERPWD, $username . ":" . $password);
curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($curl, CURLOPT_ENCODING, "");
$curlData = curl_exec($curl);
curl_close($curl);
return $curlData;
}
//calling the function
$em = "email#gmail.com";
$pw = "pass";
$feed = inbox($em, $pw);
$x = new SimpleXmlElement($feed);
echo "<ul>";
foreach($x->entry as $msg){
//extracting the link to the message from xml
$href = $msg->link->attributes()->href;
//create a link to the message and display title, summary
echo "<li>".$msg->title."<br />".$msg->summary."</li>";
}
echo "</ul>";
Now when I click on the link I just created it just opens the message in gmail. I want to access the html of the message in a string/variable. I've tried all kinds of things. I've tried forwarding the message link to another page to open in curl but instead of showing me the message google sends some html with yet another link to the message. If the link is clicked in the browser it again, opens in gmail, but if I try to curl a third time to this link it shows me a blank page.
The point is, my work server doesn't have imap/pop enabled and cURL is the last think I know of that can accomplish this.
I ended up using imap remotely then cURLing it back to the server in question. I've determined that gmail doesn't allow messages to be sent via cURL, it's one of those google things, like where they don't allow frames, etc
i am using curl in my program.
and my code is :
$tref = $_GET['tref'];
$url = "https://paypaad.bankpasargad.com/PaymentTrace";
$curl_session = curl_init($url); // Initiate CURL session -> notice: CURL should be enabled.
curl_setopt($curl_session, CURLOPT_POST, 1); // Set post method on.
//curl_setopt($curl_session, CURLOPT_FOLLOWLOCATION, 1); // Follow where ever it goes
curl_setopt($curl_session, CURLOPT_HEADER, 0); //Don't return http headers
//curl_setopt($curl_session, CURLOPT_RETURNTRANSFER, 1); // Return the content of the call
$post_data = "tref=".$tref;
curl_setopt($curl_session, CURLOPT_POSTFIELDS, $post_data);
// Get returning data
$output = curl_exec($curl_session);
print_r($output);
print_r($post_data);
but when i use this code in my hosting , $output not set and when use another server this code correctly.
how i doing in my server.
hey found this code :
$data = curl_exec($curl_handle);
if ($data === FALSE) {
die(curl_error($curl_handle));
} else {
$html_str .= $data;
}
and when i use this code , i face this this error :
SSL certificate problem, verify that the CA cert is OK. Details: error:14090086:SSL routines:SSL3_GET_SERVER_CERTIFICATE:certificate verify failed
then i search this error and face this link
http://unitstep.net/blog/2009/05/05/using-curl-in-php-to-access-https-ssltls-protected-sites/
and i add this code
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
before
curl_exec():
Make sure your server curl is enable..
Change ;extension=php_curl.dll
To
extension=php_curl.dll
in php.ini you also check this Call to undefined function curl_init().?
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.