I'm trying to download json data using curl in PHP from companies house API.
Here is the example they provide that I'm trying to use:
https://developer.companieshouse.gov.uk/api/docs/company/company_number/readCompanyProfile.html#here
The example:
curl -uYOUR_APIKEY_FOLLOWED_BY_A_COLON: https://api.companieshouse.gov.uk/company/{company_number}
My code
<?php
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, "https://api.companieshouse.gov.uk/company/01000000");
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_HTTPHEADER, array(
'curl -u8yUXrestOfmyApiKey:'
));
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0);
$result = curl_exec($curl);
if(false == $result){
echo curl_error($curl);
}
curl_close($curl);
print($result);
?>
Using header:
'-u my_api_key:8yUXGgcHznjaNWnKpuKIJ7yv7HDvU_slH273GuF1'
I get 400 - bad request and with header in my code above I get nothing no errors blank page no data.
PS. There is a company with number 01000000 I've checked
You shuld add CURLOPT_USERPWD
curl_setopt($curl, CURLOPT_USERPWD, "YOUR API KEY"); #Add your api key
Related
I am trying to integrate Foursquare API to my website, here is the code:
$curl = curl_init('https://api.foursquare.com/v2/photos/add?v=20181008&oauth_token='.$token.'&photo='.$args['img'].'&venueId='.$title);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_HTTPHEADER, array("Content-type: multipart/form-data"));
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
$data = curl_exec($curl);
curl_close($curl);
// Decode the response
$data = json_decode($data);
// Verify if the post was published
if ( #$data->meta->code == 200) {
return true;
} else {
return false;
}
But when I run this script on my website and try to post image, it is showing
An error occurred while processing your request
Remove the lines
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_HTTPHEADER, array("Content-type: multipart/form-data"));
and try a simple
curl_setopt($curl, CURLOPT_POST, false);
You are sending the Request with GET Arguments
This could have multiple reasons.
I can't tell from the error message where exactly it triggers but when the curl fails, you should first investigate if there were any issues with the curl request itself.
Use curl_error for this: http://php.net/manual/de/function.curl-error.php
And are you using the API-Call the correct way?
In the docs POST https://api.foursquare.com/v2/photos/add
theres no $photo param at all.
Also when using POST you should add the parameters via
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
and not as GET-Parameters via URL.
I've written some simple code which should enable the retrieval of a given webpage, in this case Google.
<?php
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://www.google.com/");
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_exec($ch);
curl_close($ch);
?>
Although it works, I've noticed when I click some of the hyperlinks, for instance the 'Privacy' hyperlink, I get redirected to http://mywebsite.com/intl/en/policies/privacy/ which obviously doesn't exist. Why does this happen? And is it possible to get redirected to the correct link?
<?php
function cURL() {
// Create a new cURL resource
$curl = curl_init();
if (!$curl) {
die("Couldn't initialize a cURL handle");
}
// Set the file URL to fetch through cURL
curl_setopt($curl, CURLOPT_URL, "http://ctrlq.org/");
// Set a different user agent string (Googlebot)
curl_setopt($curl, CURLOPT_USERAGENT, 'Googlebot/2.1 (+http://www.google.com/bot.html)');
// Follow redirects, if any
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
// Fail the cURL request if response code = 400 (like 404 errors)
curl_setopt($curl, CURLOPT_FAILONERROR, true);
// Return the actual result of the curl result instead of success code
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
// Wait for 10 seconds to connect, set 0 to wait indefinitely
curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 10);
// Execute the cURL request for a maximum of 50 seconds
curl_setopt($curl, CURLOPT_TIMEOUT, 50);
// Do not check the SSL certificates
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
// Fetch the URL and save the content in $html variable
$html = curl_exec($curl);
// Check if any error has occurred
if (curl_errno($curl))
{
echo 'cURL error: ' . curl_error($curl);
}
else
{
// cURL executed successfully
print_r(curl_getinfo($curl));
}
// close cURL resource to free up system resources
curl_close($curl);
}
?>
I am trying to use PHP to get JSON data using curl, however I am getting error 302, and am not getting data returned.
I can execute the curl at the command line using:
curl -X GET --header "Accept: application/json" "https://api.lootbox.eu/pc/us/Hydropotamus-1777/profile"
The following is the PHP script that is currently not working:
<?php
// Get cURL resource
$url = 'https://api.lootbox.eu/pc/eu/Hydropotamus-1777/profile';
$curl = curl_init($url);
echo "A";
// Set some options - we are passing in a useragent too here
curl_setopt($curl, CURLOPT_TIMEOUT, 10);
curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 10);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_HTTPHEADER, array(
'Accept: application/json'
));
echo "B";
$resp = curl_exec($curl);
echo "C";
echo $resp;
// Close request to clear up some resources
curl_close($curl);
?>
Below are a few environment details that may be helpful:
Windows 10
PHP 5.6.24
Chrome browser
Well I tried the following and it worked for me, getting the response:
<?php
// Get cURL resource
$url = 'https://api.lootbox.eu/pc/eu/Hydropotamus-1777/profile';
// Initiate curl
$ch = curl_init();
curl_setopt($curl, CURLOPT_TIMEOUT, 10);
curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 10);
curl_setopt($curl, CURLOPT_HTTPHEADER, array(
'Accept: application/json'
));
// Disable SSL verification
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
// Will return the response, if false it print the response
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
// Set the url
curl_setopt($ch, CURLOPT_URL,$url);
// Execute
$result=curl_exec($ch);
// Closing
curl_close($ch);
// Will dump a beauty json :3
var_dump(json_decode($result, true));
?>
Seems you need to disable ssl verification.
A great tool to use is the RESTRequest class in PHP, https://gist.github.com/therealklanni/3440166 and then you can do:
<?php
session_start();
ini_set('display_errors', 'On');
error_reporting(E_ALL | E_STRICT);
include_once("RestRequest.php");
$url = 'https://api.lootbox.eu/pc/eu/Hydropotamus-1777/profile';
$getDataRequest = new RestRequest($dataurl, 'GET');
$getDataRequest->execute();
$data = json_decode($getDataRequest->responseBody);
echo(json_encode($data));
?>
I am working on two system.In which asterisk runs on one system-1.I want to run command in asterisk and get result back in system-2.I make curl request like below.How to get value back on system2?enter code here
exec('asterisk -rx "sip show peers"',$sip);
$POST_DATA = array(
'filename'=>$sip,
);
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL,'http://192.168.50.138/test.php');
curl_setopt($curl, CURLOPT_TIMEOUT, 30);
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, $POST_DATA);
$response = curl_exec($curl);
curl_close ($curl);
?>
Since you already have
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
in your code. curl_exec should already returns the content of the page instead of a BOOL.
This is a snippet of a library I use. As pointed out this might not be needed but it helped me out once...
//The content - if true, will not download the contents
curl_setopt($ch, CURLOPT_NOBODY, false);
Also it seems to have some bugs related to CURLOPT_NOBODY (which might explain why you have this issue):
http://osdir.com/ml/web.curl.general/2005-07/msg00073.html
http://curl.haxx.se/mail/curlphp-2008-03/0072.html
I searched the Web everywhere to find an answer to my question, but i don't find any clear and simple answer... How to send a notification to an user with PHP ? We consider that the 'user' has installed the FB App.
I tried this code :
$post_data = "access_token=".$facebook->getAccessToken()."&template=My message&href=http://google.com";
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, "https://graph.facebook.com/".$user."/notifications/");
curl_setopt($curl, CURLOPT_POSTFIELDS, $post_data);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
$page = curl_exec($curl);
curl_close($curl);
print($page);
But I receive the following error message : {"error":{"message":"(#15) This method must be called with an app access_token.","type":"OAuthException","code":15}}
I don't using the right access_token ??
Thanks to help me or show me another code... :)
Try sending notification using this format with access token as querystring
$post_data = "access_token=".$facebook->getAccessToken()."&template=My message&href=http://google.com";
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, "https://graph.facebook.com/".$user."notifications?access_token= … &template= … &href= …");
curl_setopt($curl, CURLOPT_POSTFIELDS, $post_data);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
$page = curl_exec($curl);
curl_close($curl);
print($page);