How to get tweets from my home page - php

I am new in twitter Api ... but i read some in the documentations.
Now in my twitter account i have followers and they tweets displayed in my home page.
I want using PHP get all tweets that displayed in my home page ... all what i can do so far to get my tweets only.
$url = "https://api.twitter.com/1.1/statuses/user_timeline.json";
// Make Requests
$header = array(buildAuthorizationHeader($oauth), 'Expect:');
$options = array( CURLOPT_HTTPHEADER => $header,
CURLOPT_HEADER => false,
CURLOPT_URL => $url ,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_SSL_VERIFYPEER => false);
$feed = curl_init();
curl_setopt_array($feed, $options);
$json = curl_exec($feed);
curl_close($feed);
$twitter_data = json_decode($json);
thanks,

You can use home_timeline.json instead of user_timeline.json";
$url = "https://api.twitter.com/1.1/statuses/home_timeline.json";
// Make Requests
$header = array(buildAuthorizationHeader($oauth), 'Expect:');
$options = array( CURLOPT_HTTPHEADER => $header,
CURLOPT_HEADER => false,
CURLOPT_URL => $url ,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_SSL_VERIFYPEER => false);
$feed = curl_init();
curl_setopt_array($feed, $options);
$json = curl_exec($feed);
curl_close($feed);
$twitter_data = json_decode($json);

Use Twitter widget for doing like this..
sample code
<a class="twitter-timeline" href="https://twitter.com/#username" data-widget-id="438248826176933888">Tweets by #username</a>
<script>!function(d,s,id){var js,fjs=d.getElementsByTagName(s)[0],p=/^http:/.test(d.location)?'http':'https';if(!d.getElementById(id)){js=d.createElement(s);js.id=id;js.src=p+"://platform.twitter.com/widgets.js";fjs.parentNode.insertBefore(js,fjs);}}(document,"script","twitter-wjs");</script>
replace #username with your twitter name

Related

how to send two request with curl using the same headears and params

How to send two request with curl using the same headears and params, only the url change
I dont want to duplicate the code two time
$url1 = "www.example1.com"
$url2 = "www.example2.com"
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => $config->url1 . '/api/v1/orders',
CURLOPT_POST => TRUE,
CURLOPT_RETURNTRANSFER => TRUE,
CURLOPT_POSTFIELDS => json_encode($body),
CURLOPT_HTTPHEADER => $headers,
));
$response = curl_exec($curl);
Thank you
You can put everything into a function and call that:
function get_orders($url)
{
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => $url . '/api/v1/orders',
CURLOPT_POST => TRUE,
CURLOPT_RETURNTRANSFER => TRUE,
CURLOPT_POSTFIELDS => json_encode($body),
CURLOPT_HTTPHEADER => $headers,
));
$response = curl_exec($curl);
curl_close($curl);
return $response;
}
Call with
$url1 = get_orders('https://example1.com');
$url2 = get_orders('https://example2.com');
Kind of basic:
$urls[] = "https://www.example1.com";
$urls[] = "https://www.example2.com";
foreach ($urls as $url){
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => $url . '/api/v1/orders',
CURLOPT_POST => TRUE,
CURLOPT_RETURNTRANSFER => TRUE,
CURLOPT_POSTFIELDS => json_encode($body),
CURLOPT_HTTPHEADER => $headers,
));
$response = curl_exec($curl);
}
I see a function option was also answered! :) this one is with a loop.

Submitting with PHP and Curl

I've been trying all morning to get an XML string uploaded via an API so that it submits my order but no matter what I try it simply isn't working for me.
My URL:
$url = "http://example.com/SubmitOrder?apiKey=ABC123&clientID=MYId&orderXml=".$xml;
$xml is my xml details already pre-formatted.
I then put this into my curl section:
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_URL => $url
));
$resp = curl_exec($curl);
$xml1=simplexml_load_string($resp) or die("Error: Cannot create object");
print_r($xml1);
echo "Submitted";
The response I get is "Error: Cannot create object" and I can see that my details have not been submitted.
Where am iI going wrong ??
Many thanks.
You can check your response data and create data in this code example.
<?php
$url = 'https://www.w3schools.com/xml/note.xml';
$resp = file_get_contents($url);
if (resp) {
$string = simplexml_load_string($resp);
var_dump($movies);
}
but if you want get data in method curl try this.
<?php
$html_brand = 'https://www.w3schools.com/xml/note.xml';
$ch = curl_init();
$options = array(
CURLOPT_URL => $html_brand,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_HEADER => true,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_ENCODING => "",
CURLOPT_AUTOREFERER => true,
CURLOPT_CONNECTTIMEOUT => 120,
CURLOPT_TIMEOUT => 120,
CURLOPT_MAXREDIRS => 10,
);
curl_setopt_array( $ch, $options );
$response = curl_exec($ch);
switch ($http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE)) {
case 200: # OK
break;
default:
echo 'Status code HTTP: ', $http_code, "\n";
}
var_dump($response);
curl_close($ch);
die();
Check your data. And check your response status code.

Error with file_get_contents() [duplicate]

How can I read a response from Stackoverflow API in PHP? The response is GZIP-ed. I found e.g. the following suggestion:
$url = "http://api.stackoverflow.com/1.1/questions/" . $question_id;
$data = file_get_contents($url);
$data = http_inflate($data);
but the function http_inflate() is not available on the installation that I am using.
Are there some other easy ways to accomplish it?
A cool way
http://www.php.net/manual/en/wrappers.compression.php
Notice the use of a stream wrapper, compress.zlib
$url = "compress.zlib://http://api.stackoverflow.com/1.1/questions/" . $question_id;
echo $data = file_get_contents($url, false, stream_context_create(array('http'=>array('header'=>"Accept-Encoding: gzip\r\n"))));
or using curl
$ch = curl_init();
curl_setopt_array($ch, array(
CURLOPT_URL => $url
, CURLOPT_HEADER => 0
, CURLOPT_RETURNTRANSFER => 1
, CURLOPT_ENCODING => 'gzip'
));
echo curl_exec($ch);
edited--
other methods removed because they don't send an Accept-Encoding http header.
Use this to get gzip encoding json response its working like a charm
function get_gzip_json($url) {
$ch = curl_init();
curl_setopt_array($ch, array(
CURLOPT_URL => $url,
CURLOPT_HEADER => 0,
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_ENCODING => 'gzip',
));
$json = curl_exec($ch);
$json= mb_convert_encoding($json, "UTF-8", "UTF-8");
return $json;
}

Send a data request with headers

I'm using Twitter API v1.1. I've created a valid authorization header (using 0auth).
Now I want to actually send a request to Twitter for the data I want but I'm fairly new to PHP and certainly haven't got a damn clue about cURL.
So far I've got:
$authHeader = 'Authorization: 0Auth ....... Expect:'
$baseURL = 'https://api.twitter.com/1.1/statuses/user_timeline.json?screen_name='.$screenName.'&count='.$tweetCount;
Then I found the following code in twitterAPIexchange which I can't get working for me:
$options = array(
CURLOPT_HTTPHEADER => $authHeader,
CURLOPT_HEADER => false,
CURLOPT_URL => $baseURL,
CURLOPT_RETURNTRANSFER => true
);
$feed = curl_init();
curl_setopt_array($feed, $options);
$json = curl_exec($feed);
curl_close($feed);
Can anyone help me with the header formation to make this request?
You must pass the headers as an array, e.g.
curl_setopt($feed, CURLOPT_HTTPHEADER, array('HeaderName1: HeaderValue1', 'HeaderName2: HeaderValue2'));
Or, in your case,
$authHeader = array('Authorization: OAuth oauth_consumer_key...');
$baseURL = 'https://api.twitter.com/1.1/statuses/user_timeline.json?screen_name='.$screenName.'&count='.$tweetCount;
$options = array(
CURLOPT_HTTPHEADER => $authHeader,
CURLOPT_HEADER => false,
CURLOPT_URL => $baseURL,
CURLOPT_RETURNTRANSFER => true
);
$feed = curl_init();
curl_setopt_array($feed, $options);
$json = curl_exec($feed);
curl_close($feed);

How to connect to the new assembla key?

The new assembla api provides by REST-access a new authentification. i would like connect with PHP and curl, but I am not sure how I can include the api-x-key and api-x-secret as options:
The invoke with curl in terminal:
curl -H "X-Api-Key: XXX" -H "X-Api-Secret: XXX" https://api.assembla.com/v1/spaces/XXX/tickets.json
in PHP (my problem):
$ch = curl_init();
curl_setopt_array($ch, array(
CURLOPT_RETURNTRANSFER => true,
CURLOPT_URL => ' https://api.assembla.com/v1/spaces/XXX/tickets.json',
CURLOPT_POSTFIELDS => ???maybe???
));
$response = curl_exec($ch);
print_r($response);
This is my first try, without the options from api-key/api-secret including.
Send those keys as headers. Try this:
$headers = array('X-Api-Key: YOUR_KEY',
'X-Api-Secret: YOUR_SECRET'
);
$ch = curl_init();
curl_setopt_array($ch, array(
CURLOPT_RETURNTRANSFER => true,
CURLOPT_URL => ' https://api.assembla.com/v1/spaces/XXX/tickets.json',
CURLOPT_HTTPHEADER => $headers
));
$response = curl_exec($ch);
print_r($response);
Hope this helps.
Assembla API in PHP:
$headers = array('X-Api-Key: YOUR_KEY',
'X-Api-Secret: YOUR_SECRET'
);
$ch = curl_init();
curl_setopt_array($ch, array(
CURLOPT_RETURNTRANSFER => true,
CURLOPT_URL => ' https://api.assembla.com/v1/spaces/XXX/tickets.json',
CURLOPT_HTTPHEADER => $headers
));
$response = curl_exec($ch);
print_r($response);
This code prints a blank page.
No response
help required please

Categories