I'm attempting to access an API for a task management system (Nozbe to be exact) that is outlined here: http://www.nozbe.com/api
If I go in my browser and access the URL it returns the correct json response:
{"response":"644a40436"}
However, when I attempt to access this URL with cURL in PHP, it doesn't create the note like it would if I accessed it manually in my browser.
The normal method is outlined below:
http://www.nozbe.com/api/newnote/name-test/body-test/project_id-c4ca1/context_id-c4ca1/key-1a2b3c4d5e6f7g8h9i0j1k2l3m4n5o6
$api_key = "INSERTAPIKEYHERE";
$project_id = "73d173457";
$eventtitle = "Testing";
$descrip = "This is a test";
$url = "http://www.nozbe.com/api/newnote/name-$eventtitle/body-$descrip/project_id-$project_id/key-$api_key";
echo "$url<br/><br/>";
// create a new cURL resource
$ch = curl_init();
// set URL and other appropriate options
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
// grab URL and pass it to the browser
$response = curl_exec($ch);
// close cURL resource, and free up system resources
curl_close($ch);
Does anyone have any advice or pointers as to why this isn't working? I know it's probably a pretty obscure API I'm attempting to access.
Note that the API says that the body and the like must be URL-encoded. Instead, you have spaces in your URL. Try running urlencode on the arguments before placing them into the URL.
Related
I am trying to access API data from CraftingStore Public API.
When I am trying it on localhost, everything works like it should (1st pic, also printing results for to show).
However, when I am trying to view it on the actual website (2nd pic), it is not working but instead saying to enable cookies and to complete captcha.
When on localhost:
When on website:
Also code for accessing the API:
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://api.craftingstore.net/v7/payments?page=1');
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'token:'.$cs_token
));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($ch);
curl_close($ch);
$obj = json_decode($result);
$dononame = $obj->data[0]->inGameName;
$donobuy = $obj->data[0]->packageName;
What can I do?
This is quite usual for API's with Cloudflare. You'll need to make a page rule for the API domain/URI to disable the Browser Integrity Check.
Source: https://support.cloudflare.com/hc/en-us/articles/200504045-Using-Cloudflare-with-your-API
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.
I have a cURL request in my code which works fine when running locally:
$url = "http://ipinfo.io/{$_SERVER['REMOTE_ADDR']}";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
$response = curl_exec($ch);
$locale = json_decode($response);
and returns a JSON as expected. Our production system is on Google App Engine, however, where I get the website version for a browser rather than the JSON.
I can get this cURL request to work if I change
google_app_engine.enable_curl_lite = "1"
in the php.ini in the root directory of my project to
extension = "curl.so"
but Google's documentation insists the former is to be used on production. Additionally, using the latter breaks things like Monolog's SlackHandler.
Is there a way to get the JSON from this cURL request while still using Google's "cURL Lite"?
From the ipinfo.io documentation:
"We do a little bit of magic on the server to determine if we should send the JSON response or the webpage. We usually get it right, but if you're seeing the webpage instead of the JSON (or want to check the JSON output in a browser) you can force the JSON response by adding /json to the end of the URL"
Adding /json to the end of the URL worked for me in this case, but I wanted a more general solution. Since Google's cURL Lite uses their URL Fetch in the background, ipinfo.io's "magic" is somehow getting confused. I found that by specifying the Accept header then the /json addition wasn't required. In PHP:
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Accept: application/json'));
Thanks to the folks on the #php channel of NashDev Slack for helping me on this one!
Hi I am new to php and want to know some alternate function for the header('location:mysit.php');
I am in a scenario that I am sending the request like this:
header('Location: http://localhost/(some external site).php'&?var='test')
something like this but what I wanna do is that I want to send values of variables to the external site but I actually dont want that page to pop out.
I mean variables should be sent to some external site/page but on screen I want to be redirected to my login page. But seemingly I dont know any alternative please guide me. Thx.
You are searching for PHP cUrl:
$ch = curl_init();
// set URL and other appropriate options
curl_setopt($ch, CURLOPT_URL, "http://www.example.com/");
curl_setopt($ch, CURLOPT_HEADER, 0);
// grab URL and pass it to the browser
curl_exec($ch);
// close cURL resource, and free up system resources
curl_close($ch);
Set the location header to the place you actually want to redirect the browser to and use something like cURL to make an HTTP request to the remote site.
The way you usually would do that is by sending those parameters by cURL, parse the return values and use them however you need.
By using cURL you can pass POST and GET variables to any URL.
Like so:
$ch = curl_init('http://example.org/?aVariable=theValue');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($ch);
curl_close($ch);
Now, in $result you have the response from the URL passed to curl_init().
If you need to post data, the code needs a little more:
$ch = curl_init('http://example.org/page_to_post_to.php');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, 'variable1=value1&variable2=value2');
$result = curl_exec($ch);
curl_close($ch);
Again, the result from your POST reqeust is saved to $result.
You could connect to another URL in the background in numerous ways. There's cURL ( http://php.net/curl - already mentioned here in previous comments ), there's fopen ( http://php.net/manual/en/function.fopen.php ), there's fsockopen ( http://php.net/manual/en/function.fsockopen.php - little more advanced )
This question is simple. What function would I use in a PHP script to load data from a URL into a string?
CURL is usually a good solution: http://www.php.net/curl
// create a new cURL resource
$ch = curl_init();
// set URL and other appropriate options
curl_setopt($ch, CURLOPT_URL, "http://www.example.com/");
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
// grab URL and pass it to the browser
$html = curl_exec($ch);
// close cURL resource, and free up system resources
curl_close($ch);
I think you are looking for
$url_data = file_get_contents("http://example.com/examplefile.txt");
With file wrappers you can use file_get_contents to access http resources (pretty much just GET requests, no POST). For more complicated http requests you can use the curl wrappers if you have them installed. Check php.net for more info.
Check out Snoopy, a PHP class that simulates a web browser:
include "Snoopy.class.php";
$snoopy = new Snoopy;
$snoopy->fetchtext("http://www.example.com");
$html = $snoopy->results;