how to post php variables to other page using curl? - php

I'm trying to use the twitter api to make a login to my website this is my first time using any api so its really confusing for me i have read a bit online searching how to make the OAuth and found a project on git-hub which allows us to connect but this will not help me to learn php so i tried the twitter documentation and found this page. The diagram in that page shows that i should post the data to the twitter api urls again searched for on google and found a stackoverflow question but don't know how to use this is there any other tutorial or book that explains all of this? This is what i understood from reading the twitter documentation if im wrong please help me rectify my mistake

Your have to first get a request token.
Try this method, it should work (not tested) :
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'http://api.twitter.com/oauth/request_token');
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC ) ;
curl_setopt($ch, CURLOPT_USERPWD, $clientId.":".$clientSecret);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($yourPost));
$response = curl_exec($ch);
Then follow the instruction of step 2 using CURL with GET

Related

integrating php with bigcommerce api

I am a newbie to the whole concept of the Bigcommerce Api, but my php programming is good.
I am having problems with identifying where to start exactly if i am going to integrate the php code that I may have created with the bigcommerce api,
Where do my files go? How do they interact exactly...
On the Bigcommerce site, they define what its all about, and provide the basic php codes.
Google is my friend but she hasn't been able to help me with links for the whole "How to get started" part...
Or maybe I am asking her the wrong questions.
My ultimate task is to take data from an xml file and use php to handle the rest of the processing and displaying..
I am a newbie and I am not sure how it really works. But links to proper tutorials will really help
Apparently i had no idea Big-commerce doesn't need me to host any server code on their side or whatever,
i found out that which ever code i write is simply run on my side so i can interact with my store.
i wonder why i couldn't really find this information anywhere easily, shouldn't it be the first thing we see when we reach the developers page on bigcommerce.com??
Anyway, i found what i was looking for and i understand how it works now.
To begin, as far as where do your PHP scripts go, those will be hosted external to your Bigcommerce store from any server that has PHP installed or from your local computer. The Bigcommerce API basically gives you a way to access and make changes to your store's database using a program. You will interact with API resources endpoints (URLs) which have been tied to specific store related data, like your products or orders. You can make GET, PUT, POST, and DELETE requests on these URLs in order to pull, modify, create, or delete store related data, respectively.
the php files are here https://github.com/bigcommerce/bigcommerce-api-php ,
and these should be in your machine with a PHP server
to get started there are sample codes there also.
i had problems with this line
require 'vendor/autoload.php';
so i changed it to
require 'path_to_this_file/bigcommerce.php';
everything is working fine now...
but iam still learning more
Have a look at this Link for the list of Resources.
Your php file will be making calls to BigCommerce API, to get data from BigCommerce Store.
Simple cURL snippet to get orders.
$api_url = 'https://YOUR-API-PATH.mybigcommerce.com/api/v2/orders.json';
$ch = curl_init(); curl_setopt( $ch, CURLOPT_URL, $api_url );
curl_setopt( $ch, CURLOPT_HTTPHEADER, array ('Accept: application/json', 'Content-Length: 0') );
curl_setopt( $ch, CURLOPT_CUSTOMREQUEST, 'GET');
curl_setopt( $ch, CURLOPT_SSL_VERIFYPEER, 0 );
curl_setopt( $ch, CURLOPT_USERPWD, "YOUR-USERNAME:YOUR-API-TOKEN" );
curl_setopt( $ch, CURLOPT_SSL_VERIFYHOST, 0 );
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, 1 );
$response = curl_exec( $ch );
$result = json_decode($response);
print_r($result);
Hope this helps.
For that you can not make directly call to your store front..for that make request from third party server to get your store Api.
call bellow php code by Ajax request...
like that..
----------Php Code:-
$username='username';//API User name
$password='xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx';//API Token
$URL='https://store-xxxxxxx.mybigcommerce.com/api/v2/products';//API path
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$URL);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type:UTF-8','Accept: application/json'));
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
curl_setopt($ch, CURLOPT_USERPWD, "$username:$password");
$status_code = curl_getinfo($ch, CURLINFO_HTTP_CODE); code
$result=curl_exec ($ch);
curl_close ($ch);
print_r($result);//Your Requested Data

How to use Qualys API v2 with PHP?

I am trying to use the Qualys API v2 to get an xml host list returned. I think you must use cURL, but I am unfamiliar with it. Here is my code:
$ch = curl_init();
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_USERPWD, "$username:$password");
curl_setopt($ch, CURLOPT_URL, "https://qualysapi.qualys.com/api/2.0/fo/asset/host/?action=list&details=Basic");
curl_setopt($ch, CURLOPT_HTTPHEADER, array('X-Requested-With: Manitowoc Service Account'));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
$xml = curl_exec($ch);
curl_close();
$hostobj = simplexml_load_string($xml);
Actually, curl is not required for QualysGuard API calls. As long as you can make https calls you can use any method you would like. But curl is a nice framework because everything is already implemented (with perl, another alternative would be LWP).
I have not been able to find the issue with your code, but I posted a perl script that launches and downloads a Qualys report by making API requests "using WWW::Curl::Easy": https://community.qualys.com/docs/DOC-3222
I hope it could help you to write your own perl API request with libcurl.
I see nothing wrong with your code, but of course we can't see what values you are using for the username and password, and whether those credentials actually exist in QualysGuard.
To get status/error information from curl for a given request, use curl_getinfo(), curl_error(), and curl_errno() as described in the cURL Manual.

Can't get this content spinning API to work with PHP

Can you help me get this content spinning API working? It was wrote to work with C# but is there a way I can get it to work using PHP? I've been trying to post to the URL stated on that page using cURL, but all I'm getting back is a blank page. Here's the code I'm using:
$url = "http://api.spinnerchief.com/apikey=YourAPIKey&username=YourUsername&password=YourPassword";
// Some content to POST
$post_fields = "SpinnerChief is totally free. Not a 'lite' or 'trial' version, but a 100% full version and totally free. SpinnerChief may be free, but it is also one of the most powerful content creation software tools available. It's huge user-defined thesaurus means that its sysnonyms are words that YOU would normally use in real life, not some stuffy dictionary definition. And there's more...SpinnerChief can only get better, because we listen to our users, We take onboard your ideas and suggestions, and we update SpinnerChief so that it becomes the software YOU want!";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_PORT, 9001);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_fields);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$result = curl_exec($ch);
curl_close($ch);
echo $result;
Can anyone see something wrong I'm doing? Thanks a lot for the help.
The value for CURLOPT_POST should be 1, and the posted data should be set with CURLOPT_POSTFIELDS.

cURL Authentication Problems Datatel R25 XML Feed

I am still fairly new at playing with PHP. I wanted to poke at a small project. I have the ability to access an xml feed from our calendar system at work. The feed requires authentication with a username and password. I am trying to automate pulling this feed. My hope is to one day use it to automate the scheduling of another system but for now I'm just trying to get this side working.
I have read through some of the cURL documentation on php.net. I have also searched through stackoverflow for a few examples. They were helpful in getting me started but not much further. Below is what I have so far. But all I get back is:
UNAUTHORIZED The request requires
authorization
So I'm racking my head to figure out what might be the issue. Part of my problem I'm sure is my ignorance with the actual process behind what is going on between the client and server. When I interact with it normally I just go to the feed URL, the browser prompts me for a username and password, and then after putting that in it gives me an xml page.
I've checked for most of the formatting errors and syntax errors I knew to look for. That may still be it but at this point I think it's more my lack of understanding how it all fits together. Thanks for any help.
Similar issues:
Parsing XML data with Namespaces in PHP
CURL HTTP Authentication at server side
<?php
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://somesight.someplace.edu/r25ws/servlet/wrd/run/events.xml");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_USERPWD, "username:password");
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, Array("Content-Type: text/xml"));
curl_setopt($ch, CURLOPT_UNRESTRICTED_AUTH, 1);
$output = curl_exec($ch);
curl_close($ch);
echo $output;
?>
I figured it out on my own after looking at a similar tutorial for Twitter. I tried a different approach to formatting the options and it worked fine. I also cut down on the number of options I was sending because it seemed like overkill. Code that works below.
<?php
$username = 'myusername';
$password = 'mypassword';
$datecall = curl_init("http://somesite.someplace.edu/r25ws/servlet/wrd/run/events.xml");
curl_setopt($datecall, CURLOPT_USERPWD, $username.":".$password);
curl_setopt($datecall, CURLOPT_RETURNTRANSFER, 1);
$result = curl_exec($datecall);
echo $result;
?>

I can't post to Twitter using cURL and PHP

I am unable to tweet continuously in twitter - every three tweets I get:
Error posting to Twitter. Retry
How do I fix this?
My code is as follows:
$host = "http://twitter.com/statuses/update.xmlstatus=".urlencode(stripslashes(urldecode($message)));
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $host);
curl_setopt($ch, CURLOPT_VERBOSE, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_USERPWD, "$username:$password");
curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Expect:'));
$result = curl_exec($ch);
// Look at the returned header
$resultArray = curl_getinfo($ch);
//print_r($resultArray);
if($resultArray['http_code'] == "200"){
$twitter_status='Your message has been Tweeted';
update_tweet_extra($result_id,"1");
} else {
$twitter_status="Error posting to Twitter. Retry";
// update_tweet_extra($result_id,"0");
}
echo $twitter_status;
You can't use API with basic authentication since 31 august 2010. You can use a small spider function with curl that login in, fetch your home, and tweet. I made it:
http://www.barattalo.it/2010/09/09/how-to-change-twitter-status-with-php-and-curl-without-oauth/
If you don't want to use oAuth authentication model you have to do this way.
I don't think this is a problem with your code, but more the fact that Twitter is phasing out basic authentication support and moving to OAuth.
There's more information at http://apiwiki.twitter.com/Authentication.
This seems very odd, since status updates via the Twitter API are not rate-limited (see here), unless you're updating more than 1000 times a day.
You might work out more of what happened if you uncomment that helpful looking line:
//print_r($resultArray);
by removing the // at the beginning of line.
to tweet using twitter you will need a post_authenticity_token along with your username and password.
this token can be obtained from your profile page by fetching it using curl (after you login with curl). i experimented with curl and was able to tweet using curl. you can find my code at (though it is in bash script, it can be ported to php easily coz they both use curl)
http://pastebin.com/a5eBcEeP .
You can find a list of PHP libraries that support OAUTH and you can use to write a tweet function in PHP and the 1.1 version of the Twitter API here:
https://dev.twitter.com/docs/twitter-libraries
tmhOAuth is probably my favorite.

Categories