Google CSE PHP CURL Implementation to programmatically add/delete annotations - php

I have been working with Googgle's Custom Search Engine API to programmatically add and delete annotations. I feel like I have followed their documentation and others tips very closely. I can't seem avoiding a 'bad request' HTTP 400 error. My code is below
$url = "http://www.google.com/cse/api/default/annotations/";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
curl_setopt ($ch, CURLOPT_HTTPHEADER, Array(
"Authorization: GoogleLogin auth='DQAAAMI......n9dGUyA'",
"Content-Type: text/xml"
));
curl_setopt($ch, CURLOPT_POST, 1);
$data = '<?xml version="1.0"?>'.chr(10);
$data .= '<Batch>'.chr(10);
$data .= chr(9).'<Add>'.chr(10);
$data .= chr(9). chr(9).'<Annotations>'.chr(10);
$data .= chr(9). chr(9). chr(9).'<Annotation about=\"\">'.chr(10);
$data .= chr(9).chr(9). chr(9). chr(9).'<Label name=\"my_engine\"/>'.chr(10);
$data .= chr(9). chr(9). chr(9).'</Annotation>'.chr(10);
$data .= chr(9). chr(9).'</Annotations>'.chr(10);
$data .= chr(9).'</Add>'.chr(10);
$data .= '</Batch>'.chr(10);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
$result = curl_exec($ch);
if ( curl_errno($ch) ) {
$result = 'cURL ERROR -> ' . curl_errno($ch) . ': ' . curl_error($ch);
} else {
$returnCode = (int)curl_getinfo($ch, CURLINFO_HTTP_CODE);
echo $returnCode."<br/>";
switch($returnCode){
case 200:
break;
default:
$result = 'HTTP ERROR -> ' . $returnCode;
break;
}
}
curl_close($ch);
echo $result;
if anybody can help me or at least steer me in the right direction I would be very grateful

Ok, firstly, you'll need to encode the xml data thats sent to google. Secondly you should get rid of all the tabs and formatting, the receiving engine is not concerned with that.
So try this:
$data = '<?xml version="1.0"?><Batch><Add><Annotations><Annotation about=\"\"><Label name=\"my_engine\"/></Annotation></Annotations></Add></Batch>';
$data = urlencode($data); // encode the string to send via http
I think that should get you going. You may find other errors but this should clear the 400 error you are getting.

Related

Problem to shorten URL using PHP-Bitly v4

I'm trying to shorten URL using the bit.ly API V4, but i always get the same error, I can't get the short URL.
Can someone help me please?
Here is the code that Im using:
$long_url = 'https://estaticos.muyinteresante.es/media/cache/1140x_thumb/uploads/images/gallery/59bbb29c5bafe878503c9872/husky-siberiano-bosque.jpg';
$apiv4 = 'https://api-ssl.bitly.com/v4/bitlinks';
$genericAccessToken = 'MyToken';
$data = array(
'long_url' => $long_url
);
$payload = json_encode($data);
$header = array(
'Authorization: Bearer ' . $genericAccessToken,
'Content-Type: application/json',
'Content-Length: ' . strlen($payload)
);
$ch = curl_init($apiv4);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, $payload);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
$result = curl_exec($ch);
$resultToJson = json_decode($result);
if (isset($resultToJson->link)) {
echo $resultToJson->link;
}
else {
echo 'Not found';
}
I always get "Not found", anybody knows why?
I have a very detailed answer on integrating with Bit.ly V4 and how to shorten URLs.
You can find the link here.

file_get_content return null for some url

why some url with json content return null on php function get page content?
i used these ways :
file_get_content
curl
http_get
but return null . but when page open with browser json content show on browser ?? anyone can help me?
$url = 'https://pardakht.cafebazaar.ir/devapi/v2/api/validate/com.pdgroup.insta/inapp/coin_follow_1/purchases/324234235/?access_token=9IaRpdmQzFppJMabLHbHyzBaQnm8bM';
$result = file_get_content($url);
return null but in browser show
{"error_description": "Access token has expired.", "error": "invalid_credentials"}
It returns the following:
failed to open stream: HTTP request failed! HTTP/1.1 401 UNAUTHORIZED
So, you have to be authorized.
You ca play with this code snippet (I can't test since token expired again):
<?php
$access_token = "s9spZax2Xrj5g5bFZMJZShbMrEVjIo"; // use your actual token
$url = "https://pardakht.cafebazaar.ir/devapi/v2/api/validate/com.pdgroup.insta/inapp/coin_follow_1/purchases/324234235/";
// Solution with file_get_contents
// $content = file_get_contents($url . "?access_token=" . $access_token);
// echo $content;
// Solution with CURL
$headers = array(
"Accept: application/json",
"Content-Type: application/json",
"Authorization: Basic " . $access_token
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_VERBOSE, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_URL, $url); // . $params - if you need some params
curl_setopt($ch, CURLOPT_POST, false);
// curl_setopt($ch, CURLOPT_USERPWD, $userPwd);
$result = curl_exec($ch);
$ch_error = curl_error($ch);
$info = curl_getinfo($ch);
curl_close($ch);
if ($ch_error) {
throw new Exception("cURL Error: " . $ch_error);
}
if ($info["http_code"] !== 200) {
throw new Exception("HTTP/1.1 " . $info["http_code"]);
}
echo $result;

Use Post Method to send XML Parameter

I wonder if somebody could help me out with this - I have been trying for a few hours and no joy. When this is submitted I get an error message that says "Use POST method to send XML paramater". I thought I was doing this but obviously not. Any pointers are much appreciated. Obviously the Username, Password and URL are different on the live version
define('XML_PAYLOAD', '<?xml version=\"1.0\" encoding=\"UTF-8\"?><Request><Head><Username>myusername</Username><Password>mypassword</Password><RequestType>HotelSearch</RequestType></Head><Body><HotelIds><HotelId>2234836</HotelId></HotelIds><CheckInDate>2017-05-01</CheckInDate><CheckOutDate>2017-05-08</CheckOutDate><Rooms><Room><NumAdults>2</NumAdults></Room></Rooms><Nationality>GB</Nationality><Currency>GBP</Currency><AvailableOnly>0</AvailableOnly></Body></Request>');
define('XML_POST_URL', 'http://theurl');
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, XML_POST_URL);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 4);
curl_setopt($ch, CURLOPT_POSTFIELDS, XML_PAYLOAD);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Connection: close'));
$start = array_sum(explode(' ', microtime()));
$result = curl_exec($ch);
$stop = array_sum(explode(' ', microtime()));
$totalTime = $stop - $start;
if ( curl_errno($ch) ) {
$result = 'ERROR -> ' . curl_errno($ch) . ': ' . curl_error($ch);
} else {
$returnCode = (int)curl_getinfo($ch, CURLINFO_HTTP_CODE);
switch($returnCode){
case 404:
$result = 'ERROR -> 404 Not Found';
break;
default:
break;
}
}
curl_close($ch);
echo 'Total time for request: ' . $totalTime . "\n";
echo $result;
exit(0);
Add content type with your request header:
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-type: application/xml'));
Or may be text/xml or whatever the server is accepting.

Querying API through Curl/PHP

I'm looking at the Parse.com REST API and making calls using the Curl wrapper PHP uses.
Raw Curl code(works):
curl -X GET \
-H "X-Parse-Application-Id: myApplicationID" \
-H "X-Parse-REST-API-Key: myRestAPIKey" \
https://api.parse.com/1/classes/Steps
PhP code(works):
$ch = curl_init('https://api.parse.com/1/classes/Steps');
curl_setopt($ch,CURLOPT_HTTPHEADER,array('X-Parse-Application-Id: myApplicationID',
'X-Parse-REST-API-Key: myRestAPIKey',
'Content-Type: application/json'));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_exec($ch);
curl_close($ch);
Thats good and dandy, but now when I try to add a query constraint:
Raw Curl code(works):
curl -X GET \
-H "X-Parse-Application-Id: myApplicationID" \
-H "X-Parse-REST-API-Key: myRestAPIKey" \
-G \
--data-urlencode 'where={"steps":9243}' \
https://api.parse.com/1/classes/Steps
Alas, we ultimately arrive at my question- What is the php analogue to the above code?
PHP code(does not work):
$ch = curl_init('https://api.parse.com/1/classes/Steps');
$query = urlencode('where={"steps":9243}');
curl_setopt($ch,CURLOPT_HTTPHEADER,array('X-Parse-Application-Id: myApplicationID',
'X-Parse-REST-API-Key: myRestAPIKey',
'Content-Type: application/json'));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $query);
curl_exec($ch);
curl_close($ch);
Error response:
Object ( [code] => 107 [error] => invalid json: where%3D%7B%22steps%22%3A9243%7D )
Your last PHP example has changed the request to a POST from a GET. Pass your parameters in the query string instead of the POST body. Try:
$query = urlencode('where={"steps":9243}');
$ch = curl_init('https://api.parse.com/1/classes/Steps?'.$query);
curl_setopt(
$ch,
CURLOPT_HTTPHEADER,
array(
'X-Parse-Application-Id: myApplicationID',
'X-Parse-REST-API-Key: myRestAPIKey',
'Content-Type: application/json'
)
);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_exec($ch);
curl_close($ch);
To call GET,POST,DELETE,PUT All kind of request, i have created one common function
define("SITEURL", "http://localhost:82/slimdemo/RESTAPI");
function CallAPI($method, $api, $data, $headers) {
$url = SITEURL . "/" . $api;
$curl = curl_init($url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
switch ($method) {
case "GET":
curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode($data));
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "GET");
break;
case "POST":
curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode($data));
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "POST");
break;
case "PUT":
curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode($data));
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "PUT");
break;
case "DELETE":
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "DELETE");
curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode($data));
break;
}
$response = curl_exec($curl);
$data = json_decode($response);
/* Check for 404 (file not found). */
$httpCode = curl_getinfo($curl, CURLINFO_HTTP_CODE);
// Check the HTTP Status code
switch ($httpCode) {
case 200:
$error_status = "200: Success";
return ($data);
break;
case 404:
$error_status = "404: API Not found";
break;
case 500:
$error_status = "500: servers replied with an error.";
break;
case 502:
$error_status = "502: servers may be down or being upgraded. Hopefully they'll be OK soon!";
break;
case 503:
$error_status = "503: service unavailable. Hopefully they'll be OK soon!";
break;
default:
$error_status = "Undocumented error: " . $httpCode . " : " . curl_error($curl);
break;
}
curl_close($curl);
echo $error_status;
die;
}
CALL DeleteAPI
$data = array('id'=>$_GET['did']);
$header = array('USERTOKEN:' . GenerateToken());
$result = CallAPI('DELETE', "DeleteCategory", $data, $header);
CALL PostAPI
$data = array('title'=>$_POST['txtcategory'],'description'=>$_POST['txtdesc']);
$header = array('USERTOKEN:' . GenerateToken());
$result = CallAPI('POST', "InsertCategory", $data, $header);
CALL GetAPI
$data = array('id'=>$_GET['eid']);
$header = array('USERTOKEN:' . GenerateToken());
$result = CallAPI('GET', "GetCategoryById", $data, $header);
CALL PutAPI
$data = array('id'=>$_REQUEST['eid'],m'title'=>$_REQUEST['txtcategory'],'description'=>$_REQUEST['txtdesc']);
$header = array('USERTOKEN:' . GenerateToken());
$result = CallAPI('POST', "UpdateCategory", $data, $header);
This line:
curl_setopt($ch, CURLOPT_POSTFIELDS, $query);
is trying to set a request body, which is not valid for a GET request. cURL appears to let you set a body on a GET request (example).
It looks like your PHP is not making a POST request (at least best I can tell from looking at other PHP examples that use curl_setopt($ch,CURLOPT_POST, count($fields));. I believe you need to pass an array to the postfields option:
$fields = array(
'where' => urlencode('{"steps":9243}')
);
curl_setopt($ch, CURLOPT_POST, count($fields));
curl_setopt($ch, CURLOPT_POSTFIELDS, $fields);
Try this:
$query = json_encode(
array(
'where' => array( 'steps' => 9243 )
)
);
I've gleaned this from here - not tested though! The Python example appears to JSON-encode the query before sending it, so it might be worth trying that.

C2DM Server with PHP 401 Error

I'm building my own C2DM application right now. I first started with a small Android application to test the push feature. And it works if I just call the curl command with the correct settings in my shell.
Now for the server part I wanted to use PHP but as it seems I'm doing something wrong as I always get a 401 error message when I try to send a message to the client. First of all the code consists of two parts. The first curl request asks for the server token. This works I get a real response from google with a working token!
The second curl request ends up with a 401 error message. Any ideas what I'm doing wrong?
$post_params = array ( "Email" => $MY_GOOGLE_ACC, "Passwd" => $MY_GOOGLE_PWD, "accountType"=>"GOOGLE", "source=" . $MY_GOOGLE_SRC, "service=ac2dm" );
$first = true;
$data_msg = "";
foreach ($post_params as $key => $value) {
if ($first)
$first = false;
else
$data_msg .= "&";
$data_msg .= urlencode($key) ."=". urlencode($value);
}
$x = curl_init("https://www.google.com/accounts/ClientLogin");
curl_setopt($x, CURLOPT_HEADER, 1);
curl_setopt($x, CURLOPT_POST, 1);
curl_setopt($x, CURLOPT_POSTFIELDS, $data_msg);
curl_setopt($x, CURLOPT_RETURNTRANSFER, 1);
$data = curl_exec($x);
curl_close($x);
$response = $data;
$authKey = trim(substr($response, 4+strpos($response, "SID=")));
echo $authKey;
$collapse_key = 'something';
$post_params = array ( "registration_id" => $DEVICE_TOKEN, "collapse_key" => $collapse_key, "data.payload"=>"cakephp" );
$first = true;
$data_msg = "";
foreach ($post_params as $key => $value) {
if ($first)
$first = false;
else
$data_msg .= "&";
$data_msg .= urlencode($key) ."=". urlencode($value);
}
$size=strlen($data_msg);
$x = curl_init("https://android.apis.google.com/c2dm/send");
curl_setopt($x, CURLOPT_HTTPHEADER, array('Content-Type: application/x-www-form-urlencoded', 'Content-Length:'. $size, 'Authorization: GoogleLogin auth=' . $authKey));
curl_setopt($x, CURLOPT_HEADER, 1);
curl_setopt($x, CURLOPT_POST, 1);
curl_setopt($x, CURLOPT_POSTFIELDS, $data_msg);
curl_setopt($x, CURLOPT_RETURNTRANSFER, 1);
$data = curl_exec($x);
curl_close($x);
$response = $data;
Example of key'd array use with curl. This is pretty much the exact code I have working (with minor changes for clarity).
$headers = array('Authorization: GoogleLogin auth=' . $authcode);
$data = array(
'registration_id' => $device_registration_id,
'collapse_key' => 'ck_' . $device_id,
'data.arg' => 'arrrrghhh'
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://android.apis.google.com/c2dm/send");
if($headers) curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_exec($ch);
$authcode is the SID returned by ClientLogin. $device_registration_id is the registration ID that the client app on the phone gave us when we did the C2DM_REGISTER.
Hope that helps.
Try to register your app here. I had the same problem.
I'm not totally sure what's going on here, but here's a few difference I noticed from my application server that works fine.
In your curl_setopt call for CURLOPT_HTTPHEADER there is no space between the content length and the size. This 'shouldn't cause a problem, but I've seen webservers get tempremental over stupid stuff like that before.
Also, CURLOPT_POSTFIELDS is a string whereas I send mine as a key'd array.
Other than that I looks the same as my working code.

Categories