Is there any CURL Simulator for PHP? My Host does not have CURL enabled. I look for some PHP Equivalent with the same Commands as CURL have. You know such a Solution? Or i have to change the Code? How i can make a POST API-Call with a File-Upload without CURL?
I need this Equivalent:
$file_name_with_full_path = dirname(__FILE__) . "/temp/" . $sFileName;
$post = array( 'ApiKey' => '6548dcsdf'
,'File' => '#'.$file_name_with_full_path
,'WorksheetIndex' => 1
,'WorksheetActive' => 'True'
,'WorksheetName' => 'Rechnung'
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,"https://do.convertapi.com/Excel2Pdf");
curl_setopt($ch, CURLOPT_POST,1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch, CURLOPT_BINARYTRANSFER,1);
$result = curl_exec( $ch );
curl_close( $ch );
I never found the curl equivalent with same commands.
You can make your own upload function quite easily, using file_get_contents.
For example:
Upload a file using file_get_contents
Related
I'm trying to access a SOAP API with PHP and for this I need to download a copy of a WSDL description file on my computer using PHP cURL.
This is the code I'm using:
<?php
$ch = curl_init();
$credit = ('some_username'.':'.'some_password');
curl_setopt($ch, CURLOPT_URL, 'https://somevalidaddress/file.asmx?WSDL');
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($ch, CURLOPT_USERPWD, $credit);
curl_setopt($ch, CURLOPT_TIMEOUT, 30);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$data = curl_exec($ch);
curl_close($ch);
$path = 'C:\Wamp\www\mmx\test2.xml';
file_put_contents($path, $data);
?>
The file test2.xml contains, every time at line 345 (on a total number of 684 lines) a random character (e.g. ΓΈ, #, or VT, etc.). Also, the last character of the file is stripped.
I googled the thing and I read about Off-by-one errors and buffer overflow but still, I have no solution to my problem.
Any idea ? Thanks!
How about using this :
$context = stream_context_create(array(
'http' => array(
'header' => "Authorization: Basic " . base64_encode("$username:$password")
)
));
$data = file_get_contents('https://somevalidaddress/file.asmx?WSDL', false, $context);
Hi i am trying to upload using this tutorial http://ge.tt/developers/start .On step 4 they mention something like this
curl --upload-file myfile.txt http://blobs.ge.tt/a1b2c3/myfile.txt?sig=-TR2k2-3kjsh9nfmn4
What is the equivalent php code for the above line ? i tried below code , but its not working (returning bool false)
$url = $arr["posturl"];
$ch = curl_init();
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_URL, $url);
$postData = array(
'file' => '#/home/nextgen/public_html/api/myfile.txt',
);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postData);
$response = curl_exec($ch);
var_dump($response);
note : i got correct $url and myfile.txt exists and i tried replacing 'file' => '#/home/nextgen/public_html/api/myfile.txt' with '#myfile.txt' ..nothing seems to work.
'#myfile.txt' must be '#/full/path/to/myfile.txt'
curl_setopt($ch, CURLOPT_POST,1); is required
If you'd used curl's --libcurl option you would've seen the difference. Your command line does PUT, your PHP version does multipart formpost...
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
I'm having trouble with a REST POST request after the API to which I'm posting published the final release of their API. It was working without incident, and I've been told that with the new version the server is more strict regarding the type being 'application/json'. The following cli curl command works swimmingly:
cat json.txt|curl -v -k -u user:password -F 'exchangeInstance=#-;type=application/json' https://my.url.here
However, I need to execute this in code. Using the php curl libraries I've got a simple test script up that looks like this:
$post = array(
"exchangeInstance" => $json_string,
"type" => "application/json",
);
$url = 'myurlhere';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($ch, CURLOPT_USERPWD, "user:pass");
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
$result = curl_exec($ch);
$info = curl_getinfo($ch);
var_dump($post);
var_dump($result);
echo $result;
var_dump($info);
As I read the documentation, the Content-type in the header should automatically be set to 'multipart/form' if I pass an array as CURLOPT_POSTFIELDS, and then I'm setting the type for the element pass to 'application/json' in the array.
However, the api has had no POST requests from me. And I'm getting an error from them that clearly indicates that they are receiving a GET request. How can this possibly be? What am I missing?
curl -F !== -d
$post = array(
"exchangeInstance" => sprintf('#%s;type=application/json', $json_string),
);
When try to create a new API request with the Windows Azure new Bing based API, Using the code below
$url= 'https://'.$this->m_host.'/Web?Query={keyword}&Adult=%27Off%27&$top=50&$format=Atom';
$url=str_replace('{keyword}', urlencode($this->m_keywords), $url);
// Replace this value with your account key
$accountKey = $this->key;
$WebSearchURL = $url;
$context = stream_context_create(array(
'http' => array(
'proxy' => 'tcp://127.0.0.1:8888',
'request_fulluri' => true,
'header' => "Authorization: Basic " . base64_encode($accountKey . ":" . $accountKey)
)
));
$request = $WebSearchURL;
$response = file_get_contents($request, 0, $context);
print_r($response);
i get following error.
Warning: file_get_contents() [function.file-get-contents]:
Couldn't connect to server in /home/xxxxx on line 43
Warning: file_get_contents(https://api.datamarket.azure.com/
failed to open stream: operation failed in /home/xxxx/ bing_search.php on line 43
Any idea why this fails ? or is it best to use the CURL Library than the file_get_contents() ?
The below code works for me, it is to search news but it will work for web searches too.
Just replace appkey with your one, leave username as it is (i.e. username) since it is ignored by the server
function getBingResult($keyword)
{
$credentials = "username:appkey";
$url= "https://api.datamarket.azure.com/Data.ashx/Bing/Search/v1/News?Query=%27{keyword}%27". "&\$format=json";
$url=str_replace('{keyword}', urlencode($keyword), $url);
$ch = curl_init();
$headers = array(
"Authorization: Basic " . base64_encode($credentials)
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT,5);
curl_setopt($ch, CURLOPT_FAILONERROR, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_AUTOREFERER, true);
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($session, CURLOPT_VERBOSE, TRUE);
$rs = curl_exec($ch);
$jsonobj = json_decode($rs);
curl_close($ch);
return $jsonobj;
}
Testing the function:
$bingResult = getBingResult("John");
foreach($bingResult->d->results as $value)
{
echo '<pre>'."URL:". $value->Url.'</pre>';
echo '<pre>'."Title:". $value->Title.'</pre>';
echo '<pre>'."Description:". $value->Description.'</pre>';
echo '<pre>'."Source:". $value->Source.'</pre>';
echo '<pre>'."Date:". $value->Date.'</pre>';
}
Either file_get_contents or CURL will work for the Bing API, you can use what will work on your system and what you are comfortable with.
First I would check your server can connect to the Windows Azure server. Try running a ping and then a wget from the command line to see if it can. Do you go through a proxy? You'll need to set those details in your stream context.
I'm not sure what you have $this->m_host set to, but the new Bing API should be at either:
https://api.datamarket.azure.com/Bing/Search/ or https://api.datamarket.azure.com/Bing/SearchWeb/. The URL https://api.datamarket.azure.com/Web comes back as invalid.
Here is working example of Search API just replace your access key with "XXXX". Even i wasted quite a few hours to get it work using cURL but it was failing cause of "CURLOPT_SSL_VERIFYPEER" on local :(
$process = curl_init('https://api.datamarket.azure.com/Bing/Search/Web?Query=%27xbox%27');
curl_setopt($process, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($process, CURLOPT_USERPWD, "username:XXXX");
curl_setopt($process, CURLOPT_TIMEOUT, 30);
curl_setopt($process, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($process, CURLOPT_SSL_VERIFYPEER, false);
$response = curl_exec($process);
# Deliver
return $response;
# Have a great day!
curl_close($process);
1.) You do not need str_replace(). Use the var directly inside the url:
$url= 'https://'.$this->m_host.'/Web?Query='.urlencode($this->m_keywords).'&Adult=%27Off%27&$top=50&$format=Atom';
2.) You defined three different vars with the same value:
$WebSearchURL = $url;
$request = $WebSearchURL;
Use $url only.
3.) base64_encode($accountKey . ":" . $accountKey) can be reduced to base64_encode(":" . $accountKey)
4.) Add Accept-Encoding: gzip to your header to reduce traffic and raise speed.
5.) Your problem should be this line:
'proxy' => 'tcp://127.0.0.1:8888',
Remove it or change it to the correct value.