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.
Related
stupid question I think.
I have an API that i want to access. if I simply put the url in my browser it returns all the results correctly.
https://api.mydomain.com/v1/name?user=user1&pass=1234
in my browser this returns array information:
{"firstname":"John","Surname":"Smith"}
I want to be able to use PHP to simply assign the results of the URL page to a variable:
$url="https://api.mydomain.com/v1/name?user=user1&pass=1234";
$result=parse($url);
print_r($result);
This obviously doesnt work but just looking for the correct syntax. done some research but not getting any luck. should be so simple but is not.
advice appreciated as always.
Thanks
Just make a request to your API service:
$url="https://api.mydomain.com/v1/name?user=user1&pass=1234";
$result = file_get_contents($url);
Then, if I understand correctly, your API returns JSON response, so you have to decode it:
$vars = json_decode($result, true);
Which will return an array with all the variables:
echo $vars['firstname']; //"John";
echo $vars['Surname']; //"Smith";
solution: file_get_contents (or maybe you'll need curl if ini_get("allow_url_fopen") !=1 ....)
$url="https://api.mydomain.com/v1/name?user=user1&pass=1234";
$result=parse(file_get_contents($url));
print_r($result);
hope your "parse()" function knows how to parse the result from your api call. i guess you don't know what you're doing, and next you'll be asking why the parse function is not defined :p (it looks like you're looking for json_decode , just a guess.)
i think your parse function would look like:
function parse($apiresponse){return json_decode($apiresponse,true);}
If you have the CURL library installed, you could do this:
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://api.mydomain.com/v1/name?user=user1&pass=1234');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$result = curl_exec($ch);
$vars = json_decode($result, true);
//do something useful with the $vars variable
Cannot you do some search?
Take a look at Google with keywords "PHP get results from URL"
Hi I've researched this problem here pretty extensively and on other sites and have not found my answer. First off let me start by saying I am trying to learn this all on my own so any advice is welcome.
I am trying to curl a website where one of our applications is hosted on. I am attempting to dump it's contents into a div for a sales demo and I am having difficulty doing so. Since it is a Https protocol an simple iFrame will not do the job. Again this is our company's owned app but hosted on a social network site.
Here is my code at the head level:
<?php
// Defining the basic cURL function
function file_get_html($url) {
$ch = curl_init(); // Initialising cURL
curl_setopt($ch, CURLOPT_URL, "https://www.a_website.com");
curl_setopt($ch, CURLOPT_URL, 1);
curl_setopt($ch, CURLOPT_URL, $url); // Setting cURL's URL option with the $url variable passed into the function
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE); // Setting cURL's option to return the webpage data
$data = curl_exec($ch); // Executing the cURL request and assigning the returned data to the $data variable
// var_dump($data);
curl_close($ch); // Closing cURL
return $data; // Returning the data from the function
}
?>
I've already tried the var_dump($data) suggestion which I've seen as an answer for similar posts such as mine but I being a newbie don't understand what that actually does. I know from reading other posts here and elsewhere that "Notice: Trying to get property of non-object in C:\xampp\htdocs\fps.php on line 117" is not and error but a warning/notice.
Any guidance will be appreciated.
Attempting to output content to:
<div>
<?php
// Dump contents (without tags) from HTML
echo file_get_html("www.facebook.com/appcenter/sgprivacy")->html;
?>
</div>
Any guidance will be appreciated.
There error is accurate here is the problem:
<div>
<?php
// Dump contents (without tags) from HTML
echo file_get_html("www.facebook.com/appcenter/sgprivacy")->html; // this here
?>
</div>
The value file_get_html returns is not an object, it is a string. Change it to this:
echo file_get_html("www.facebook.com/appcenter/sgprivacy");
if you want to remove all html tags, you can use the strip_tags() function.
$data = file_get_html("www.facebook.com/appcenter/sgprivacy");
echo strip_tags($data);
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How to read a web page in PHP
I'm sure there is some simple way to do this. I need to pass get variables through to my cart software to record a conversion, but not redirect the user, I just want the server to send GET variables to a URL. I'd rather not turn on allow_url_fopen in php.ini.
Anyone know the best way to do this? Thanks in advance.
Server side, your best option is probably to use cURL - see the documentation for details, it's not too difficult to use.
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://www.example.com/script.php");
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$response = curl_exec($ch);
curl_close($ch);
if ($response === false) {
// Failed to connect or some error occurred
} else {
// Everything was fine, check the response here if you need to
}
$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.
I'm sending some data to an external URL using Curl. The server sends me back a response in a string like this:
trnApproved=0&trnId=10000002&messageId=7&messageText=DECLINE
I can assign this string to a variable like this:
$txResult = curl_exec( $ch );
echo "Result:<BR>"; echo $txResult;
But how do I use the data that is sent back? I need a way to get the value of each variable sent back so that I can use it in my PHP script.
Any help would be much appreciated.
Thanks.
The default behavior of Curl is to just dump the data you get back out to the browser. In order to instead capture it to a variable, you need:
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$txResult = curl_exec($ch);
That default behavior has always annoyed me. Returning the data from the curl_exec() call seems by far the more correct choice to me.
Use parse_str():
parse_str($txResult, $txArr);
var_dump($txArr);