Differences in using PHP Curl vs command line curl - php

I am integrating the Badgeville REST API with my PHP 5.3, curl 7.22 application.
The API documentation for BV all uses command line curl calls for their examples. When I run these examples they work fine.
When I attempt to do the same thing with the PHP Curl class I always get a 500 error from the BV server.
I have tried to do the synonomous functionality with the Advanced Rest Client extension in Chrome.
PHP Curl Example:
$ch = curl_init('http://sandbox.v2.badgeville.com/api/berlin/[private_api_key]/users.json');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 3);
if($this->getRequestType() == 'POST')
{
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS,
array(
'user[name]' => 'Generic+Username',
'user[email]' => 'johndoe%40domainname.com'
);
);
}
$response = curl_exec($ch);
Rest Client Example:
URL:
http://sandbox.v2.badgeville.com/api/berlin/[private_api_key]/users.json
POST
No Headers Payload:
user[name]=Generic+Username&user[email]=johndoe%40domainname.com
I have manually created the command line curl call and ran that with shell_exec(), but I would REALLY prefer not having to do that.
In my research I found a Drupal module and all the API calls are done through fsockopen() calls.
Is there some way to do successfully make Badgeville calls using the PHP Curl class?

As it turns out Badgeville has a 500 error when a curl request comes in that has headers set.
Error returning code:
$ch = curl_init('http://sandbox.v2.badgeville.com/api/berlin/[private_api_key]/users.json');
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 3);
if($this->getRequestType() == 'POST')
{
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS,
array(
'user[name]' => 'Generic+Username',
'user[email]' => 'johndoe%40domainname.com'
);
);
}
$response = curl_exec($ch);
Properly functioning code:
$ch = curl_init('http://sandbox.v2.badgeville.com/api/berlin/[private_api_key]/users.json');
//curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 3);
if($this->getRequestType() == 'POST')
{
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS,
array(
'user[name]' => 'Generic+Username',
'user[email]' => 'johndoe%40domainname.com'
);
);
}
$response = curl_exec($ch);
SMH

Related

How to send multiple images into REST API

I'm taking multiple documents of candidate from database, put it into one array and sending this array and some other additional information to the REST API. Then it's throwing an exception FILE Type NOT Support. When sending single image it works properly.
foreach($a_files as $a_file)
{
$file_name_with_full_path=$a_file->uploaded_path.$a_file->document_name;
$mime_type=mime_content_type($file_name_with_full_path);
$cFile[] = '#'.realpath($file_name_with_full_path).';type='.$mime_type.';';
}
Sending this information through REST API. My post data is like:
$post= array('level' => "level",
'nameOfInstitute' => "nameOfInstitute",
'nameAsPerDocument' => "nameAsPerDocument",
'issueDate' => "issueDate",
'registrationNumber' => "registrationNumber",
'yearOfPassing' =>"yearofpassing",
'degree' =>"degree",
'fieldOfStudy' =>"fieldofstudy",
'grade' =>"grade",
'nameOfBoardUniversity'=>"university",
'file' =>$cFile
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: multipart/form-data'));
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_SAFE_UPLOAD, false);
curl_setopt($ch, CURLOPT_USERPWD, $val);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_URL,$target_url);
curl_setopt($ch, CURLOPT_POST,1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
$response = curl_exec ($ch);
$err = curl_error($ch);
curl_close($ch);
It's throwing an exception file type not supported. But when sending single file, it's working fine (without loop). What is the problem in above code when sending multiple files in the API. Please help me out from this problem.

CURL php in Google developers Console

I'm using Google Developers Console to build a bot for telegram.
everything works fine, except that when I try to send files (photo, audio, etc ..) php returns a fatal error for the lack of libraries Curl.
I checked and this is the php version 5.5.26, and also supports the new function CURLFile.
How can I fix?
I leave you the error that returns the console
1 - Method
function sendIMG($api_url,$path,$chatID)
{
$url = $api_url."sendPhoto?chat_id=".$chatID ;
$post_fields = array(
'chat_id' => $chatID,
'photo' => new \CURLFile(realpath($path))
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type:multipart/form-data"));
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_fields);
$output = curl_exec($ch);
sendText($api_url, $chatID, $output);
}
PHP Fatal error: Class 'CURLFile' not found in /base/data/home/apps/s~macabreobot/1.389444928540417808/main.php on line 18
2 Method
function sendIMG_2($api_url,$path,$chatID)
{
$api_url = "https://api.telegram.org/botYOUR_BOT_TOKEN/";
$url = $api_url."sendPhoto?chat_id=".$chatID;
$ch = curl_init();
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type:multipart/form-data"));
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, array("photo" => "#".$path));
curl_setopt($ch, CURLOPT_INFILESIZE, filesize($path));
$output = curl_exec($ch);
sendText($api_url, $chatID, $output);
}
PHP Fatal error: Call to undefined function curl_init() in /base/data/home/apps/s~macabreobot/1.389445039709239473/main.php on line 36
There are some configuration steps needed to use curl_ functions on App Engine PHP, see Terrence Ryan's blog post on the subject for more details.

Server Side script for cURL request

I use cURL but untill now I used it for requesting data from servers. But now I want ot write API and data will be requested with cURL. But I don't know how Server reads data from cURL request.
This is my "client server" side request:
function sendRequest($site_name,$send_xml,$header_type=array('Content-Type: text/xml'))
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$site_name);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS,$send_xml);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST,1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER,0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch, CURLOPT_HTTPHEADER,$header_type);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 120);
$result = curl_exec($ch);
return $result;
}
$xml = "<request>
<session>
<user>exampleuser</user>
<pass>examplepass</pass>
</session>
</request>";
$sendreq = sendRequest("http://sitename.com/example.php",$xml);
echo $sendreq;
How do I need to write "main server" side script so I can read what user and pass from request are???
Thank you a lot.
To just be able to read it try this
curl_setopt($ch, CURLOPT_POSTFIELDS,array('data'=>$send_xml));
Then
print_r($_POST['data'])
Alternatively skip the XML and try something like this:
$data = array(
'request' => array(
'session' => array(
'user'=>'exampleuser',
'pass'=>'examplepass')
)
);
$sendreq = sendRequest("http://sitename.com/example.php",$data);
In example.php
print_r($_POST)

Diigo API -- HTTP Basic Authentication Error over CURL in PHP for HTTPS

Hi I am trying to post data Using CURL in PHP to diigo bookmarking, I have tried through API, When i executing file i got HTTP basic authentication here is my code
require_once('libs/diigo.class.php');
$diggo = new DiigoAPI("username","password");
$book = $diggo->getBookmarks();
$diggo->saveBookmarks("http://www.example.com");
public function saveBookmarks($url)
{
$attachment = array ("url" => $url, "title" => "SEnthil" , "shared" => "yes" );
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,'https://secure.diigo.com/api/v2/bookmarks');
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $attachment);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); //to suppress the curl output
$result = curl_exec($ch);
echo $result;
curl_close ($ch);
}
Your curl doesn't have basic HTTP authentication set. You should set it up this way:
curl_setopt($curl, CURLOPT_USERPWD, $user_here . ":" . $password_here );
And the way you're doing it now the saveBookmarks function doesn't require the Diigo Class at all.

Telegram Api - Creating an Authorization Key 404 error - PHP

I am trying to write a simple program in PHP to use telegram api, (not bot api, main messaging api). when i run my script, result is 404 error. Why?
<?php
$postfields = array();
$postfields['phone_number'] ='+1234567890';
$postfields['sms_type'] ='0';
$postfields['api_id'] ='12345';
$postfields['api_hash'] ='abcdefghijklmnopqrstuvwxyz';
$postfields['lang_code'] ='en';
$url = '149.154.167.40:443';
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postfields );
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
echo $html=curl_exec($ch);
curl_close($ch);
?>
tanks a lot.

Categories