$ch = curl_init("http://api.twitter.com/1/statuses/user_timeline/nafhameducation.json");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$latest_tweet = curl_exec($ch);
$latest_tweet_id = $latest_tweet[0]->id_str;
curl_close($ch);
I'm using this code to get the ID of the latest tweet on my time line, However $latest_tweet_id returns an empty string, any idea why it's not getting what I need?
Try
$latest_tweet = json_decode(curl_exec($ch));
Curl returns text. Turn up your error reporting, btw, you should have been getting PHP errors, trying to access a string as an object.
Also, in debugging this, it would have been useful to var_dump($latest_tweet); after line 3 -- that should have made it clear that the type of $latest_tweet was not an object.
Related
There is a supermarket website and I need to get list of product name and price data.
The website is: http://www.sanalmarket.com.tr/kweb/sclist/30011-tum-meyveler
However, I cannot get this content with success. Every attempt finalized with a null result. I am not familiar with cURL, but it is recommended me to overcome this issue. As I see, the product list is called with Ajax - JSON and for this reason, I should follow requests to see JSON files and their contents using PHP. ...But how?
Thank you in advance.
The code I tried:
<?php
$url="https://www.sanalmarket.com.tr/kweb/sclist/30011-tum-meyveler";
$ch = curl_init();
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_URL,$url);
$result=curl_exec($ch);
curl_close($ch);
var_dump(json_decode($result, true));
?>
Your curl request did work and you are getting html response in the $result variable. The problem is that you are treating the html response string like a valid JSON string.
instead of
var_dump(json_decode($result, true));
try
var_dump($result);
Here $result is not a valid JSON string. It is a string containing the html that the server responded. So you cannot parse it directly into an array or object without using a html parser.
i am trying to use the google feed api in my wordpress site. i have enabled php with a plugin, which allows me to input php code in my pages. my hosting provider also confirmed they have curl enabled.
This is the code which iam trying to run which i got from the google developer site
(https://developers.google.com/feed/v1/jsondevguide#basic_query)
<?php
$url = "https://ajax.googleapis.com/ajax/services/feed/find?v=1.0&q=iphone5& userip=2.96.214.41";
// sendRequest
// note how referer is set manually
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_REFERER, http://www.iweb21.com);
$body = curl_exec($ch);
curl_close($ch);
// now, process the JSON string
$json = json_decode($body);
// now have some fun with the results...
?>
i don't get any results, just a blank page.
i am not a php programmer. just a novice wordpress user. i have been searching for a plugin to use the google feed api but got nowhere. so i decided to try using the code provided by google.
i Would very much appreciate any advise. thnx
Blank page means that there is a Fatal or Parse error, and error reporting is disabled in PHP settings. See this: How to get useful error messages in PHP?
In your particular case, the referer string is not enclosed in quotes and generates a Parse Error. Replace with:
curl_setopt($ch, CURLOPT_REFERER, 'http://www.iweb21.com');
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How to get useful error messages in PHP?
I can't get this php json script to work. I'm trying to get the screen name from twitter, using their api.
Here's what I did.
$send_request = file_get_contents('https://api.twitter.com/1/users/lookup.json?screen_name=frankmeacey');
$request_contents = json_decode($send_request);
echo $request_contents->screen_name;
Why is this returning a blank value every time? I've tried changing things here and there and it's just not working...
Because the data structure you get back is an array of objects, not an object.
echo $request_contents[0]->screen_name;
That data looks to be an object inside an array. Try
echo $request_contents[0]->screen_name;
Best to check first it is an array and to get the first user from it:
if (is_array($request_contents)) {
$user_info = $request_contents[0];
}
if (isset($user_info)) {
echo $user_info->screen_name;
}
It's
$request_contents[0]->screen_name
since $request_contents is an array of objects, not the object itself.
Do a
var_dump($request_contents);
to see the structure of your json.
Your page should not be blank .. you should get an error like Notice: Trying to get property of non-object in since you are calling $request_contents->screen_name which is not valid.
Try telling PHP to output all error Using
error_reporting(E_ALL);
I also prefer CURL its faster
$ch = curl_init("https://api.twitter.com/1/users/lookup.json?screen_name=frankmeacey");
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($ch);
curl_close($ch);
$request_contents = json_decode($result);
var_dump($request_contents[0]->screen_name);
Output
string 'frankmeacey' (length=11)
try to use
print_r($request_contents);
OR
var_dump($request_contents);
for checking array.
I am running the following command and getting an exception:
$headers = get_headers("http://www.moneysupermarket.com/mortgages/", 1);
How do I handle this exception (in my case, ignore this url as it is causing an exception).
I have tried:
try/catch
The code in this link: https://stackoverflow.com/a/6184127/1486303
However, I still get this error appear (which I want ignored).
Thanks!
NEW VERSION
Again this's not the right answer of the question, but avoiding the error, can give the expected result.
get_headers do an HTTP GET without Agent, so moneysupermarket.com don't like it, so use ini_set to set the default user agent in request, and all work well:
ini_set("user_agent","Mozilla custom agent");
$headers = get_headers("http://www.moneysupermarket.com/mortgages/", 1);
PREVIOUS
Apparently moneysupermarket.com reset a connection if request is not well formatted, do the request using cUrl (take from curl man page):
// create a new cURL resource
$ch = curl_init();
// set URL and other appropriate options
curl_setopt($ch, CURLOPT_URL, "http://www.moneysupermarket.com/mortgages/");
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla custom agent");
// grab URL and pass it to the browser
curl_exec($ch);
// close cURL resource, and free up system resources
curl_close($ch);
There's no exception. get_headers() returns FALSE on error. There's a warning message though, but that is no exception, thus cannot get catched. For warnings and other errors see: http://www.php.net/manual/en/function.set-error-handler.php
I have a photo application where the user can create albums. My concern is that if they delete the album via facebook andthey try to upload another picture in that album, it's eaither not going to work or create a default album and upload it to that.
So what I was thinking was to check if the album exists before uploading a picture. Here's the code I have:
exec("curl https://graph.facebook.com/10150160401046994?access_token=i_have_a_access_token_here_but_i_dont_want_to_share_it", $hi);
print_r("$hi");
However, when outputting I just get "ARRAY". I really want to see what Facebook says just as if you went to the page.
Thanks in advanceCoulton
Is there any possible reason you can't use curl for this?
$ch = curl_init('https://graph.facebook.com/10150160401046994?access_token=i_have_a_access_token_here_but_i_dont_want_to_share_it');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
$response = curl_exec($ch);
var_dump($response);
var_dump(json_decode($response)); // if you want an array.
First problem, use PHP's Curl library. Let's stay away from exec if we can :P
Second, when converting and Array to a String the converted string is equal to "Array". If you just want a quick dump of the information use var_dump( $hi );