laravel curl not working on azure - php

I am connecting my two apps (laravel with python) using curl command. It is working absolutely fine on my local server without changing a single line but when it gives me following error when on azure server
POST https://myweb.com/searchJobs 500 ()
my code is
$ch = curl_init();
//URL to send the request to
curl_setopt($ch, CURLOPT_URL, 'http://python-scrapper-python001.azurewebsites.net/myfunction');
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Accept: application/json'));
//We are doing a post request
curl_setopt($ch, CURLOPT_POST, 1);
//adding the post data to request
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_json);
//Return instead of outputting directly
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$output = curl_exec($ch);
curl_close($ch);
is there any security protocol that I need to update on azure or what can be possible issue?

Actually these lines were working fine on localhost but not on azure
$arr = "";
$arr['hello'] = '7';
so I changed them as
$arr = [];
$arr['hello'] = '7';
Though i don't no why azure didn't run it. If php convert it to an array on localhost than it must run on azure as well

Since you are using the default domain provided by azure appservice. Instead of just using the HTTP protocol try to use the HTTPS since this is already free on the default domain provided. in this case try this changes.
$ch = curl_init();
//URL to send the request to
curl_setopt($ch, CURLOPT_URL, 'https://python-scrapper-python001.azurewebsites.net/myfunction');
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Accept: application/json'));
//We are doing a post request
curl_setopt($ch, CURLOPT_POST, 1);
//adding the post data to request
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_json);
//Return instead of outputting directly
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$output = curl_exec($ch);
curl_close($ch);

Related

firebaseio.com refusing connections after many requests

I use PHP CURL for making changes to the Firebase database. After many curl requests, suddenly, Failed to connect to something.firebaseio.com port 443: Connection refused error occours. After few hours, it starts working again.
Following is the curl script in php
$url = 'https://something.firebaseio.com/iochatting/roomchats/data.json?auth=private_key';
$json = json_encode($msg);
$headers = array();
$headers[] = 'Content-Type: application/json';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST,"POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, $json);
curl_setopt($ch, CURLOPT_HTTPHEADER,$headers);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, false);
curl_setopt($ch, CURLOPT_VERBOSE, true);
curl_setopt($ch, CURLOPT_TIMEOUT, 20500);
$response = curl_exec($ch);
if ($response === FALSE) {
}
curl_close($ch);
This code works fine most of the time but suddenly all connection requests are rejected.
Also, i have tried https://github.com/kreait/firebase-php , this too stops suddenly.
I want the curl to work not the php-sdk. Is there anyway ? Is google refusing server request ?
When the connections are rejected when sending request normally, if i use proxy in of port 443. It works again for few times.

Php Query regarding URL setting in CURL

I am new to PHP, just started exploring it a few hours ago.
I am working in Openshift and have deployed a CustomerApp Pod which has a Service name customer and port 8080.
I deployed another simple PHP app in the same project/namespace which has index.php with below content. When I try to call Route of my PHP app, it gives an 503 error.
I guess http://customer:8080 is being taken as a string and not an URL. What should I add to tell PHP that it should be considered as an URL?
<?php
$url="http://customer:8080";
$client=curl_init($url);
curl_setopt($client,CURLOPT_RETURNTRANSFER,1);
$response=curl_exec($client);
echo $response;
?>
The curl_init() call doesn't take an arg -
$url = "https://api.example.com:8000/get/some/info";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$curl_result = curl_exec($ch);
curl_close($ch);

Facebook Graph API to Post to Page not working on new server

So I have been using the below code on my shared hosting server and it has been working fine but now we have moved to a dedicated server but this script is not working?
The dedicated server has CentOS 6.8, Apache 2.2, PHP 7.0.14, MySQL 5.6 and does have curl installed.
I am using a permanent access token also.
$data['access_token'] = '{my permanent access token}';
$post_url = 'https://graph.facebook.com/{my feed ID}/feed';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $post_url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$return = curl_exec($ch);
curl_close($ch);
$dec = (Array)json_decode($return);
$link = "https://www.facebook.com/".$dec["id"];
I cannot find any errors in the console, is there any way for me to debug this?
Figured it out. I didn't force SSL on the old server but do on the new server so the below line needed to be added after curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);

How to make a call to .aspx https from php script from my localhost with xamp?

I am trying to send SMS from my localhost with xamp installed.
Requested page is on https and an .aspx page.
I am getting error: "HTTP Error 400. The request is badly formed." or blank page only in some cases.
Detaisl is as follows :
$url = 'https://www.ismartsms.net/iBulkSMS/HttpWS/SMSDynamicAPI.aspx';
$postArgs = 'UserId='.$username.
'&Password='.$password.
'&MobileNo='.$destination.
'&Message='.$text.
'&PushDateTime='.$PushDateTime.
'&Lang='.$Lang;
function getSslPage($url) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_REFERER, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
$result = curl_exec($ch);
curl_close($ch);
return $result;
}
$response = getSslPage($all);
echo "<pre>";
print_r($response); exit;
I tried every possible solution/combination found on internet but could not resolve that. The API developers do not have a example for php script.
I tried httpful php library and file_get_contents function but getting empty page. Also tried every combination with curl_setup.
I need to call this url without any post data and see the response from it.
Instead getting a blank page.
Please note that when I execute the url with all details in browser it works fine.
Can anybody help me in this regard.
Thank you,
Usman
First do urlencode over your data as follows:
$postArgs = 'UserId='. urlencode($username.
'&Password='.urlencode($password).
'&MobileNo='.urlencode($destination).
'&Message='.urlencode($text).
'&PushDateTime='.urlencode($PushDateTime).
'&Lang='.urlencode($Lang);
After that two possible solutions. One is using GET.
curl_setopt($ch, CURLOPT_URL, $url . "?" . $postArgs);
Second option is using POST method.
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postArgs);

How to make Php rest server send reponse back to client

I am making a simple PHP rest service, I am calling this service with CURL here is the code for this
//client code example
$ch = curl_init($URL);
//curl_setopt($ch, CURLOPT_MUTE, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: text/xml'));
curl_setopt($ch, CURLOPT_POSTFIELDS, $xml);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$output = curl_exec($ch);
Now on Rest Service I am receiving the request and doing the task. Then I have to send the response back.
//server code example
$xml_post = file_get_contents('php://input');
$xmlparser = new XMLParser;
$parsedata = $xmlparser->parse($xml_post);
$resultobj = new ResultGenerate;
$result = $resultobj->generate($parsedata);
I have no idea how to send the reponse ($result) back.So that $output has the xml string in the end. Please Help
echo $result
is all you need. think about it like this. when using PHP to serve web pages, all you are doing is serving a response to the web browser. You use echo there just like you are using echo here.

Categories