When I use your curl method of YouTube API (v3)
$api_key = 'YOUR_API_KEY';
$url = 'https://www.googleapis.com/youtube/v3/channels?part=snippet,statistics&key=' . $api_key . '&forUsername=' . $_GET['username'];
// initializes the request
$curl = curl_init();
// sets the url
curl_setopt($curl, CURLOPT_URL, $url);
// enables that curl_exec() returns content instead of status code
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
// allows redirects
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1);
// checks if a secure site has been requested
if(preg_match('/^https:\/\//', $url)){
// if so, verification is disabled
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
}
// performs the actual request
$data = curl_exec($curl);
// destructs the request
curl_close($curl);
// this line converts the json string which is returned into a php object
$data = json_decode($data);
print_r($data);
It shows
Array (
[error] => Array (
[errors] => Array (
[0] => Array (
[domain] => youtube.parameter
[reason] => missingRequiredParameter
[message] => No filter selected.
[locationType] => parameter
[location] =>
)
)
[code] => 400
[message] => No filter selected.
)
)
Why this error is coming up?
Related
How to achieve an image upload via cURL?
I am using a sandbox api for convert pdf file to .txt file. If i use "https://s3.amazonaws.com/sample.pdf" path of pdf for $sourceFile variable then its working fine. But if i use system path "test.pdf" then it's not working.
I was trying to through curl_file_create, but it gives the following error:
Recoverable fatal error: Object of class CURLFile could not be
converted to string in line.
I am using PHP Version 7.3.0
<?php
error_reporting(E_ALL);
$endpoint = "https://api.xxx.com/v1/jobs";
$apiKey = "GiVU******FR48H";
$sourceFile = "https://s3.amazonaws.com/sample.pdf";//new CurlFile("test.pdf");
$targetFormat = "txt";
$postData = array(
"source_file" => $sourceFile,
"target_format" => $targetFormat
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $endpoint);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
curl_setopt($ch, CURLOPT_SAFE_UPLOAD, false);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postData);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_USERPWD, $apiKey . ":");
$body = curl_exec($ch);
curl_close($ch);
$response = json_decode($body, true);
echo "Response:\n---------\n";
echo"<pre>";
print_r($response);
?>
Response: ---------
Array
(
[id] => 8442473
[key] => GiVU******FR48H
[status] => initialising
[sandbox] =>
[created_at] => 2019-11-12T10:35:01Z
[finished_at] =>
[import] => Array
(
[id] => 671784
[url] => https://s3.amazonaws.com/sample.pdf
[status] => initialising
)
[target_files] => Array
(
)
[target_format] => txt
[credit_cost] => 0
)
Above example give me output as fine as well. If i replace amazonaws from system path then it doesn't gives any response.
CURLOPT_SAFE_UPLOAD is always set to false and curl expects files to be passed as string (#/path/to/file) because of it. In your case the code sends files as objects because it's using curl_file_create() when available.
When CURLOPT_SAFE_UPLOAD is set to false, you should not use curl_file_create() (it's flag for backwards compatibility).
Also, try to set CURLOPT_POSTFIELDS after CURLOPT_SAFE_UPLOAD (see here why).
PHP 5.x is not supported anymore and I suggest to get rid of the old syntax (#/path/to/file and CURLOPT_SAFE_UPLOAD = false) and always use curl_file_create().
I have a problem calling Google urlshortener. I use curl function to make a call:
$url = base_url()."home/register?source=#".$userid;
$longUrl = $url;
$apiKey = 'xxxxxxxxapikeyxxxxxxx';
$postData = array('longUrl' => $longUrl, 'key' => $apiKey);
$jsonData = json_encode($postData);
$curlObj = curl_init();
curl_setopt($curlObj, CURLOPT_URL,
'https://www.googleapis.com/urlshortener/v1/url?key='.$apiKey);
curl_setopt($curlObj, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curlObj, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($curlObj, CURLOPT_HEADER, 0);
curl_setopt($curlObj, CURLOPT_HTTPHEADER, array('Content-
type:application/json'));
curl_setopt($curlObj, CURLOPT_POST, 1);
curl_setopt($curlObj, CURLOPT_POSTFIELDS, $jsonData);
$response = curl_exec($curlObj);
$json = json_decode($response);
curl_close($curlObj);
print_r($json);
So I'm getting the following response:
stdClass Object ( [error] => stdClass Object ( [errors] => Array ( [0] =>
stdClass Object ( [domain] => global [reason] => forbidden [message] =>
Forbidden ) ) [code] => 403 [message] => Forbidden ) )
Please help me.
You can no longer use the Google URL Shortener API using an api key.
Starting March 30, 2018, we will be turning down support for goo.gl URL shortener. Please see this blog post for detailed timelines and alternatives.
You should be aware that the Google URL Shortener API has been discontinued Transitioning Google URL Shortener to Firebase Dynamic Links
They have already begun tuning down a number of the features within the API. I suspect the issue you are having is due to that.
You should switch to FireBase Dynamic links
I am working in Zoho CRM. I am not able to understand how to deal with special characters when I have to search records.
Here is the code that i am trying.
<?php
$product_name = "99X210MM DL - SINGLE SUPPLY & ATTACHMENT"; // not working with this
$product_name1 = "99X210MM DL SINGLE SUPPLY TTACHMENT"; // working with this
$auth = "------------------------"; //can't share
$url ="https://crm.zoho.com/crm/private/json/Products/searchRecords";
$query ="authtoken=".$auth."&scope=crmapi&criteria=(Product Name:".$product_name.")";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 30);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $query);
$response = curl_exec($ch);
curl_close($ch);
$rslt = json_decode($response);
echo "<pre>"; print_r($rslt);
?>
Result :
stdClass Object
(
[response] => stdClass Object
(
[uri] => /crm/private/json/Products/searchRecords
[error] => stdClass Object
(
[code] => 4832
[message] => API call cannot be completed as the Criteria parameter value is invalid
)
)
)
Consider using urlencode to encode your special characters.
urlencode($product_name)
I have URL shortening script the was working well until I added website referrers to KEY restriction in API console (which i had to do). Now i dosen't return short url. I get following error:
Array ( [error] => Array ( [errors] => Array ( [0] => Array ( [domain]
=> usageLimits [reason] => ipRefererBlocked [message] => The request did not specify any referer. Please ensure that the client is sending
referer or use the API Console to remove the referer restrictions.
[extendedHelp] =>
https://console.developers.google.com/apis/credentials?project=XXXXXXXXX
) ) [code] => 403 [message] => The request did not specify any
referer. Please ensure that the client is sending referer or use the
API Console to remove the referer restrictions. ) )
My PHP:
<?php
$longurl = "http://example.com";
$api_key_google = "XXXX_API_KEY_XXXXX";
$curl = curl_init('https://www.googleapis.com/urlshortener/v1/url?key='.$api_key_google);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($curl, CURLOPT_HEADER, 0);
curl_setopt($curl, CURLOPT_HTTPHEADER, array('Content-type:application/json'));
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode(array('longUrl' => $longurl)));
$return = json_decode(curl_exec($curl), true);
curl_close($curl);
print_r($return);
echo $shortDWName = $return['id'];
?>
What Am I missing here? Thanks for your help in advance.
Try to add this line
curl_setopt($curl, CURLOPT_HEADER, 0);
// should add this line
curl_setopt($ch, CURLOPT_REFERER, '[your restriction domain]');
For some reason my script stopped working today. When I look in the API control panel says I still have 100% left of usage. Any ideas? Did they change the auth way?
function url_small($url)
{
//This is the URL you want to shorten
$longUrl = $url;
$apiKey = '#####HIDDEN######';
//Get API key from : http://code.google.com/apis/console/
$postData = array('longUrl' => $longUrl, 'key' => $apiKey);
$jsonData = json_encode($postData);
$curlObj = curl_init();
curl_setopt($curlObj, CURLOPT_URL, 'https://www.googleapis.com/urlshortener/v1/url');
curl_setopt($curlObj, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curlObj, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($curlObj, CURLOPT_HEADER, 0);
curl_setopt($curlObj, CURLOPT_HTTPHEADER, array('Content-type:application/json'));
curl_setopt($curlObj, CURLOPT_POST, 1);
curl_setopt($curlObj, CURLOPT_POSTFIELDS, $jsonData);
$response = curl_exec($curlObj);
//change the response json string to object
$json = json_decode($response);
curl_close($curlObj);
return $json->id;
}
Response
stdClass Object
(
[error] => stdClass Object
(
[errors] => Array
(
[0] => stdClass Object
(
[domain] => usageLimits
[reason] => dailyLimitExceededUnreg
[message] => Daily Limit for Unauthenticated Use Exceeded. Continued use requires signup.
[extendedHelp] => https://code.google.com/apis/console
)
)
[code] => 403
[message] => Daily Limit for Unauthenticated Use Exceeded. Continued use requires signup.
)
)
My below answer is no longer valid. Google now makes you use Firebase for URL Shortening. Easy to setup.
https://firebase.google.com/docs/dynamic-links/rest
So it turns out this old function that is displayed in multiple websites now needs the api key to be displayed in the URL section too for google to register the request to your account.
curl_setopt($curlObj, CURLOPT_URL, 'https://www.googleapis.com/urlshortener/v1/url');
switched to this
curl_setopt($curlObj, CURLOPT_URL, 'https://www.googleapis.com/urlshortener/v1/url?key='.$apiKey);