PHP Curl Not working - php

I am new here and not a pro in php but need a small help. Actually I was given a code by my service prodvider to upload on my hosting. But when i uploaded that code on my hosting account it didnt work well and after contact the support I was told to use php curl code in some part to make it run. can anyone please help me with the issue.
Original Code:
<?php
$MobileNumber=substr($_REQUEST['VerifiedNumber'],-10);
$APIKey="<Your Dial2verify API Key Here>";
$SenderID="<6 ALPHABETIC CHARACTERS ONLY>";
$Message="<Your SMS Text Here>";
echo `curl -XPOST "http://host/SMS/SEND/$APIKey/$SenderID/$MobileNumber" -d "Msg=$Message"`;
?>
I don't know how to make the above code into curl code to make it work.
Thanks :)

The reason it did not work was that you where trying to call the command line version of cURL (and also doing the call wrong).
The best way is to use the php cURL module to make this call. Verify first that it is installed by creating an info.php file with the contents
<?php phpinfo();
If cURL is present on that page you are good to go.
Obviously I have not had the ability to test this code but something like this should work
$msisdn = substr($_REQUEST['VerifiedNumber'],-10);
$apiKey = "<Your Dial2verify API Key Here>";
$senderId = "<6 ALPHABETIC CHARACTERS ONLY>";
$message = "<Your SMS Text Here>";
$url = "http://host/SMS/SEND/$apiKey/$senderId/$msisdn";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, array('Msg' => $message));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($ch);
curl_close($ch);
echo 'Response from server:';
print_r($result);

By using `, you are using CURL via command line.
1st, check if the CURL extension for php is installed properly on you environment. Check if something about CURL is available when you print <?php phpinfo();?>
2nd, if CURL is installed, check on php.net/curl to know how to use CURL.
Here a simple example to call your webservice :
<?php
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,"http://www.host.com/SMS/SEND/$APIKey/$SenderID/$MobileNumber");
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS,"Msg=This+is+my+text+message");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$reply = curl_exec ($ch);
curl_close ($ch);
echo $reply;
?>

Related

How to access odata that produced by Nav webservice into html layout using PHP

I try to get odata produced by a navision webservice. When I directly access the url given using chrome browser, the page asks for user name and password and then chrome shows xml data as expected.
But when i use a PHP script, it always returns "1".
My code looks like this:
$url = 'http://103.7.1.182:14048/DynamicsNAV71-6/OData/Company(\'Unit%20G%205\')/Item_Master_on_hand_no_Desc';
$login = 'Gem-gae\senzo1:Bsbsenzo2018';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_USERPWD, $login);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type:application/x-www-form-urlencoded',
'Content-Length: ' . strlen(1))
);
$output = curl_exec($ch);
curl_close($ch);
echo $output;
Is there any mistakes with that code ?
Thanks for your help
There's no mistake on your code, it successfully connects to the external server via cURL and retrieves the data.
The value in $output (if you do a var_dump) is true (that's why you see 1 if you echo it).
Said that, the issue is on the server you are contacting (103.7.1.182:14048).
You probably need to send some data or something, I can't tell you exactly what because I don't know what is in that server.
To send data, you can add a curl_setopt line:
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
Where $data is an array.

Unable to upload file using curl

I need upload a file using php. I have the following code that I am using
<?php
$file = realpath('hello_world.jpg');
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'http://api.newocr.com/v1/upload?key=*My key*');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_POST, TRUE);
curl_setopt($ch, CURLOPT_POSTFIELDS, array('file' => '#'.$file));
$result = curl_exec($ch);
echo $result;
curl_close ($ch);
?>
On executing I get the error msg
{"status":"error","message":"File must be uploaded.
https://www.newocr.com/api/"}
But when I manually make a form and upload the image using multipart it works fine. Is something wrong with my code or the issue is with the API
Executing it from command line like this
curl -X POST -F "file=#hello_world.jpg" http://api.newocr.com/v1/upload?key=*my api key*
Works fine
curl_setopt($ch, CURLOPT_UPLOAD, TRUE);
Now, it is giving:
404 Page Not Found
The page you are looking for could not be found. Check the address bar to ensure your URL is spelled correctly. If all else fails, you can visit our home page at the link below.
Visit the Home Page
I had the same problem.
Solution:
<?php
$result = exec('curl -H "Expect:" -F file=#'.realpath($file).' http://api.newocr.com/v1/upload?key=KEY');
$result = json_decode($result, true);
return $result;
?>

Unable to access Flask web app from php curl

I have a Flask app, with a basic function, where I have exposed app.run() to a public ip, so that it is accessible from an external server;[ using Flask - Externally Visible Dev Server ]
#app.route('/')
def hello_world():
return 'Hello World!'
if __name__ == '__main__':
app.run(host = '0.0.0.0', port = 8080)
The curl request I have written in my php code is:
$signed_url = "http://my-ip-address:8080/";
$ch = curl_init($signed_url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, 0);
$data= curl_exec($ch);
echo $data;
I can do a curl request :
curl http://my-ip-address:8080/
from command line. However, when the curl request is embedded within my PHP code, it gives me an error "Connection refused".
Kindly help!
If the PHP code is on another server, but your command line cURL request is on the same server, then you aren't comparing apples to apples.
Two things that might be wrong:
Your Flask server has a firewall that doesn't allow external connections.
You are connecting using an private network IP address rather than a public IP address.
For now your PHP code looks correct, so I would narrow down the problem a little bit. Ignore that PHP code and try to connect using cURL on the command line from the same server you are running your PHP code on.
try to set your port with curl options like this:
curl_setopt($ch, CURLOPT_PORT, 8080);
so your signed url will be:
$signed_url = "http://my-ip-address";
I use this code for my work and worked :)
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'http://localhost:5000/spmi/api/1');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, "{\"teks_analysis\":\"tidak ada skor nol\"}");
curl_setopt($ch, CURLOPT_POST, 1);
$headers = array();
$headers[] = 'Content-Type: application/json';
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$result = curl_exec($ch);
if (curl_errno($ch)) {
echo 'Error:' . curl_error($ch);
}
curl_close($ch);
the key is CURLOPT_POSTFIELDS

need help converting php curl code to C language

my service provider has given me following piece of PHP code for accessing his service. I need help in converting to C lang code for use in my application. The code is using curl module to post on to a site.
pls advise.
<?php
$ch = curl_init();
curl_setopt($ch,CURLOPT_URL, "http://api.mVaayoo.com/mvaayooapi/MessageCompose?user="myusername":"mypassword"&senderID=TEST SMS&receipientno="phonenum"&msgtxt=This is a test from mVaayoo API&state=4");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, "user=$user&senderID=$senderID&receipientno=$receipientno&cid=$cid&msgtxt=$msgtxt");
$buffer = curl_exec($ch);
if(empty ($buffer))
{ echo " buffer is empty "; }
else
{ echo $buffer; }
curl_close($ch);
?>
Use libcurl with it's C-interface. The remainer is good old C-style-string-handling.
Your example libcurl program in the comment looks good, except that for a POST you need to install a CURLOPT_READFUNCTION, not a CURLOPT_WRITEFUNCTION. But if you just want to post a static buffer, use CURLOPT_POSTFIELDS instead of a callback function.

Use curl in php script

I'm trying to create a script that will delete all user properties for a particular individual. I'm able to use an api call to get the users' properties. And I'm trying use a delete api to remove each property. But I'm having an issue doing so. Below is the code:
$delete = "http://www.ourwiki.com/#api/DELETE:users/$user_id/properties/%s";
$xml = new SimpleXMLElement($xmlString);
foreach($xml->property as $property) {
$name = $property['name']; // the name is stored in the attribute
file_get_contents(sprintf($delete, $name));
}
I believe I need to use curl to perform the actual delete. Here is an example of that command (property=something):
curl -u username:password -X DELETE -i http://ourwiki.com/#api/users/=john_smith#ourwiki.com/properties/something
-u Provides external user authentication.
-X Specifies the HTTP request method.
-i Outputs the HTTP response headers. Useful for debugging.
Is this something that I can incorporate right into the existing script? Or is there something else I need to do? Any help would be greatly appreciated.
update:
<?php
$user_id="john_smith#ourwiki.com";
$url=('http://aaron:12345#192.168.245.133/#api/deki/users/=john_smith#ourwiki.com/properties');
$xmlString=file_get_contents($url);
$delete = "http://aaron:12345#192.168.245.133/#api/deki/DELETE:users/$user_id/properties/%s";
$xml = new SimpleXMLElement($xmlString);
function curl_fetch($url,$username,$password,$method='DELETE')
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE); // returns output as a string instead of echoing it
curl_setopt($ch,CURLOPT_USERPWD,"$username:$password"); // if your server requires basic auth do this
return curl_exec($ch);
}
foreach($xml->property as $property) {
$name = $property['name']; // the name is stored in the attribute
curl_fetch(sprintf($delete, $name),'aaron','12345');
}
?>
You can use php curl, or shell out to curl using exec.
If curl is already enabled on you web server, go with php curl. If you cant install php-curl copy a command line version of curl and you are good to go.
In php-curl to set the delete method do:
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'DELETE');
edit
Something like this:
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://www.ourwiki.com/#api/whatever/url/you/want/or/need");
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'DELETE');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE); // returns output as a string instead of echoing it
curl_setopt($ch,CURLOPT_USERPWD,"$username:$password"); // if your server requires basic auth do this
$output = curl_exec($ch);
edit2
stick that above code in a function:
function curl_fetch($url,$username,$password,$method='DELETE')
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE); // returns output as a string instead of echoing it
curl_setopt($ch,CURLOPT_USERPWD,"$username:$password"); // if your server requires basic auth do this
return curl_exec($ch);
}
and replace the call to file_get_contents() in your script with the new function.
curl_fetch(sprintf($delete, $name),'aaron','12345');
Done.
looks like you are looking for the curl function calls in php, including curl_setopt
See: http://php.net/curl

Categories