Test Laravel API on localhost cannot get response - php

So have this laravel get product api that works if i called it from the browser as you can see from this image
here is the curl code chunk that tried to get this json response
$url = 'http://localhost:8000/api/products';
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_TIMEOUT, 5);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$data = curl_exec($ch);
curl_close($ch);
$result = json_decode($data,true);
echo $result;
but i cannot get anything in result, nothing to echo. I have one mockable api in https://demo0546443.mockable.io/products/getAllProducts that produce nearly the same json as my localhost api and it return response as it should have. please help
SOLUTION
So after countless hours researching about this, it seems laravel needs to be accessed on static ip, not on localhost.

It’s recommended to use a library like Guzzle though to make HTTP requests instead though.its very eazy to implement and work with

Rather than echo $result try return response()->json($data).
echo $result return Header application/html while json() return application/json hope this helps.

Related

Rest API giving NULL in response

I am working with rest API. I am using zoho API's for making calls. Working in yii2 I am trying to call a GET API which gives me some details about my project.
/* Set the Request Url (without Parameters) here */
$request_url = 'https://projectsapi.zoho.com/restapi/portal/[PORTALID]/projects/[PROJECTID]/bugs/defaultfields/';
$ch = curl_init($request_url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_TIMEOUT, 0);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('authtoken' => 'key')); // here i am using key but in actual i am putting the real key value
$curl_response = curl_exec($ch);
$json = json_decode($curl_response);
var_dump($json);
exit();
Accessing it via POSTMAN when I run this call it gives me NULL in response. I have also tried out the way mentioned in the PHP Example here. But it doesn't work out for me as well.
I must be missing something that I don't know.
Any help would be highly appreciated.
Try checking your headers and make sure you're passing through all the required fields for an example authorization, content type etc.

What am I doing wrong here (CURL), no matter what I try it returns empty/null

$url = "http://www.reddit.com/r/{mysubreddit}/new.json";
$fields = "sort=new";
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $fields);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$data = curl_exec($ch);
curl_close($ch);
var_dump($data);
{mysubreddit} is whatever subreddit I wanna check. It works fine to just grab that url via postman, or even in the browser. But when I use PHP/CURL, it returns empty. I've tried replacing the URL, with another URL to another site, and it works fine, so the curl part is working fine.
Is there something with reddit that I have to set? headers? or explicitly tell it for JSON? Or what?
I thought it might have to do with POST, but I tried GET to, still empty/null.
$url = "http://www.reddit.com/r/{mysubreddit}/new.json?sort=new";
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$data = curl_exec($ch);
curl_close($ch);
That doesnt work either
You just need to add:
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
As others have mentioned, reddit is sending you a 302 redirect to https. You would be able to see that by examining the headers returned by curl_getinfo().
Enabling redirect following, as sorak describes, will work. However, it's not a good solution - you will make two HTTP requests on every single API call. This is a completely unnecessary waste of network and increases the execution time of your script. Instead, just change the url that you're requesting to be from https://www.reddit.com/ in the first place.

How to get latest instagram posts using php

I want to get latest posts from my instagram account and I'm using this api url: https://api.instagram.com/v1/users/[USER_ID]/media/recent?access_token=[ACCESS_TOKEN]
When I paste this url in the browser I can see the string JSON but I can not find a way to decode it and use object properties. I'm using PHP and cURL for getting and decoding JSON. It always return NULL. My cURL function is this:
function fetchData($url){
$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);
return $result;
}
and at the end:
$url = https://api.instagram.com/v1/users/[USER_ID]/media/recent?access_token=[ACCESS_TOKEN]
$result = fetchData($url);
var_dump(json_decode($result));
what am I doing wrong here?
I also tested file_get_contents instead or cURL but it gave me this error:
No connection could be made because the target machine actively refused it
The problem with this endpoint:
https://api.instagram.com/v1/users/[USER_ID]/media/recent?access_token=[ACCESS_TOKEN]
is that it is not possible to get public media with your own access token. One can only get self media with generated token. So the correct endpoint should be
https://api.instagram.com/v1/users/self/media/recent?access_token=[ACCESS_TOKEN]
this works on server and client, but because instagram access token must be secured I'm using server side script to get latest posts.

Curling Instagrams API

I am trying to curl the url https://www.instagram.com/<handle>/media/ to get my profiles data as json. How come when I hit this url in a browser I get the correct response but when I try to curl it using php as follows:
$url = "https://www.instagram.com/<handle>/media/";
$curl_connection = curl_init($url);
curl_setopt($curl_connection, CURLOPT_CONNECTTIMEOUT, 30);
curl_setopt($curl_connection, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl_connection, CURLOPT_SSL_VERIFYPEER, true);
$json = curl_exec($curl_connection);
curl_close($curl_connection);
echo $json;
I do get a response:
{"status":"ok","items":[],"more_available":false}
however it is not very useful and not the same as I get when I hit the url in a browser.
This leads me to believe that I am not curling correctly.
Any suggestions?
FOLLOW UP:
I tried file_get_contents($url); which seems to give some users data correctly but for my account it does not. Is there any reason for this?
I'd suggest using file_get_contents instead:
file_get_contents("https://www.instagram.com/derek/media/");
worked perfectly for me!
Hope this helps :-)

Redmine REST API, update issue using PHP

I am new to APIs and I am having trouble updating the status of issues. I have successfully listed projects and issues but not sure how to update them. I am using PHP to access the API. Can some one please share some example in php to how to update an issue using json. I have read the API documentation but its too minimal. I am not familiar with PUT and I cant find any good example or tutorial on the internet. Thankyou!
This what I tried but didnt work
function update_issue($issue_id){
// set url
$planio_url = "http://something.plan.io/issues/".issue_id.".json?key=185f1edsadfsdafy86578ce82ea70a0eb4267";
// create curl resource
$ch = curl_init();
$data = '{"issue":{"status":{"name":"done","id":21},"notes":"closing all old issues"}}';
$data = json_decode($data);
$ch = curl_init($planio_url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT");
curl_setopt($ch, CURLOPT_POSTFIELDS,http_build_query($data));
$response = curl_exec($ch);
if(!$response) {
return false;
}else{
return $response;
}
}
Returns empty response.
For everybody who wants to copy the snippet above. to update a ticket (like i did)
The URL in $planio_url is missing a $
..issues/".issue_id.".json
must be:
issues/".$issue_id.".json
also i decoded the json with true as second parameter to get an array for http_build_query
works like a charm.

Categories