json_decode limitations? - php

I am trying to decode the JSON at https://api.twitter.com/1/statuses/user_timeline.json?include_entities=true&incslude_rts=0&screen_name=microsoft&count=200&exclude_replies=1&contributor_details=0 with json_decode() in PHP (decoding as an associative array, so the second parameter is set to TRUE.
The problem is that it seems to not do anything (no error, warning either). The data contains 200 tweets + some extra data about them. If I fetch only let's say 50 tweets, the json_decode function runs successfully.
So my question is: is json_decode not able to decode large strings?
Edit: my code:
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url); // $url is the above mentioned URL
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST,0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER,0);
$content = curl_exec($ch);
print_r(json_decode($content,true));

There is something wrong with your cURL I suppose. I just tried the following code with the URL you have provided and works just fine:
$curl = curl_init();
$url = 'https://api.twitter.com/1/statuses/user_timeline.json?include_entities=true&incslude_rts=0&screen_name=microsoft&count=200&exclude_replies=1&contributor_details=0';
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_URL, $url);
$content = curl_exec($curl);
var_dump(json_decode($content, true));

Related

My PHP curl Post request didnt work as expected

I try to send a post request to another php page with the following code:
$vars = ['val1' => 'myval'];
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POSTFIELDS, $vars);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_REFERER, $_SERVER['REQUEST_URI']);
curl_setopt($ch, CURLOPT_VERBOSE, TRUE);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, TRUE);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
curl_setopt($ch, CURLOPT_MAXREDIRS, 4);
curl_setopt($ch, CURLOPT_POST, TRUE);
curl_setopt($ch, CURLOPT_USERPWD, "username:password");
$data = curl_exec($ch);
curl_close($ch);
But on the target page, var_dump($_POST); will output only "1". I had expected something like:
array(val1 = myval) or something similar. So that i can check with isset($_POST["val1"]) if this exists and contains "myval".
Any ideas whats wrong with the request?
Edit:
I have now changed my code to the following:
<?php
$data = array(
'foo'=>'bar',
'baz'=>'boom',
'cow'=>'milk',
'php'=>'hypertext processor');
$ch=curl_init();
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/x-www-form-urlencoded'));
curl_setopt($ch, CURLOPT_URL, "target.php");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
curl_close($ch);
echo $response;
?>
And in target.php:
<?php
echo print_r($_POST, true));
?>
But there is no response. Both files are in the same dir at the server.
That's because curl_setopt() doesn't accept such complext types as array ;) You need to present your data in form of a query string: "postvar1=value1&postvar2=value2&postvar3=value3".
$format = http_build_query($vars);
curl_setopt($ch, CURLOPT_POSTFIELDS, $format);
or
$format = "val1=my-value"; // be careful to create proper encoding
curl_setopt($ch, CURLOPT_POSTFIELDS, $format);
If you're using CURLOPT_POSTFIELDS, you don't need to set CURLOPT_POST as well. In fact, setting it after will result in a potentially incorrect header being sent.
CURLOPT_POSTFIELDS accepts an array, and will set the Content-Type header to multipart/form-data if one is provided. Later, setting CURLOPT_POST will overwrite this to application/x-www-form-urlencoded, and means that the PHP script at the other end will expect data encoded as an HTTP query-string. This is why you're having problems.
You can fix this either by encoding $vars correctly before sending (using http_build_query, as in the other answer), or just remove the call to set CURLOPT_POST. I'd recommend the latter.
I think, i got it. When i set as target something like "target.php", data was not send. But when i use a complete URL, like https://myserver.com/target.php, this will work as expected. Sometimes, its to easy... sorry for stealing your time.

Make PHP Request to tomcat localhost using cURL and get response

I'm new to PHP and am currently struggling to set this up. Could someone provide me an outline of how you use curl to post and retrieve the returning data?
I tried this out:
$url = 'http://localhost:8080/ds/stuff?maybe=false';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, $string);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
curl_close($ch);
But I keep getting the HTTP status 405 Error
(aka this):
Am I doing something wrong/what should I do? Or do I have to change something in my ds/stuff
You should set CURLOPT_POST to true. POST data goes to CURLOPT_POSTFIELDS.
$url = 'http://localhost:8080/ds/stuff?maybe=false';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $string);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
curl_close($ch);

CURL + transfer data with Base64

I'm using the following CURL Call and its transfer successfully.
I've tried to send an encrypted key - which changes with each encrypted (using AES) but looks something like this: cpZa˜Hó”™itz²÷ðt?=þ|w±I†ïÛì„¡
I've been told I need to use base64 - have tried utf8_encode below.
How do I use base64?
Is this an option added to CURL or something encoded and decoded in PHP?
$data = array('validation' => '1', 'encryptkey' => utf8_encode($encryptedDate));
//utf8_encode()
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'http://www.domain.com/this.php');
curl_setopt($ch, CURLOPT_FAILONERROR, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_USERPWD,$authentication);
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($ch, CURLOPT_REFERER,$_SESSION['domainname']);
curl_setopt($ch, CURLOPT_POSTFIELDS,$data);
$result = curl_exec($ch);
curl_close($ch);
echo $result;
Use base64_encode().
Simply swap utf8_encode() with base64_encode().

php google weather api query

I'm having trouble retrieving data from the google api. When I run the code, it only returns a blank page and not a printout of the xml array. Here is the code:
$url="http://www.google.com/ig/api";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, "?weather=london,england");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$data = curl_exec($ch);
curl_close($ch);
echo "<pre>";
print_r($data);
I think that the problem is that you use POST method, and not the GET
Try like this
$url="http://www.google.com/ig/api?weather=london,england";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$data = curl_exec($ch);
curl_close($ch);
Hope it helps :)
EDIT: And yes, you have to do some extra parsing to get the data from the XML string

Not getting HTML using curl, why?

Why am I not getting the HTML code when I use PHP curl? This is my code:
// $content = file_get_contents('http://www.datadiary.com/Company/311734/dimsinstituteofhotelmanagement');
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'http://www.datadiary.com/Company/311734/dimsinstituteofhotelmanagement');
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_COOKIESESSION, false);
$content = curl_exec($ch);
curl_close($ch);
echo $content;
By default curl_exec sends the response to the output (usually the browser). Set the CURLOPT_RETURNTRANSFER option if you want curl_exec to return the result instead:
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
Relevant manual entries:
http://php.net/curl-exec
http://php.net/curl-setopt
You need to add the option to return the transfer as a string:
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

Categories